Arduino Mega - DC Motor

This guide will show you how to use the Arduino Mega to control a DC motor. In simple terms, we will cover:

Arduino Mega controlling a 5V DC motor with a DRV8833 motor driver

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
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×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

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

A DC motor uses two wires.

  • Positive wire is usually red.
  • Negative wire is usually black.
DC Motor Pinout

How It Works

When you buy a DC motor, you should know what voltage it needs. For example, a DC motor that runs on 12 volts. When you connect a 12V DC motor to a 12-volt power source:

  • Attach 12V to the red wire and connect the black wire to ground: the DC motor runs clockwise at full speed.
  • Attach 12V to the black wire and connect the red wire to ground: the DC motor runs counterclockwise at full speed.

If you swap the two wires of a DC motor, the motor will turn in the opposite direction. This simple trick helps you control which way the motor spins. It is done by programming, not by hand.

If you give a DC motor less than 12 volts, it starts turning but not at full speed. This shows that changing the voltage changes the speed. But changing the voltage directly is hard to do in real life. So there is an easier way to control the motor speed. Keep the power supply voltage the same and use a PWM signal to control speed. PWM stands for Pulse Width Modulation. It turns the voltage on and off quickly. When the on-time (duty cycle) is higher, the motor runs faster; when it is lower, it slows down.

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 Mega

To run a DC motor, you must control how fast it spins and which way it turns. The Arduino Mega can make a PWM signal, but that signal has too little voltage and current to drive the motor by itself. So you need a hardware driver to connect the Arduino Mega to the DC motor. This driver does two main jobs:

  • Make the PWM signal from the Arduino Mega stronger (higher voltage and more current) to control speed.
  • Get a control signal from the Arduino Mega to swap the power supply's polarity to control direction.
Arduino Mega control DC motor

※ NOTE THAT:

  • This guide can be used for any DC motor. We use a 12V motor as an example.
  • If you use a 5V DC motor, even though the Arduino Mega pin gives 5V (the same voltage as the motor), you still need a driver between the Arduino Mega and the motor. This is because the Arduino Mega pin cannot supply enough current to run the motor.

A DRV8833 driver sits between the Arduino Mega and the DC motors in this guide. Its MOSFET outputs waste almost no voltage, so a 5V motor really gets close to 5V.

Overview of DRV8833 Driver

In this guide, you will learn how to use the DRV8833 motor driver to run a DC motor.

DRV8833 Driver Pinout

DRV8833 Driver Pinout

One DRV8833 driver has room for two DC motors, motor A and motor B, and both can run at once. This tiny module has 12 pins. Six of them, IN1, IN2, VCC, GND, IN3 and IN4, sit along one edge. The other six, SLEEP, OUT1, OUT2, OUT3, OUT4 and FAULT, sit along the opposite edge.

The common pins for both motors:

  • VCC pin: Powers the motors. Voltage range: 2.7 to 10.8 volts.
  • GND pin: Common ground. It joins the minus wire of the motor supply and a GND pin of the Arduino Mega.
  • SLEEP pin (some boards print EEP): Wakes the driver up. If nothing is connected, the chip pulls it to LOW on its own, and then the driver sleeps. So connect it to the 5V pin of the Arduino Mega to keep the driver awake.
  • FAULT pin: An output that drops LOW when the chip overheats or the current is too high. We leave it free here.

Motor A pins (Channel A):

  • IN1 & IN2 pins: These two pins decide both how fast Motor A goes and which way it turns. Connect them to two PWM pins of the Arduino Mega.
  • OUT1 & OUT2 pins: These pins go to Motor A.

Motor B pins (Channel B):

  • IN3 & IN4 pins: These work like IN1 and IN2, but for Motor B.
  • OUT3 & OUT4 pins: These connect to Motor B.

The DRV8833 needs only one power input: the motor supply on VCC and GND. It makes its own logic voltage inside, so it needs no logic supply and there are no jumpers to remove. The wire from 5V to SLEEP carries only the wake-up signal. Its inputs accept both 3.3V and 5V signals. Each channel can give about 1.5A (2A for a short time), and the chip shuts itself off if it gets too hot, sees a short circuit, too much current, or too low a voltage. The board is so small that it needs no heat sink.

An Arduino Mega with a DRV8833 motor driver can also run a second DC motor at the same time as the first. Each motor needs just two PWM pins from the Arduino Mega.

※ NOTE THAT:

This guide shows you how to control a DC motor with channel A. The same steps can be used to control another DC motor.

Wiring Diagram

The Arduino Mega sends its signals from pins 10 and 9, and its 5V pin keeps SLEEP HIGH. The 5V adapter feeds the motor through the DC power jack.

The wiring diagram between Arduino Mega DC Motor DRV8833 Driver

This image is created using Fritzing. Click to enlarge image

DRV8833 Module Connected To
IN1 Arduino Mega pin 10
IN2 Arduino Mega pin 9
SLEEP Arduino Mega 5V
GND Arduino Mega GND and the - wire of the 5V adapter
VCC The + wire of the 5V adapter (via DC power jack)
OUT1, OUT2 The two wires of the DC motor
FAULT Not connected

How To Control the Speed of DC Motor via DRV8833 Driver

Speed control works without an ENA pin on the DRV8833. One IN pin gets the PWM signal while its partner IN pin stays LOW. Here is how to do it:

  • Pick two Arduino Mega pins that support PWM, and run one wire to IN1 and one wire to IN2 of the DRV8833 module.
  • Keep IN2 LOW, and use the analogWrite() function to send a PWM signal to IN1. The DRV8833 driver uses this PWM signal to power the DC motor.
digitalWrite(IN2_PIN, LOW); // IN2 stays LOW analogWrite(IN1_PIN, speed); // Write PWM value to IN1_PIN to control motor speed (0-255)

Speed can be any number from 0 to 255. If the speed is 0, the motor stops. If the speed is 255, the motor runs as fast as it can.

How To Control the Direction of DC Motor via DRV8833 Driver

The pin that gets the PWM signal decides which way the motor turns. The table below shows all states of IN1 and IN2 for channel 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
  • Motor A turns clockwise.
digitalWrite(IN2_PIN, LOW); analogWrite(IN1_PIN, speed);
  • Motor A spins counterclockwise.
digitalWrite(IN1_PIN, LOW); analogWrite(IN2_PIN, speed);

※ NOTE THAT:

If you connect OUT1 and OUT2 to the motor the wrong way, the motor will spin the other way. To fix it, just swap OUT1 and OUT2, or swap the jobs of IN1 and IN2 in your code.

How To Stop DC Motor Spinning

There are two ways to stop a DC motor.

  • Coasting means both inputs, IN1 and IN2, are LOW: the driver lets go of the motor, and it rolls to a stop.
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
  • Braking needs HIGH on both inputs, IN1 and IN2: the driver shorts the motor wires, and it stops very quickly.
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH);

How to control a DC motor using DRV8833 driver.

Arduino Mega Code

The code below does these things:

  • Make the DC motor run faster
  • Change direction
  • Make the DC motor run slower
  • Stop the motor
/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-dc-motor */ #define IN1_PIN 10 // The Arduino Mega pin connected to the IN1 pin of DRV8833 (PWM pin) #define IN2_PIN 9 // The Arduino Mega pin connected to the IN2 pin of DRV8833 (PWM pin) void setup() { // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); pinMode(IN2_PIN, OUTPUT); // IN1 and IN2 LOW: the motor is free and does not move digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW); } void loop() { // clockwise: IN2 stays LOW, the speed goes to IN1 as PWM 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, IN2 gets full speed digitalWrite(IN1_PIN, LOW); analogWrite(IN2_PIN, 255); delay(1000); // rotate at maximum speed 1 second in anti-clockwise direction // anti-clockwise: slow down by lowering the PWM on IN2 for (int speed = 255; speed >= 0; speed--) { analogWrite(IN2_PIN, speed); // control the speed delay(10); } delay(1000); // stop motor 1 second (IN1 and IN2 are both LOW now) }

Detailed Instructions

Follow these steps one by one.

  • Wire the parts according to the diagram.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Paste the code into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Plug the 5V adapter into the DC power jack.
  • Does the motor stay still? Check the wire between SLEEP and 5V first.
  • Observations:
    • The DC motor speeds up, then runs at full speed for 1 second.
    • The direction of the motor changes.
    • The motor runs at full speed for 1 second in the opposite direction.
    • The motor slows down.
    • The motor stops for 1 second.
    • This sequence repeats.

    ※ NOTE THAT:

    In this guide, we’ll learn how to change a DC motor’s speed relative to its maximum speed. To set the exact speed in rotations per second, you’ll need a PID controller and an encoder. We’ll cover how to control the precise speed of the DC motor in a separate guide.

    Want to run two motors, for example the two wheels of a robot car? The Arduino Mega - DRV8833 DC Motor Driver tutorial uses both channels of the module.

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!