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:
- How a DC motor works
- How to set the speed and direction of a DC motor
- Using a DRV8833 driver so the Arduino Mega can control a DC motor

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.

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.

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

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.

※ 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

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.

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.
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.
- Motor A spins counterclockwise.
※ 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.
- Braking needs HIGH on both inputs, IN1 and IN2: the driver shorts the motor wires, and it stops very quickly.
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
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.