From 9a5c2e37f43465e5265143ef5bb43f36a3ceb93e Mon Sep 17 00:00:00 2001 From: caternuson Date: Thu, 20 Mar 2025 10:08:57 -0700 Subject: [PATCH] adding DRV8833 code examples --- .../arduino/drv8833_onoff/drv8833_onoff.ino | 40 +++++++++++++ .../arduino/drv8833_pwm/drv8833_pwm.ino | 60 +++++++++++++++++++ .../drv8833_stepper/drv8833_stepper.ino | 30 ++++++++++ Adafruit_DRV8833/circuitpython/onoff/code.py | 37 ++++++++++++ Adafruit_DRV8833/circuitpython/pwm/code.py | 48 +++++++++++++++ 5 files changed, 215 insertions(+) create mode 100644 Adafruit_DRV8833/arduino/drv8833_onoff/drv8833_onoff.ino create mode 100644 Adafruit_DRV8833/arduino/drv8833_pwm/drv8833_pwm.ino create mode 100644 Adafruit_DRV8833/arduino/drv8833_stepper/drv8833_stepper.ino create mode 100644 Adafruit_DRV8833/circuitpython/onoff/code.py create mode 100644 Adafruit_DRV8833/circuitpython/pwm/code.py diff --git a/Adafruit_DRV8833/arduino/drv8833_onoff/drv8833_onoff.ino b/Adafruit_DRV8833/arduino/drv8833_onoff/drv8833_onoff.ino new file mode 100644 index 000000000..b95db1801 --- /dev/null +++ b/Adafruit_DRV8833/arduino/drv8833_onoff/drv8833_onoff.ino @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Basic ON/OFF control of DC motor via DRV8833 + +#define AIN1 5 +#define AIN2 6 +#define SLP 7 + +void setup() { + Serial.begin(115200); + Serial.println("Adafruit DRV8833 DC Motor Example - ON/OFF"); + + // configure pins + pinMode(AIN1, OUTPUT); + pinMode(AIN2, OUTPUT); + pinMode(SLP, OUTPUT); + + // enable DRV8833 + digitalWrite(SLP, HIGH); +} + +void loop() { + // + // FORWARD + // + Serial.println("Forward"); + digitalWrite(AIN1, HIGH); + digitalWrite(AIN2, LOW); + delay(1000); + + // + // REVERSE + // + Serial.println("Reverse"); + digitalWrite(AIN1, LOW); + digitalWrite(AIN2, HIGH); + delay(1000); +} diff --git a/Adafruit_DRV8833/arduino/drv8833_pwm/drv8833_pwm.ino b/Adafruit_DRV8833/arduino/drv8833_pwm/drv8833_pwm.ino new file mode 100644 index 000000000..163888dca --- /dev/null +++ b/Adafruit_DRV8833/arduino/drv8833_pwm/drv8833_pwm.ino @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// PWM speed control of DC motor via DRV8833 + +#define AIN1 5 +#define AIN2 6 +#define SLP 7 + +void setup() { + Serial.begin(115200); + Serial.println("Adafruit DRV8833 DC Motor Example - PWM"); + + // configure pins + pinMode(AIN1, OUTPUT); + pinMode(AIN2, OUTPUT); + pinMode(SLP, OUTPUT); + + // enable DRV8833 + digitalWrite(SLP, HIGH); +} + +void loop() { + // + // FORWARD + // + Serial.println("Forward"); + digitalWrite(AIN2, LOW); + // ramp speed up + Serial.println(" ramping up"); + for (int duty_cycle=0; duty_cycle<256; duty_cycle++) { + analogWrite(AIN1, duty_cycle); + delay(10); + } + // ramp speed down + Serial.println(" ramping down"); + for (int duty_cycle=255; duty_cycle>=0; duty_cycle--) { + analogWrite(AIN1, duty_cycle); + delay(10); + } + + // + // REVERSE + // + Serial.println("Reverse"); + digitalWrite(AIN1, LOW); + // ramp speed up + Serial.println(" ramping up"); + for (int duty_cycle=0; duty_cycle<256; duty_cycle++) { + analogWrite(AIN2, duty_cycle); + delay(10); + } + // ramp speed down + Serial.println(" ramping down"); + for (int duty_cycle=255; duty_cycle>=0; duty_cycle--) { + analogWrite(AIN2, duty_cycle); + delay(10); + } +} diff --git a/Adafruit_DRV8833/arduino/drv8833_stepper/drv8833_stepper.ino b/Adafruit_DRV8833/arduino/drv8833_stepper/drv8833_stepper.ino new file mode 100644 index 000000000..572e33cd1 --- /dev/null +++ b/Adafruit_DRV8833/arduino/drv8833_stepper/drv8833_stepper.ino @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: 2025 lady ada for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include + +// change this to the number of steps on your motor +#define STEPS 200 + +// create an instance of the stepper class, specifying +// the number of steps of the motor and the pins it's +// attached to +Stepper stepper(STEPS, 4, 5, 6, 7); + + +void setup() +{ + Serial.begin(9600); + Serial.println("Stepper test!"); + // set the speed of the motor to 30 RPMs + stepper.setSpeed(60); +} + +void loop() +{ + Serial.println("Forward"); + stepper.step(STEPS); + Serial.println("Backward"); + stepper.step(-STEPS); +} diff --git a/Adafruit_DRV8833/circuitpython/onoff/code.py b/Adafruit_DRV8833/circuitpython/onoff/code.py new file mode 100644 index 000000000..104405fde --- /dev/null +++ b/Adafruit_DRV8833/circuitpython/onoff/code.py @@ -0,0 +1,37 @@ +# SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import time +import board +import digitalio + +# Configure pins +AIN1 = digitalio.DigitalInOut(board.D5) +AIN2 = digitalio.DigitalInOut(board.D6) +SLP = digitalio.DigitalInOut(board.D7) + +AIN1.switch_to_output() +AIN2.switch_to_output() +SLP.switch_to_output() + +# Enable DRV8833 +SLP.value = True + +# Loop forever +while True: + # + # FORWARD + # + print("Forward") + AIN1.value = True + AIN2.value = False + time.sleep(1) + + # + # REVERSE + # + print("Reverse") + AIN1.value = False + AIN2.value = True + time.sleep(1) diff --git a/Adafruit_DRV8833/circuitpython/pwm/code.py b/Adafruit_DRV8833/circuitpython/pwm/code.py new file mode 100644 index 000000000..d1f542eb4 --- /dev/null +++ b/Adafruit_DRV8833/circuitpython/pwm/code.py @@ -0,0 +1,48 @@ +# SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import time +import board +import digitalio +import pwmio + +# Configure pins +AIN1 = pwmio.PWMOut(board.D5, frequency=2000) +AIN2 = pwmio.PWMOut(board.D6, frequency=2000) +SLP = digitalio.DigitalInOut(board.D7) + +SLP.switch_to_output() + +# Enable DRV8833 +SLP.value = True + +# Loop forever +while True: + # + # FORWARD + # + print("Forward") + AIN2.duty_cycle = 0 + print(" ramping up") + for duty_cycle in range(0, 65536, 100): + AIN1.duty_cycle = duty_cycle + time.sleep(0.01) + print(" ramping down") + for duty_cycle in range(65535, -1, -100): + AIN1.duty_cycle = duty_cycle + time.sleep(0.01) + + # + # REVERSE + # + print("Reverse") + AIN1.duty_cycle = 0 + print(" ramping up") + for duty_cycle in range(0, 65536, 100): + AIN2.duty_cycle = duty_cycle + time.sleep(0.01) + print(" ramping down") + for duty_cycle in range(65535, -1, -100): + AIN2.duty_cycle = duty_cycle + time.sleep(0.01)