Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

How to Control a DC Motor with L293D and Arduino: A Complete Guide

小编

Published2025-10-15

This article walks you through the process of controlling a DC motor using the L293D motor driver and an Arduino. Whether you're a beginner or an intermediate hobbyist, this guide will provide step-by-step instructions and tips to help you create your own motorized projects.

DC motor, L293D, Arduino, motor control, robotics, motor driver, Arduino tutorial, DIY electronics, robotics project, L293D motor driver

Introduction to DC Motor, L293D, and Arduino Basics

Understanding DC Motors

A DC motor is one of the most common components used in robotics and electronic projects. It operates on direct current (DC), converting electrical energy into mechanical movement. DC motors are simple to control and can be used for a wide variety of applications, from moving robots to powering small household devices. A key advantage of DC motors is that their speed and direction can be easily controlled, making them a fundamental component in many DIY projects.

When you want to control a DC motor with an Arduino, a motor driver circuit is typically required. This is where the L293D motor driver comes into play.

The L293D Motor Driver: Why Use It?

The L293D is an integrated circuit (IC) designed to control the direction and speed of DC motors. It is often used in robotics, Arduino projects, and other DIY electronics. The L293D can drive two DC motors simultaneously, allowing for more complex control (like controlling a robot's movement) while maintaining low power consumption.

The IC features built-in diodes to protect against back electromotive force (back-EMF), which is a voltage spike that occurs when the motor is stopped suddenly. This is important because back-EMF can damage your circuit components. With L293D, you can also control the motor's speed and direction via pulse-width modulation (PWM) signals sent from the Arduino.

How Does the L293D Work with Arduino?

In a typical Arduino project, the L293D acts as a middleman between the Arduino and the motor. The Arduino sends control signals to the L293D, which then drives the motor based on those inputs. The L293D has four input pins for controlling the direction and speed of the motor, and two enable pins that allow you to enable or disable the motor.

To simplify:

The input pins from the Arduino control the direction of the motor's rotation.

The enable pins control whether the motor is turned on or off.

The PWM pin can be used to control the motor's speed by adjusting the power delivered to the motor.

Using the L293D, the Arduino can achieve both forward and reverse motion, as well as varying speed control.

Key Components You’ll Need

Before jumping into the wiring and programming, it’s important to gather the necessary components. For this project, you’ll need:

Arduino Board (e.g., Arduino Uno)

L293D Motor Driver IC

DC Motor

External Power Source for the motor (since the Arduino cannot provide enough current for the motor)

Jumper Wires for connecting the components

Breadboard (optional, for easy prototyping)

Resistors and Capacitors (for stabilizing the power supply and preventing noise)

Arduino IDE for writing and uploading code

Once you have all these components, you’re ready to start building your circuit.

Setting Up the Circuit

Let’s dive into the actual wiring. The L293D motor driver has 16 pins, but the key pins that you’ll be using are:

Pin 1 (1A) and Pin 2 (1Y): Controls motor 1's input and output.

Pin 3 (Enable 1): Enable pin for motor 1.

Pin 4 (Ground): Connect to the ground of the power supply.

Pin 5 (Input 1) and Pin 6 (Input 2): Control the direction of motor 1.

Pin 7 (Vcc2): Motor power supply pin (usually 5-12V depending on motor requirements).

Pin 8 (Vcc1): Power supply for the L293D (5V from Arduino).

Pin 9 (Ground): Connect to the ground.

Pin 10 (Input 3) and Pin 11 (Input 4): Control the direction of motor 2 (if you're using a second motor).

Pin 15 (Enable 2): Enable pin for motor 2.

Wiring the Components

Motor Connections

Connect the two terminals of the DC motor to the output pins (1Y and 2Y) of the L293D. Make sure to connect the motor’s positive and negative terminals to their respective pins to ensure correct polarity.

Arduino to L293D Connections

Pin 1 (Input 1) and Pin 2 (Input 2) will be connected to two digital pins on the Arduino (say, pin 9 and pin 10).

Enable Pin (Pin 1) should be connected to 5V on the Arduino to power the motor.

Ground Pin (Pin 4) from the L293D should be connected to the ground of the Arduino.

Power Supply for the Motor

The motor requires a separate power source, which should be connected to Pin 7 (Vcc2). This can be a battery or any other suitable DC power supply. The ground of the power supply should be connected to the ground of the Arduino.

Programming and Controlling the Motor

Writing the Arduino Code

Now that the hardware setup is complete, it's time to move on to programming the Arduino. We will write a simple program that controls the motor's rotation direction and speed.

Here’s a basic Arduino code that makes the motor rotate in both directions using the L293D.

// Define motor control pins

const int motorPin1 = 9; // Input 1 on L293D

const int motorPin2 = 10; // Input 2 on L293D

const int enablePin = 3; // Enable pin (PWM)

void setup() {

// Set the motor pins as outputs

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

// Start with motor off

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

analogWrite(enablePin, 128); // Set motor speed to half

}

void loop() {

// Motor moves forward

digitalWrite(motorPin1, HIGH);

digitalWrite(motorPin2, LOW);

delay(2000); // Run forward for 2 seconds

// Motor stops

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

delay(1000); // Pause for 1 second

// Motor moves backward

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, HIGH);

delay(2000); // Run backward for 2 seconds

// Motor stops

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

delay(1000); // Pause for 1 second

}

Explanation of the Code:

Motor Control Pins:

We define three pins—motorPin1, motorPin2, and enablePin—for controlling the motor direction and speed. The motorPin1 and motorPin2 are used to control the direction, while the enablePin is used to regulate the speed using PWM.

Setup:

In the setup() function, we set all control pins as output. Initially, we stop the motor by setting both motorPin1 and motorPin2 to LOW. We also use analogWrite() to set the motor’s speed (the value 128 corresponds to half-speed).

Loop:

The loop() function continuously alternates the motor’s direction. First, the motor moves forward for 2 seconds, then stops for 1 second, moves backward for 2 seconds, and stops again.

Testing the Circuit

Once the code is uploaded to your Arduino, you should be able to observe the motor rotating forward and backward. Adjust the analogWrite() value to control the motor speed. If everything is wired correctly, your DC motor should be up and running!

Conclusion: Bringing Your Projects to Life

By using an Arduino and L293D motor driver, you now have the ability to control DC motors for a wide range of applications, from simple DIY gadgets to complex robotics projects. Whether you're designing a robot or a motorized vehicle, understanding how to control motors is a fundamental skill in electronics and robotics.

Experiment with different motor speeds, directions, and add more motors for even more control. The possibilities are endless, and with the power of Arduino and L293D, you have all the tools you need to bring your ideas to life!

Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.