Control a DC Motor Using Arduino With L293D
by Tatsu Technologies in Circuits > Electronics
18623 Views, 7 Favorites, 0 Comments
Control a DC Motor Using Arduino With L293D
You need a few components in addition to an Arduino Uno board and a breadboard for this project. You first need a DC motor, with a nominal voltage close to 5V so it can be powered by the Arduino board alone. I just used a small motor that was left on my desk, and it worked just fine. In this project, we possibly want to control the motor in both directions, because we want to control curtains up and down for example. For this reason, we cannot simply use a transistor to command the motor, as it will only allow controlling the motor in one fixed direction. We will use a small integrated circuit, the L293D motor driver
Hardware & Software Requirements
- Arduino UNO R3 board
- L293D motor driver
- 5V DC motor
- Breadboard and jumper wires
Hardware Setup
1) Connect 5V and ground of the IC to 5V and ground of Arduino.
2) Connect the motor to pins 2 and 3 of the IC.
3) Connect IN1 of the IC to pin 8 of Arduino.
4) Connect IN2 of the IC to pin 9 of Arduino.
5) Connect EN1 of IC to pin 2 of Arduino.
6) Connect SENS A pin of IC to the ground.
7) Connect the Arduino using Arduino USB cable and upload the program to the Arduino using Arduino IDE software.
8) Provide power to the Arduino board using power supply, battery or USB cable.
The motor should now run first in the clockwise (CW) direction for 3 seconds and then counter-clockwise (CCW) for 3 seconds.
Code
const int pwm = 2;
const int in_1 = 8; const int in_2 = 9;
void setup() { pinMode(pwm,OUTPUT); pinMode(in_1,OUTPUT); pinMode(in_2,OUTPUT); }
void loop() {
//for clock wise motion
digitalWrite(in_1,HIGH); digitalWrite(in_2,LOW); analogWrite(pwm,255);
//delay for 3 sec Clock wise motion
delay(1000);
//for break
digitalWrite(in_1,HIGH); digitalWrite(in_2,HIGH); delay(200);
//for anticlock wise
digitalWrite(in_1,LOW); digitalWrite(in_2,HIGH); delay(3000);
//for break
digitalWrite(in_1,HIGH); digitalWrite(in_2,HIGH); delay(200);
}