This example shows how to control a DC motor speed and direction of rotation using Arduino UNO and L293D motor driver chip.
The L293D quadruple half-H drivers chip allows us to drive 2 motors in both directions, with two PWM outputs from the Arduino we can easily control the speed as well as the direction of rotation of one DC motor. (PWM: Pulse Width Modulation).
Components Required:
- Arduino UNO board —> ATmega328P datasheet
- L293D driver
- 12V DC motor
- 10K ohm potentiometer
- Pushbutton
- 12V source
- Breadboard
- Jumper wires
Arduino DC motor control circuit:
Project circuit schematic diagram is the one below.
The speed of the DC motor (both directions) is controlled with the 10k potentiometer which is connected to analog channel 0 (A0) and the direction of rotation is controlled with the push button which is connected to pin 8 of the Arduino UNO board. If the button is pressed the motor will change its direction directly.
The L293D driver has 2 VCCs: VCC1 is +5V and VCC2 is +12V (same as motor nominal voltage). Pins IN1 and IN2 are the control pins where:
IN1 | IN2 | Function |
L | H | Direction 1 |
H | L | Direction 2 |
L | L | Fast motor stop |
H | H | Fast motor stop |
As shown in the circuit diagram we need only 3 Arduino terminal pins, pin 8 is for the push button which toggles the motor direction of rotation. Pins 9 and 10 are PWM signal outputs, at any time there is only 1 active PWM, this allows us to control the direction as well as the speed by varying the duty cycle of the PWM signal. The active PWM pin decides the motor direction of rotation (one at a time, the other output is logic 0).
Arduino code:
Arduino code is below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // Arduino DC motor speed and direction control #define button 8 #define pot 0 #define pwm1 9 #define pwm2 10 boolean motor_dir = 0; int motor_speed; void setup() { pinMode(button, INPUT_PULLUP); pinMode(pwm1, OUTPUT); pinMode(pwm2, OUTPUT); } void loop() { motor_speed = analogRead(pot) / 4; if(motor_dir) analogWrite(pwm1, motor_speed); else analogWrite(pwm2, motor_speed); if(!digitalRead(button)){ // If direction button is pressed while(!digitalRead(button)); // Wait until direction button released motor_dir = !motor_dir; // Toggle direction variable if(motor_dir) digitalWrite(pwm2, 0); else digitalWrite(pwm1, 0); } } |
The video below shows a simple hardware circuit of the example:
And the one below shows example simulation using Proteus:
Downloads:
To be able to simulate this example, Proteus needs the Arduino library which can be downloaded from the link below. After extracting the files (ARDUINO.IDX and ARDUINO.LIB) put it in the Library folder (ex: C:\Program Files\Labcenter Electronics\Proteus 8 Professional\LIBRARY):
Download
Arduino DC motor speed and direction control Proteus simulation file download:
Download
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
whenever i try to connect the arduino usb in my PC the port is unable to connect, please guide mefor this issue. I tried many things on youtube but still cant able to enable to port.
Looks great!
Do you have a flowchart available for this code?
If I want to control 2 motor what will be the change in code ?
Hi.
First of all congratulations on your work.
I am wondering if your code would work with the BTS7960 driver?
Thanks. The code will be changed if you use buttons with BTS 7960 module?
I read that on L293D takes voltage from range 4.5V to 36V. Does that mean if I supply high voltage, I can control motors to rotate at even higer speeds?
The supply voltage (VCC2) should be the same as motor nominal voltage, if you supply the motor with voltage higher than its nominal then your motor may be damaged!
if I were you, I would change 18 line to motor_speed = map(analogRead(pot),0,1023,0,255);
Yes, it may be used.
What should i do if i wanted to add a display to show the percentage of the speed? Can anyone provide me with the code?
So if you had to stop the motor in between a direction change using only that tactile switch,how would that be done? Like how a garage door operates
You can add a 3rd button to stop the motor whenever this button is pressed regardless of motor direction of rotation and speed.