Skip to content

Commit 9a5c2e3

Browse files
committed
adding DRV8833 code examples
1 parent e4aac8b commit 9a5c2e3

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// Basic ON/OFF control of DC motor via DRV8833
6+
7+
#define AIN1 5
8+
#define AIN2 6
9+
#define SLP 7
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
Serial.println("Adafruit DRV8833 DC Motor Example - ON/OFF");
14+
15+
// configure pins
16+
pinMode(AIN1, OUTPUT);
17+
pinMode(AIN2, OUTPUT);
18+
pinMode(SLP, OUTPUT);
19+
20+
// enable DRV8833
21+
digitalWrite(SLP, HIGH);
22+
}
23+
24+
void loop() {
25+
//
26+
// FORWARD
27+
//
28+
Serial.println("Forward");
29+
digitalWrite(AIN1, HIGH);
30+
digitalWrite(AIN2, LOW);
31+
delay(1000);
32+
33+
//
34+
// REVERSE
35+
//
36+
Serial.println("Reverse");
37+
digitalWrite(AIN1, LOW);
38+
digitalWrite(AIN2, HIGH);
39+
delay(1000);
40+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// PWM speed control of DC motor via DRV8833
6+
7+
#define AIN1 5
8+
#define AIN2 6
9+
#define SLP 7
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
Serial.println("Adafruit DRV8833 DC Motor Example - PWM");
14+
15+
// configure pins
16+
pinMode(AIN1, OUTPUT);
17+
pinMode(AIN2, OUTPUT);
18+
pinMode(SLP, OUTPUT);
19+
20+
// enable DRV8833
21+
digitalWrite(SLP, HIGH);
22+
}
23+
24+
void loop() {
25+
//
26+
// FORWARD
27+
//
28+
Serial.println("Forward");
29+
digitalWrite(AIN2, LOW);
30+
// ramp speed up
31+
Serial.println(" ramping up");
32+
for (int duty_cycle=0; duty_cycle<256; duty_cycle++) {
33+
analogWrite(AIN1, duty_cycle);
34+
delay(10);
35+
}
36+
// ramp speed down
37+
Serial.println(" ramping down");
38+
for (int duty_cycle=255; duty_cycle>=0; duty_cycle--) {
39+
analogWrite(AIN1, duty_cycle);
40+
delay(10);
41+
}
42+
43+
//
44+
// REVERSE
45+
//
46+
Serial.println("Reverse");
47+
digitalWrite(AIN1, LOW);
48+
// ramp speed up
49+
Serial.println(" ramping up");
50+
for (int duty_cycle=0; duty_cycle<256; duty_cycle++) {
51+
analogWrite(AIN2, duty_cycle);
52+
delay(10);
53+
}
54+
// ramp speed down
55+
Serial.println(" ramping down");
56+
for (int duty_cycle=255; duty_cycle>=0; duty_cycle--) {
57+
analogWrite(AIN2, duty_cycle);
58+
delay(10);
59+
}
60+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-FileCopyrightText: 2025 lady ada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Stepper.h>
6+
7+
// change this to the number of steps on your motor
8+
#define STEPS 200
9+
10+
// create an instance of the stepper class, specifying
11+
// the number of steps of the motor and the pins it's
12+
// attached to
13+
Stepper stepper(STEPS, 4, 5, 6, 7);
14+
15+
16+
void setup()
17+
{
18+
Serial.begin(9600);
19+
Serial.println("Stepper test!");
20+
// set the speed of the motor to 30 RPMs
21+
stepper.setSpeed(60);
22+
}
23+
24+
void loop()
25+
{
26+
Serial.println("Forward");
27+
stepper.step(STEPS);
28+
Serial.println("Backward");
29+
stepper.step(-STEPS);
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
9+
# Configure pins
10+
AIN1 = digitalio.DigitalInOut(board.D5)
11+
AIN2 = digitalio.DigitalInOut(board.D6)
12+
SLP = digitalio.DigitalInOut(board.D7)
13+
14+
AIN1.switch_to_output()
15+
AIN2.switch_to_output()
16+
SLP.switch_to_output()
17+
18+
# Enable DRV8833
19+
SLP.value = True
20+
21+
# Loop forever
22+
while True:
23+
#
24+
# FORWARD
25+
#
26+
print("Forward")
27+
AIN1.value = True
28+
AIN2.value = False
29+
time.sleep(1)
30+
31+
#
32+
# REVERSE
33+
#
34+
print("Reverse")
35+
AIN1.value = False
36+
AIN2.value = True
37+
time.sleep(1)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import pwmio
9+
10+
# Configure pins
11+
AIN1 = pwmio.PWMOut(board.D5, frequency=2000)
12+
AIN2 = pwmio.PWMOut(board.D6, frequency=2000)
13+
SLP = digitalio.DigitalInOut(board.D7)
14+
15+
SLP.switch_to_output()
16+
17+
# Enable DRV8833
18+
SLP.value = True
19+
20+
# Loop forever
21+
while True:
22+
#
23+
# FORWARD
24+
#
25+
print("Forward")
26+
AIN2.duty_cycle = 0
27+
print(" ramping up")
28+
for duty_cycle in range(0, 65536, 100):
29+
AIN1.duty_cycle = duty_cycle
30+
time.sleep(0.01)
31+
print(" ramping down")
32+
for duty_cycle in range(65535, -1, -100):
33+
AIN1.duty_cycle = duty_cycle
34+
time.sleep(0.01)
35+
36+
#
37+
# REVERSE
38+
#
39+
print("Reverse")
40+
AIN1.duty_cycle = 0
41+
print(" ramping up")
42+
for duty_cycle in range(0, 65536, 100):
43+
AIN2.duty_cycle = duty_cycle
44+
time.sleep(0.01)
45+
print(" ramping down")
46+
for duty_cycle in range(65535, -1, -100):
47+
AIN2.duty_cycle = duty_cycle
48+
time.sleep(0.01)

0 commit comments

Comments
 (0)