Learn to code

ARDUINO & CODES

Previous Post
Arduino Sensor: Detecting movement with a tilt sensor
Next Post
Direction and speed of a DC Motor with an H-Bridge and Arduino

Driving a brushed DC Motor with Arduino

in Arduino Motor Control

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: Arduino Tutorial: 4.8 Map function for analog results 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.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 10
Hobby DC Motor Hobby DC Motor × 1
Battery Snap Battery Snap × 1
1N4001 Diode 1N4001 Diode × 1
TIP120 Transistor TIP120 Transistor ×

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

TIP120 Transistor

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()
{
  Serial.begin(9600);
}
void loop()
{
  if ( Serial.available()) {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '9') // is ch a number?
    {
      int speed = map(ch, '0', '9', 0, 255);
      analogWrite(3, speed);
      Serial.println(speed);
    }
    else
    {
      Serial.print("Unexpected character ");
      Serial.println(ch);
    }
  }
}

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 = Serial.read();
    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);
      Serial.println(speed);
    }
Incorrect input from the user

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu