Arduino Nano - DC Motor

This tutorial instructs you how to use Arduino Nano to control DC Motor. In detail, we will learn:

Arduino Nano controls a 5V DC motor with the DRV8833 motor driver

Hardware Preparation

1×Official Arduino Nano
1×Alternatively, DIYables Nano V3.0 Development Board
1×USB A to Mini-B USB cable
1×DRV8833 Motor Driver Module
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of DC Motor

DC Motor Pinout

DC Motor has two wires, the positive wire is usually red and the negative wire is usually black.

DC Motor pinout

How It Works

When purchasing a DC motor, it is important to be aware of the voltage at which it operates. For instance, let's consider a 12V DC motor.

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

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

Swapping the power pole between two wires of the DC motor will reverse its rotation direction. This technique is used to control the direction of the DC motor, not manually but by programming.

If the voltage of the power supply for a DC motor is lower than 12V, the motor will still rotate, but not at its maximum speed. Therefore, by adjusting the voltage of the power source, we can control the speed of the DC motor. Nevertheless, this approach is not typically used due to the difficulty in controlling the voltage of the power source. As an alternative, we keep the voltage of the power source fixed and control the speed of the DC motor through a PWM signal. The higher the duty cycle of the PWM, the faster 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 Nano

Controlling a DC motor involves two elements: speed and direction. Arduino Nano can generate a PWM signal, but this signal has low voltage and current, so it cannot be used directly to control the motor. We need to use a hardware driver between Arduino Nano and the motor. The driver performs two tasks:

  • Amplifying the PWM signal from Arduino Nano (in terms of current and/or voltage) for speed control
  • Receiving the control signal from Arduino Nano to switch the polarity of the power supply for direction control.
Arduino Nano control DC motor

※ NOTE THAT:

  • This tutorial can be used for all small DC motors. We are taking a 5V DC motor as an example.
  • Even though the Arduino Nano pin outputs 5V (which is the same voltage as the DC motor), a driver is still required between the Arduino Nano and the DC motor as the Arduino Nano pin does not provide enough current for the DC motor.
  • The 5V DC motor needs its own 5V power adapter. Do not power it from the Arduino Nano.
  • Do you have a 12V DC motor? Then use the BTS7960 motor driver instead, because the DRV8833 accepts at most 10.8V.

There are lots of chips and modules that can work as drivers for DC motors, such as the L293D, the DRV8833 and the BTS7960. In this guide, we'll be using the DRV8833 driver. It replaced the old L298N here, because that chip wastes about 2V inside, so a 5V motor got only about 3V.

Overview of DRV8833 Driver

The DRV8833 module is a tiny board with two H-bridges made of MOSFETs. It can drive two DC motors, or one bipolar stepper motor. This tutorial uses it to control one DC motor.

Here are the main facts:

  • Motor power supply on the VCC pin: 2.7V to 10.8V. A 5V adapter fits well.
  • Current: about 1.5A for each channel, and up to 2A for a short peak.
  • Logic: it works with 3.3V and 5V boards, so the Arduino Nano pins can drive it directly.
  • Protection: over-current, short circuit, over-heating (thermal shutdown) and too-low supply voltage (under-voltage lockout).
  • It loses almost no voltage, so the motor gets nearly the full 5V. It stays cool and needs no heat sink.

DRV8833 Driver Pinout

DRV8833 Motor Driver Pinout

With its two channels, the DRV8833 Driver can run two DC motors together: motor A and motor B. The module has twelve pins, six on each side.

The common pins for both motors:

  • VCC pin: the power for the motors, from 2.7V to 10.8V. Connect it to the + of the 5V power adapter.
  • GND pin: the common ground. Connect it to the - of the power adapter and to GND of the Arduino Nano.
  • SLEEP pin (printed EEP on some boards): it must be HIGH, or the driver sleeps and the motor does not move. The chip pulls it LOW inside, so connect it to the 5V pin of the Arduino Nano.
  • FAULT pin: it goes LOW when the chip finds a problem. It is optional and not used in this tutorial.

Motor A pins (Channel A):

  • IN1 & IN2 pins: these two inputs set both the direction and the speed of motor A. There is no ENA pin.
  • OUT1 & OUT2 pins: connect them to the two wires of motor A.

Motor B pins (Channel B):

  • IN3 & IN4 pins: these two inputs set the direction and the speed of motor B.
  • OUT3 & OUT4 pins: connect them to the two wires of motor B.

The DRV8833 has only one power input (VCC and GND). It does not need a separate 5V logic supply.

To control one motor, we need only two pins of the Arduino Nano, and both must be PWM pins. One more wire from the Nano's 5V pin to SLEEP keeps the driver awake.

※ NOTE THAT:

The remainder of this tutorial will focus on controlling a DC motor using channel A. Similar steps should be followed to control the other DC motor.

How To Control the Speed of DC Motor via DRV8833 Driver

The DRV8833 has no speed pin. The speed comes from a PWM signal on one IN pin, while the other IN pin stays LOW. To do this:

  • IN1 and IN2 of the DRV8833 each get their own PWM pin on the Arduino Nano
  • Generate a PWM signal on IN1 (or IN2) through the analogWrite() function. The DRV8833 Driver will pass this PWM signal to the DC motor with more power.
analogWrite(IN1_PIN, speed); // speed is a value from 0 to 255 digitalWrite(IN2_PIN, LOW);

The speed is a number ranging from 0 to 255. When the speed is 0, the motor will cease to move. When the speed is 255, the motor will rotate at its fastest rate.

How To Control the Direction of DC Motor via DRV8833 Driver

The pin that gets the PWM signal decides the direction. The table below shows the four states of motor A.

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

Accordingly:

  • Arduino code to make motor A rotate in a clockwise direction.
analogWrite(IN1_PIN, speed); digitalWrite(IN2_PIN, LOW);
  • Arduino code to make motor A rotate in a counter-clockwise direction.
digitalWrite(IN1_PIN, LOW); analogWrite(IN2_PIN, speed);

※ NOTE THAT:

The clockwise/counter-clockwise direction of the DC motor can be reversed by connecting the OUT1 & OUT2 pins to two pins of the motor in an opposite manner.

How To Stop DC Motor Spinning

Two methods for stopping a DC motor:

  • Coast: write LOW to IN1 and LOW to IN2, and the motor slows down little by little.
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
  • Brake: once IN1 and IN2 are both HIGH, the motor stops in a very short time.
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH);

How to control a DC motor using DRV8833 driver.

Wiring Diagram

Jumper wires go straight from the Arduino Nano pins to the DRV8833 module. The motor and the 5V adapter connect only to the module.

The wiring diagram between Arduino Nano and DC Motor DRV8833 Driver

This image is created using Fritzing. Click to enlarge image

DRV8833 Pin Connect To
IN1 Arduino Nano D10
IN2 Arduino Nano D9
SLEEP (EEP) Arduino Nano 5V
GND Arduino Nano GND and - of the 5V power adapter
VCC + of the 5V power adapter (via the DC power jack)
OUT1, OUT2 the two wires of the DC motor
FAULT nothing (leave free)

If you want the wires to hold firmly, put the Arduino Nano on a Screw Terminal Expansion Board for Arduino Nano and fix each wire with a screw. The picture below shows this version.

The wiring diagram between Arduino Nano and DC Motor DRV8833 Driver Screw Terminal

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Nano and other components.

Arduino Nano Code

The following code does the followings one by one:

  • Increases the speed of a DC motor
  • Changes the direction
  • Decreases the speed of a DC motor
  • Stops the DC motor
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-dc-motor */ const int IN1_PIN = 10; // The Arduino Nano pin connected to the IN1 pin of DRV8833 (PWM pin) const int IN2_PIN = 9; // The Arduino Nano pin connected to the IN2 pin of DRV8833 (PWM pin) // The setup function runs once on reset or power-up void setup() { // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); // start with the motor stopped digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); } // The loop function repeats indefinitely void loop() { // speed up in the clockwise direction: PWM on IN1, IN2 stays LOW digitalWrite(IN2_PIN, LOW); for (int speed = 0; speed <= 255; speed++) { analogWrite(IN1_PIN, speed); // control the speed delay(10); } delay(1000); // rotate at maximum speed 1 second in clockwise direction // change direction: IN1 goes LOW, full PWM on IN2 digitalWrite(IN1_PIN, LOW); analogWrite(IN2_PIN, 255); delay(1000); // rotate at maximum speed 1 second in anti-clockwise direction // slow down in the anti-clockwise direction for (int speed = 255; speed >= 0; speed--) { analogWrite(IN2_PIN, speed); // control the speed delay(10); } // stop: IN1 = LOW and IN2 = LOW digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); delay(1000); // stop motor 1 second }

Detailed Instructions

  • Do the wiring as shown in the diagram. Do not forget the wire from SLEEP to 5V. Keep the 5V adapter unplugged for now.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button on the Arduino IDE to upload the code to the Arduino Nano.
  • Plug the 5V adapter into the DC power jack.
  • You should observe:
    • The DC motor will speed up and then rotate at its maximum speed for 1 second.
    • The direction of the DC motor will be changed.
    • The DC motor will rotate at its maximum speed for 1 second in the opposite direction.
    • The DC motor will slow down.
    • The DC motor will stop for 1 second.
    • This process will be repeated continuously.
  • The motor stays still? Then the SLEEP wire is the first thing to check, because without 5V on SLEEP the DRV8833 stays asleep.

※ NOTE THAT:

This tutorial instructs you how to adjust the speed of a DC motor relative to its maximum speed. To control the absolute speed (in revolutions per second), we will need to use a PID controller and an encoder. A separate tutorial will cover controlling the absolute speed of the DC motor.

How to Control two DC Motors using DRV8833 Driver

One DRV8833 module can run two motors: the second motor goes to OUT3 and OUT4, and two more PWM pins of the Arduino Nano go to IN3 and IN4. The Arduino Nano - DRV8833 DC Motor Driver tutorial shows the wiring and the code for two motors.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!