DC Motor

In this project, you'll learn how to connect and control a DC motor with the Arduino.

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

Parts needed:
  • Arduino
  • DC motor
  • L293D H-Bridge driver chip
  • Wires
  • Breadboard
  • 4 AA batteries needed for the experiments and robot car
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 (5V) and negative (GND) of a power supply, and the motor will spin continously.

To reverse the spin direction, simply switch the two motor wires.
DC motor connection
2 To control the motor with an Arduino, a L293D H-Bridge driver chip is needed. The L293D H-Bridge driver chip is capable of controlling two DC motors. The pinouts are shown here.

Pin      Description
1      Enable pin 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 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 are for controlling motor 1, and inputs/outputs 3 and 4 are for controlling motor 2.
L293D H-Bridge
L293D H-Bridge pinouts
3 Making the connections

Make the following connections for one motor:

Arduino L293D Motor Description
11 1 Enable 1,2
10 2 Input 1
9 7 Input 2
3 & 6 motor wires Connection to motor
GND 5 Ground (negative)
5V 8 & 16 5V (positive) power
DC motor connections
4 L293D Control Logic

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

Enable 1,2 Input 1 Input 2 Motor Operation
0 x x Stop
1 0 0 Stop
1 0 1 Turn clockwise
1 1 0 Turn counter-clockwise
1 1 1 Stop

To control the speed, we need to send an analog signal to the Enable pin using the analogWrite command. Recall that the value specified is between 0 and 255. Bigger the value the faster the motor will spin, and 0 will stop the motor.

The Enable signal must be connected to one of the PWM digital pins on the Arduino in order for the analogWrite command to work.
5 Create a new BareMinimum program and type in this program.

The motor will continuously turn in one direction for two seconds and then in the other direction for two seconds.
int enable12Pin = 11;  // must be one of the PWM pins
int in1Pin = 10;
int in2Pin = 9;

void setup() {
  Serial.begin(9600);
  pinMode(enable12Pin, OUTPUT);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);  
}

void loop() {
  Serial.println("Clockwise");
  digitalWrite(in1Pin, LOW);       // set direction of motor 1
  digitalWrite(in2Pin, HIGH);      // the two inputs are always inverses of each other
  analogWrite(enable12Pin, 255);   // set speed for motor 1
  delay(2000);

  Serial.println("Counter Clockwise");
  digitalWrite(in1Pin, HIGH);       // set direction of motor 1
  digitalWrite(in2Pin, LOW);      // the two inputs are always inverses of each other
  analogWrite(enable12Pin, 255);   // set speed for motor 1
  delay(2000);
}
6 Run the program.