
If you’re planning to build a robot, you’ll need to control various motors like DC motors, stepper motors, or servos, and the L293D Motor Driver Shield is the best option for this task. This shield is not only capable of handling these motor types but also eliminates the need for additional modules. The brains of this shield are its two L293D motor drivers and a 74HC595 shift register, which work together seamlessly. I’ve found this combination particularly useful for automating motorized systems in my own projects.
The L293D is a dual-channel H-Bridge motor driver, meaning it can control up to four DC motors or two stepper motors. The 74HC595 shift register, on the other hand, extends the Arduino’s digital pins by converting its four pins into eight direction control pins, making it easy to manage multiple motors. With these features, the L293D Shield ensures precision and efficiency for both simple and complex robotic designs.
The L293D Motor Driver Shield is a versatile and popular component designed for controlling various types of motors in Arduino projects. It simplifies motor operations, especially when dealing with DC motors, by acting as a reliable interface between the Arduino and the motors. When I first started using it, I appreciated how its compact design made it easy to connect and manage multiple motors without needing complex circuits or external power regulation.
This tutorial covers how to control a DC motor using the L293D Motor Driver Shield and Arduino. To set it up, you simply mount the shield onto your Arduino, connect your motor wires to the output pins, and upload a control code. The L293D handles the power and direction control efficiently, making it ideal for projects like robots, conveyor belts, or any automated system requiring precise motor control.


Connecting a DC Motor to an Arduino Uno using the L293D Motor Driver Shield opens up a world of possibilities for motor control. Whether you’re working with speed adjustments or directional changes, integrating components like an I2C LCD display, potentiometer (POT), and buttons adds precision and flexibility to your project. Below is a simplified guide to setting up this system for seamless operation.
DC Motor and Motor Control:
Connect the DC Motor to the M3 motor output terminal of the L293D Motor Driver Shield. This configuration allows precise control of motor speed and rotation direction, including clockwise and anti-clockwise rotation, using digital input pins.
Buttons for Control:
Use three buttons for motor commands:
Potentiometer (POT) for Speed Adjustment:
The potentiometer regulates speed by connecting one side to 5V, the other to GND, and the middle pin to an analog input pin (e.g., A0).
I2C LCD Display:
For visual feedback, link the I2C LCD display to the Arduino via SDA and SCL pins, ensuring efficient communication.
External Power Supply:
An external 5V power supply connects to the L293D Motor Driver Shield’s power terminals to ensure sufficient power for the motor. Remember to disconnect the jumper between the Arduino 5V and shield power when using an external source.
Arduino and Shield Alignment:
Mount the L293D Motor Driver Shield on the Arduino Uno, ensuring all pins align. Power the Arduino via USB or another external power source.
By setting up these components with proper connections and leveraging the versatility of the L293D Motor Driver Shield, you can create a reliable and efficient motor control system tailored to your project’s needs.
#include
// Library to run DC Motor Using Motor Driver Shield
#include
// Library to Run I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Format -> (Address,Columns,Rows )
// Create the motor object connected to M3
AF_DCMotor motor(3);
// Define button pins
const int forwardButtonPin = A1;
const int reverseButtonPin = A2;
const int stopButtonPin = A3;
// Define potentiometer pin
const int potPin = A0;
// Variables to store motor state and direction
bool motorRunning = false;
int motorDirection = BACKWARD; // FORWARD or BACKWARD
// Read the potentiometer value
int potValue;
int motorSpeed;
// Variable to store button states
bool forwardButtonState;
bool stopButtonState;
bool reverseButtonState;
// Inline function to check if button is pressed packed with debouncing logic
inline bool chkButtonState(int pinNum, int checkState, int debounceDelay) {
if (((digitalRead(pinNum) == checkState) ? true : false) == true) {
delay(debounceDelay);
return (((digitalRead(pinNum) == checkState) ? (true) : (false)) == true);
} else {
return false;
}
}
void setup() {
// initialize the lcd
lcd.init();
// Turn on the Backlight
lcd.backlight();
// Clear the display buffer
lcd.clear();
// Set cursor (Column, Row)
lcd.setCursor(0, 0);
lcd.print("DC Motor using");
lcd.setCursor(0, 1);
lcd.print("L293D Shield");
// Set button pins as inputs
pinMode(forwardButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin , INPUT_PULLUP);
pinMode(reverseButtonPin, INPUT_PULLUP);
// Start with motor off
motor.setSpeed(0);
motor.run(RELEASE);
delay(2000);
// Clear the display buffer
lcd.clear();
// Set cursor (Column, Row)
lcd.setCursor(0, 0);
lcd.print("Motor Direction:");
lcd.setCursor(0, 1);
lcd.print("Stopped ");
}
void loop() {
// Read the potentiometer value for changing speed as per Analog input
potValue = analogRead(potPin);
motorSpeed = map(potValue, 0, 1023, 0, 255);
// Read the button states
forwardButtonState = chkButtonState(forwardButtonPin, LOW, 20);
reverseButtonState = chkButtonState(reverseButtonPin, LOW, 20);
stopButtonState = chkButtonState(stopButtonPin, LOW, 20);
// check for Forward run
if (forwardButtonState && (!motorRunning || motorDirection == BACKWARD)) {
// Set cursor (Column, Row)
lcd.setCursor(0, 1);
lcd.print("Clock ");
if (motorDirection == BACKWARD) {
motor.setSpeed(0);
motor.run(RELEASE);
delay(1000);
}
motorRunning = true;
motorDirection = FORWARD;
motor.setSpeed(motorSpeed);
motor.run(FORWARD);
}
// check for Reverse run
else if (reverseButtonState && (!motorRunning || motorDirection == FORWARD)) {
// Set cursor (Column, Row)
lcd.setCursor(0, 1);
lcd.print("Anti-Clk");
if (motorDirection == FORWARD) {
motor.setSpeed(0);
motor.run(RELEASE);
delay(1000);
}
motorRunning = true;
motorDirection = BACKWARD;
motor.setSpeed(motorSpeed);
motor.run(BACKWARD);
}
// Stop motor
else if (stopButtonState && motorRunning) {
// Set cursor (Column, Row)
lcd.setCursor(0, 1);
lcd.print("Stopped ");
motorRunning = false;
motor.setSpeed(0);
motor.run(RELEASE);
}
// Adjust motor speed if running and display speed on LCD
if (motorRunning) {
motor.setSpeed(motorSpeed);
// Set cursor (Column, Row)
lcd.setCursor(9, 1);
lcd.print("SPD:");
lcd.print(((motorSpeed*100)/255));
lcd.print("% ");
}
}
Looking for affordable components for this Arduino project? Check out QKZee Technologies, an online shop in Lahore, Pakistan, offering the best components for students and DIY projects. Whether you’re looking for sensors, modules, or other electronics at a cheap price, they’ve got it all. Visit them at QKZeeTech.