Arduino - DC Motor

In this tutorial, we are going to learn:

About DC Motor

DC Motor Pinout

DC Motor has two wires:

  • Positive wire: usually red
  • Negative wire: usually black
DC Motor Pinout

How It Works

When you buy a DC motor, you need to know what voltage DC motor work. Let's take a 12V DC motor as an example.

When you power the 12V DC motor by a 12V power source:

  • 12V and GND to the positive wire and negative wire, respectively: the DC motor rotates at maximum speed in the clockwise direction
  • 12V and GND to the negative wire and positive wire, respectively: the DC motor rotates at maximum speed in the anti-clockwise direction

As described above, when the power pole is swapped between two wires of the DC motor, the rotating direction is reversed. This method is used to control the direction of the DC motor. Of course, not by changing manually but by programming.

If we provide power to DC motors below 12V, the motor still rotates but not at maximum speed. It means if we change the voltage of the power supply, we can change the speed of the DC motor. However, this method is not used in practice because of the difficulty in controlling the voltage of the power source. Instead, we fix the voltage of the power source and control the speed of the DC motor via a PWM signal. The more duty cycle the PWM is, the higher speed the DC motor rotates.

How to control DC motor

The following animation shows how a PWM signal is used to control the speed of a DC motor:

How to control speed of DC motor by using PWM

How to control DC motor using Arduino

Controlling DC motor includes two factors: speed and direction. Arduino can generate the PWM signal. However, this PWM signal has low voltage and current, We cannot use it to control the DC motor. We need to use a hardware driver in between Arduino and DC motor. The driver does two works:

  • Amplify the PWM signal from Arduino (current and voltage) → for speed control
  • Receive the control signal from Arduino to swap pole of power supply → for direction control.
Arduino control DC motor

※ NOTE THAT:

  • This tutorial can be applied to all DC motors. 12V DC motor is just an example.
  • When you controls 5V DC motor, although Arduino pin outputs 5V (the same as DC motor voltage), you still needs a driver in between Arduino and DC motor because the Arduino pin does not provide enough the current for DC motor.

There are many chips and modules that can work as a DC motor driver (e.g. L293D, DRV8833). In this tutorial, we will use the DRV8833 driver. We moved this page from the old L298N to the DRV8833: the old driver dropped around 2V, which left a 5V motor with only about 3V.

About DRV8833 Driver

The DRV8833 driver can control DC motors and small stepper motors. In this tutorial, we learn how to use it to control the DC motor.

DRV8833 Driver Pinout

DRV8833 Driver Pinout

The DRV8833 driver can control two DC motors independently at the same time, called motor A and motor B. The module has 12 pins in two rows: IN1, IN2, VCC, GND, IN3, IN4 on one side, and SLEEP, OUT1, OUT2, OUT3, OUT4, FAULT on the other side.

The common pins for both motors:

  • VCC pin: supplies power for the motors. It can be anywhere between 2.7V and 10.8V.
  • GND pin: is a common ground pin. It needs to be connected to GND of the motor power supply and GND of Arduino.
  • SLEEP pin (some boards print EEP): the on/off switch of the chip. Inside, the chip pulls this pin LOW, and LOW means sleep. So we run a wire from SLEEP to the 5V pin of Arduino. With no wire here, the motor never turns.
  • FAULT pin: goes LOW when the chip has a problem, for example too much current or too much heat. It is optional, and we do not use it.

Motor A pins (Channel A):

  • IN1 & IN2 pins: are used to control both the speed and the spinning direction of Motor A. They need to be connected to two PWM pins of Arduino.
  • OUT1 & OUT2 pins: are connected to Motor A.

Motor B pins (Channel B):

  • IN3 & IN4 pins: are used to control the speed and the spinning direction of Motor B, in the same way as IN1 & IN2.
  • OUT3 & OUT4 pins: are connected to Motor B.

The DRV8833 has two MOSFET H-bridges inside. Each channel gives about 1.5A (2A peak). The outputs lose almost no voltage, so the chip runs cool and needs no heat sink. It also protects itself against over-current, short circuit, overheating and low supply voltage. The module needs no logic supply and has no jumpers: the wire on SLEEP only wakes the chip up. Its input pins work with both 3.3V and 5V signals.

We can control two DC motors independently at the same time by using an Arduino and a DRV8833 driver. To control each motor, we need only two PWM pins from Arduino.

※ NOTE THAT:

The rest of this tutorial controls a DC motor using channel A. Controlling the other DC motor is similar.

How To Control the Speed of DC Motor via DRV8833 Driver

The DRV8833 has no ENA pin. Instead, one input pin carries the PWM signal and the other input pin stays LOW. We can do this by:

  • Connect two PWM pins of Arduino to IN1 and IN2 of DRV8833
  • Keep IN2 LOW, and generate a PWM signal to IN1 by using analogWrite() function. The DRV8833 driver amplifies the PWM signal to the DC motor
digitalWrite(IN2_PIN, LOW); analogWrite(IN1_PIN, speed); // speed is a value from 0 to 255

The speed is a value between 0 and 255. If the speed is 0, the motor stops. If the speed is 255, the motor spins at maximum speed.

How To Control the Direction of DC Motor via DRV8833 Driver

The spinning direction of a motor depends on which input pin gets the PWM signal. The below table shows what motor A does for each state of IN1 and IN2 pins.

IN1 pin IN2 pin Direction
LOW LOW Motor A stops (coast)
HIGH HIGH Motor A stops (brake)
PWM LOW Motor A spins Clockwise
LOW PWM Motor A spins Anti-Clockwise
  • Control motor A spins clockwise
digitalWrite(IN2_PIN, LOW); analogWrite(IN1_PIN, speed);
  • Control motor A spins anti-clockwise
digitalWrite(IN1_PIN, LOW); analogWrite(IN2_PIN, speed);

※ NOTE THAT:

The spinning direction is inverted if the OUT1 & OUT2 pin connects to two pins of the DC motor in an inverse way. If so, it just needs to swap between OUT1 & OUT2 pin or swap the roles of IN1 and IN2 pin in the code.

How To Stop DC Motor Spinning

There are two ways to stop DC motor

  • Coast (free stop): set IN1 and IN2 pins to LOW. The motor turns freely and slows down by itself.
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
  • Brake (fast stop): set IN1 and IN2 pins to HIGH. The motor stops at once.
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH);

How to control a DC motor using DRV8833 driver.

Wiring Diagram

IN1 goes to pin 10 and IN2 goes to pin 9 of Arduino. Both are PWM pins. SLEEP goes to the 5V pin of Arduino, so the driver stays awake. The 5V power adapter powers the motor through the DC power jack and the VCC and GND pins of the module.

Arduino DC Motor DRV8833 Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

DRV8833 Pin Connect To
IN1 Arduino pin 10
IN2 Arduino pin 9
SLEEP Arduino 5V
GND Arduino GND and - of the 5V power adapter (via DC power jack)
VCC + of the 5V power adapter (via DC power jack)
OUT1, OUT2 the two wires of the DC motor
FAULT not connected

Want the wires to stay firm when the motor shakes? Put a Proto Screw Terminal Block Shield or a Screw Shield V2 on the Arduino Uno. Every pin gets its own screw terminal. The connections do not change, as you can see below.

Arduino DC Motor DRV8833 Screw Shield Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

The below code does:

  • Speed up DC motor
  • Change the direction
  • Speed down DC motor
  • Stop the motor
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dc-motor */ // constants won't change const int IN1_PIN = 10; // the Arduino pin connected to the IN1 pin of DRV8833 (PWM pin) const int IN2_PIN = 9; // the Arduino pin connected to the IN2 pin of DRV8833 (PWM pin) // the setup function runs once when you press reset or power the board void setup() { // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); motorStop(); // the motor does not move at start } // the loop function runs over and over again forever void loop() { // speed up in the clockwise direction for (int speed = 0; speed <= 255; speed++) { motorClockwise(speed); delay(10); } delay(1000); // rotate at maximum speed 1 second in clockwise direction // change direction motorAntiClockwise(255); delay(1000); // rotate at maximum speed 1 second in anti-clockwise direction // speed down in the anti-clockwise direction for (int speed = 255; speed >= 0; speed--) { motorAntiClockwise(speed); delay(10); } motorStop(); delay(1000); // stop motor 1 second } // clockwise: PWM on IN1, IN2 is LOW. speed: 0 to 255 void motorClockwise(int speed) { digitalWrite(IN2_PIN, LOW); analogWrite(IN1_PIN, speed); } // anti-clockwise: IN1 is LOW, PWM on IN2. speed: 0 to 255 void motorAntiClockwise(int speed) { digitalWrite(IN1_PIN, LOW); analogWrite(IN2_PIN, speed); } // stop (coast): IN1 and IN2 are LOW, the motor slows down by itself void motorStop() { digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); }

Quick Steps

  • Do the wiring as shown in the diagram above. Plug in the 5V power adapter last.
  • Check the wire from SLEEP to 5V of Arduino. Without it, the driver sleeps and the motor does not move.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • You will see:
    • DC motor is speeded up and then rotates at the maximum speed 1 second
    • DC motor's direction is changed
    • DC motor rotates at the maximum speed of 1 second in the reverse direction
    • DC motor is speeded down
    • DC motor stop 1 second
    • The above process is run repeatedly.

    ※ NOTE THAT:

    In this tutorial, we learn how to control the DC motor speed that is relative to the maximum speed. In order to control the absolute speed (round per second), we need to use a PID controller and an encoder. Controlling the absolute speed of the DC motor will be presented in another tutorial.

How to Control two DC Motors using DRV8833 Driver

For a second motor, use channel B: the motor goes to OUT3 and OUT4, and IN3 and IN4 take two more PWM pins of Arduino. The Arduino - DRV8833 DC Motor Driver tutorial shows the wiring and the code for two motors, like the two wheels of a small robot car.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES