DC Motor

In this project, you'll learn how to connect and control a DC motor with the Arduino. A DC motor can be used to turn the wheels of a robot.

A DC motor turns continuously in either direction depending on the polarity of the positive and negative wires connected to the motor. A L293D H-Bridge driver chip is needed to control the direction and speed of the motor by the Arduino.
 

Parts needed:
  • Arduino
  • DC motor
  • L293D H-Bridge driver chip
  • Wires
  • Breadboard
DC motor DC motor L293D H-Bridge
1 To manually turn on the DC motor, simply connect the two wires from the motor to the positive (red) and negative (black) of a power supply, and the motor will spin continously. DC motor connection
2 To reverse the spin direction, simply reverse the two motor wires. DC motor connection
3 Instead of manually flipping the motor wires to change the direction of the motor spin, we need to use the L293D H-Bridge motor driver chip to flip the wires for us under the control of the Arduino. The L293D H-Bridge driver chip is capable of controlling two DC motors.

Notice the notch at one end of the chip. Orient the chip so that this notch is on the left side. The pin right below this notch is pin 1. The pinouts are shown here.

Pin      Description
1      Enable pin for Inputs 1 & 2 for motor 1
2 & 7      Inputs 1 and 2 for controlling motor 1
3 & 6      Outputs 1 and 2 for connection to motor 1
9      Enable pin for Inputs 3 & 4 for motor 2
10 & 15      Inputs 3 and 4 for controlling motor 2
11 & 14      Outputs 3 and 4 for connection to motor 2
8      Vcc2 5V to 9V motor power supply
16      Vcc1 5V IC logic power supply
4, 5, 12 & 13      GND (just connect one)

The L293D H-Bridge driver chip has four sets of input/output signals, and each DC motor requires two sets of input/output signals to control it.
  • Inputs/outputs 1 and 2 (the bottom half) are for controlling motor 1.
  • inputs/outputs 3 and 4 (the top half) are for controlling motor 2.
To keep things simple we will not use the Enable 1,2 signal (pin 1 on the L293D chip). The default is that it is enabled.
L293D H-Bridge
L293D H-Bridge pinouts
4 Making the connections
4a Placing the L293D chip and connecting the motor wires

Plug in the L293D chip on the breadboard as shown in the picture. Place the breadboard with the red row at the top. Note the orientation and placement of the chip. The notch is on the left side. First align all the pins of the L293D chip correctly above the breadboard holes.
  • Pin 1 with the notch is on the left side.
  • Pin 5 (Gnd) is connected to the black battery negative wire.
  • Pin 8 (Vcc 2) is connected to the red battery positive wire.
Gently but with some pressure push the chip down.

 
  • Connect the two motor wires to pins 3 and 6 of the chip (yellow and green wires).




DC motor connections
4b Adding the Arduino control wires
  • Connect pin 6 of the Arduino to pin 2 of the L293D chip (purple wire).
  • Connect pin 5 of the Arduino to pin 7 of the L293D chip (purple wire).


Note that these two Input signals must be connected to the ~ PWM digital pins on the Arduino in order for the analogWrite command to work.
DC motor connections
4c Power and ground connections
  • Connect the 5V pin of the Arduino to pin 16 of the L293D chip (orange wire).
  • Connect the Vin pin of the Arduino to pin 8 of the L293D chip which also connects to the battery red positive wire (red wire).
  • Connect the GND pin of the Arduino to the blue row on the breadboard (black wire).
  • Connect the blue row on the breadboard to pin 5 of the L293D chip (black wire).
4d Final complete connections

Here are all the connections that you need to make for one motor:

Arduino L293D Motor Description
3 & 6 motor 1 wires Connection to motor
(yellow and green)
6 (PWM) 2 Input 1 (purple)
5 (PWM) 7 Input 2 (purple)
Vin 8 6V (red)
5V 16 5V (orange)
GND 5 Ground (black)
8 battery positive (red)
5 battery negative (black)
Final complete connections
5 L293D Control Logic

To control a DC motor, the correct signals must be sent to the two Input pins on the L293D chip for that motor as shown in the following table.

Input 1 Input 2 Motor Operation
LOW (or 0) LOW (or 0) Stop
HIGH (or a value between
1 and 255)
LOW (or 0) Turn in one direction
LOW (or 0) HIGH (or a value between
1 and 255)
Turn in the other direction

Use the digitalWrite command to send a LOW or HIGH value to the two input pins to control how the motor spins.

If you also want to control how fast the motor spins, you'll need to use the analogWrite command to send a value between 0 and 255 to the two input pins instead of the LOW and HIGH. This is why the two Input signals must be connected to the PWM ~ digital pins. The bigger the value the faster the motor will spin. The motor will not turn if the value is too small because not enough power is going to it.
6 Create a new program and type in this program.
int in1Pin = 6;   // must be one of the PWM ~ pins
int in2Pin = 5;   // must be one of the PWM ~ pins

void setup() {
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);  

  // turn on motor to spin in one direction
  analogWrite(in1Pin, 255);
  analogWrite(in2Pin, 0);
  delay(2000);
  // stop the motor
  analogWrite(in1Pin, 0);	
  analogWrite(in2Pin, 0);
}

void loop() {
}
7 Run the program. The motor will continuously turn in one direction for two seconds and then stop.