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 control 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.
 

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 (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


The Enable signal must be connected to one of the PWM ~ digital pins on the Arduino in order for the analogWrite command to work.
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
255 LOW LOW Stop
255 LOW HIGH Turn clockwise
255 HIGH LOW Turn counter-clockwise
255 HIGH HIGH Stop

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

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.
int enable12Pin = 11;  // must be one of the PWM ~ pins
int in1Pin = 10;
int in2Pin = 9;

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

  analogWrite(enable12Pin, 255);   // set speed for motor 1
  digitalWrite(in1Pin, LOW);       // set direction for motor 1
  digitalWrite(in2Pin, HIGH);
}

void loop() {
}
6 Run the program. The motor will continuously turn in one direction.