Public post

7.1 Driving a brushed DC Motor with Arduino


In this tutorial, you will learn how to turn a motor on and off. This tutorial will also cover the control of the speed of a brushed motor but will not cover the control over the direction of the motor. This tutorial will learn you how to turn a motor in one direction.

A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.

This tutorial will use analogWrite to control the speed of the motor. See the tutorial about reading analog signals for more on analogWrite and Pulse width Modulation (PWM).

Controlling the DC Motor

Controlling motors with an Arduino is more complicated than just controlling LEDs for a couple of reasons. First, motors require more current than the Arduino’s output pins can supply, and second, motors can generate their own current through a process called induction, which can damage your circuit if you don’t plan for it. However, motors make it possible to move physical things, making your projects much more exciting. They’re worth the complications!

Moving things takes a lot of energy. Motors typically require more current than the Arduino can provide. Some motors require a higher voltage as well. To start moving, and when it has a heavy load attached, a motor will draw as much current as it can. The Arduino can only provide 40 milliamps (mA) from its digital pins, much less than what most motors require to work.

The reverse is also true: a motor can generate electricity when the shaft is spun around. Try attaching an LED to the two leads of your motor, then spin the shaft with your hand. If nothing happens, spin the shaft the other way. The LED should light up. You’ve just made a tiny generator out of your motor. When you stop supplying energy to a motor, it will continue to spin, because it has inertia. When it’s spinning, it will generate a voltage in the opposite direction than the current you gave it. You saw this effect when you made your motor light up an LED. This reverse voltage, sometimes called back-voltage, can damage your transistor. For this reason, you should put a diode in parallel with the motor, so that the back voltage passes through the diode. The diode will only allow electricity to flow in one direction, protecting the rest of the circuit.

REMEMBER− Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC.

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 2x
Breadboard 400 points 1x
Hobby DC Motor 1x
1N4001 Diode 1x
TIP120 Transistor 1x

TIP120 Transistor

Tip120-transistor-1 dsddsdsad

Transistors are components that allow you to control high current and high voltage power sources from the low current output of the Arduino. There are many diferent kinds, but they work on the same principle. You can think of transistors as digital switches. When you provide voltage to one of the transistor’s pins, called the gate, it closes the circuit between the other two pins, called the source and drain. This way, you can turn a higher current/voltage motor on and of with your Arduino

DC Motor

Motors are a type of inductive device. Induction is a process by which a changing electrical current in a wire can generate a changing magnetic field around the wire. When a motor is given electricity, a tightly wound coil inside the housing of copper creates a magnetic field. This field causes the shaf (the part that sticks out of the housing) to spin around.

Breadboard Layout

Circuit Diagram – First, connect wires for power and ground. In the illustration, the red (power) and black (ground), connect to the two long vertical rows on the side of the breadboard. This provides access to the 5 volt supply and ground. We will not use this 5 volt rail but we do need the ground.

Connect pin 3 on the Arduino to the base pin of the TIP120. If you are looking at the transistor so that the metal tab is facing away from you, the base pin is on the left side of the transistor. This is the pin that will control open or close the . The transistor’s collector connects to one lead of the motor, the emitter to ground.

The other end of the motor connects to the positive lead of the 9V battery. Connect the battery’s ground to the Arduino’s ground.

Test Code

The below code is to test your breadboard layout. Before continuing in this tutorial, make sure that everything is wired correctly and that the code below works.

int motorPin = 3;
void setup() {
void loop() {
   digitalWrite(motorPin, HIGH);

The Code

   DC Motor Brushed sketch
   commands from serial port control motor speed
   digits '0' through '9' are valid where '0' is off, '9' is max speed
const int motorPins = 3; // motor driver is connected to pin 3
void setup()
void loop()
  if ( Serial.available()) {
    char ch =;
    if (ch >= '0' && ch <= '9') // is ch a number?
      int speed = map(ch, '0', '9', 0, 255);
      analogWrite(3, speed);
      Serial.print("Unexpected character ");

Code Explanation

Variable for DC Motor

Firstly, we will define the pin that is attached to the motor driver. Since we are going to use PWM to control the speed of the motor we will use pin 3 with the ~ symbol. That allows us to use Pulse Width Modulation.

const int motorPins = 3; // motor driver is connected to pin 3
Serial Communication

Since we will use the Serial Port for controlling the speed of the motor we need to start Serial communication.

void setup() { Serial.begin(9600); }
Read number from Serial Monitor

In the void loop() part of the sketch we will check whether the user has submitted a number. So if the user has entered a number that is between 0 – 9 the condition is met to execute the code of turning the DC motor.

if ( Serial.available()) { char ch =; 
if (ch >= '0' && ch <= '9') // is ch a number?
Map Function for the speed of the DC Motor

If the condition are not met none of the code will be executed. In other words, the motor will not spin and a message is printed to the Serial Monitor to let the user know that there is an “Unexpected character” entered.

int speed = map(ch, '0', '9', 0, 255); 
analogWrite(3, speed); 
Incorrect input from the user

If the condition is not true, none of the codes will be executed. In other words, the motor will not spin and a message is printed to the Serial Monitor to let the user know that there is an “Unexpected character” entered.

// goes from 180 degrees to 0 degrees 
for (angle = 180; angle >= 1; angle -= 1) 
// tell servo to go to position in variable 'pos'
// waits 20ms between servo commands

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member


Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.