How to Use L293D Motor Driver with Arduino: Simple Guide
To use the
L293D motor driver with an Arduino, connect the motor driver inputs to Arduino digital pins and power the motors separately. Then, write Arduino code to set the input pins HIGH or LOW to control motor direction and speed using PWM signals.Syntax
The L293D motor driver uses input pins to control motor direction and enable pins to control motor speed. You connect the motor driver inputs (IN1, IN2 for motor 1) to Arduino digital pins. The enable pin (EN1) controls if the motor runs and can be connected to a PWM pin for speed control.
Basic wiring includes:
- IN1, IN2: Control motor direction by setting HIGH/LOW.
- EN1: Enable motor and control speed with PWM.
- Motor power: Connect motor power supply to Vcc2 pin.
- Ground: Common ground between Arduino and motor driver.
arduino
const int IN1 = 9; // Arduino pin connected to L293D IN1 const int IN2 = 8; // Arduino pin connected to L293D IN2 const int EN1 = 10; // Arduino PWM pin connected to L293D EN1 void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(EN1, OUTPUT); } void loop() { // Example to run motor forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(EN1, 255); // Full speed delay(2000); // Stop motor analogWrite(EN1, 0); delay(1000); // Run motor backward digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(EN1, 255); delay(2000); // Stop motor analogWrite(EN1, 0); delay(1000); }
Example
This example shows how to control a single DC motor connected to the L293D motor driver using Arduino. It runs the motor forward at full speed for 2 seconds, stops for 1 second, then runs it backward for 2 seconds, and repeats.
arduino
const int IN1 = 9; const int IN2 = 8; const int EN1 = 10; void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(EN1, OUTPUT); } void loop() { // Motor forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(EN1, 255); // Full speed delay(2000); // Motor stop analogWrite(EN1, 0); delay(1000); // Motor backward digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(EN1, 255); delay(2000); // Motor stop analogWrite(EN1, 0); delay(1000); }
Output
Motor runs forward for 2 seconds, stops for 1 second, runs backward for 2 seconds, stops for 1 second, then repeats.
Common Pitfalls
- Incorrect wiring: Not connecting the motor power supply or ground properly can prevent the motor from running.
- Missing enable pin control: Forgetting to set the enable pin HIGH or use PWM will keep the motor stopped.
- Power supply issues: Using Arduino 5V to power motors can damage the board; use an external power supply for motors.
- Wrong pin modes: Not setting Arduino pins as OUTPUT causes no signal to motor driver.
arduino
/* Wrong way: Missing enable pin HIGH */ const int IN1 = 9; const int IN2 = 8; const int EN1 = 10; void setup() { pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(EN1, OUTPUT); } void loop() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); // Missing analogWrite(EN1, 255); so motor won't run delay(2000); } /* Right way: Enable pin set HIGH or PWM */ void loop() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(EN1, 255); // Enable motor at full speed delay(2000); }
Quick Reference
Remember these key points when using L293D with Arduino:
- Connect IN pins to Arduino digital outputs to control motor direction.
- Use EN pins with PWM to control motor speed.
- Provide separate power supply for motors (Vcc2) and common ground.
- Set Arduino pins as OUTPUT before use.
- Use
analogWrite()on EN pins for speed control,digitalWrite()on IN pins for direction.
Key Takeaways
Connect L293D input pins to Arduino digital pins to control motor direction.
Use the enable pin with PWM signals to control motor speed.
Always power motors with an external supply and share ground with Arduino.
Set Arduino pins as OUTPUT before controlling the motor driver.
Check wiring carefully to avoid common mistakes like missing enable signals.