adding DRV8833 code examples

This commit is contained in:
caternuson 2025-03-20 10:08:57 -07:00
parent e4aac8bdbb
commit 9a5c2e37f4
5 changed files with 215 additions and 0 deletions

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2025 lady ada for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <Stepper.h>
// 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);
}

View file

@ -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)

View file

@ -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)