This example shows how to control both speed and direction of a DC motor using Arduino.
The DRV8833 can drive two DC motors even though this example only shows a single motor on the "A" set of pins. A second motor can be driven in the same way. Just use the "B" set of pins for both input and output. Code would be the same, updated for the specific pins connected to the "B" channel inputs.
Wiring
Here is the wiring diagram to use for these examples. We show a Metro 328, but in general any microcontroller can be used. For speed control, PWM outputs are needed, two per DC motor.
The Metro connections are:
- GND to GND
- AIN1 to Digital 5
- AIN2 to Digital 6
- SLP to Digital 7
Motor connections are:
- AOUT1 to DC motor
- AOUT2 to DC motor
The wiring connection order for the DC motor generally does not matter and will only affect the resulting motor direction.
External power is used for the motor and is connected via the screw terminal blocks. This power supply must be enough to support whatever DC motor is being used, within the range capable of the DRV8833 (2.7V to 10.8V).
The SLP pin can also be tied directly to 5V (or 3.3V for a 3V logic board) output pin on the Metro for the DRV8833 to be "always active".
Basic ON/OFF Control
This example does not control speed but is useful for understanding basic motor control. For this, only two digital output pins are needed, which should be readily available on any microcontroller board. Control is done using the digitalWrite() command.
The DRV8833 datasheet has a table which summarizes what the motor behavior is for the various combinations of the two digital pin (xIN1 and xIN2) states:
To keep things simple for this example, we only demonstrate forward and reverse operation. Here is the example sketch:
// 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); }
PWM Speed Control
OK, now let's control speed using PWM. In Arduino, that's done using the analogWrite() command. The pins 5 and 6 on the Metro 328 were chosen since they support PWM output. The general idea is to set one pin LOW (or HIGH) and then PWM the other pin.
The DRV8833 has two PWM "modes" which it refers to as "fast decay" and "slow decay". The datasheet includes a table which summarizes pin state for each mode:
To keep things simple for this example, we only show the "fast decay" method. Here is the example sketch:
// 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); } }
Page last edited March 25, 2025
Text editor powered by tinymce.