# Control Your Motors With L293D and Arduino

573,416

554

151

Posted

## Introduction: Control Your Motors With L293D and Arduino

After long research and trial and error, I have came up to a new walkthrough regarding this nice chip, the L293D.

Each project is one project and each one has its own unique power configurations, so you must be aware of the best battery choice and how to distribute voltage through your robot.

I strongly advice you to read the following articles:

Picking Batteries for your Robot
Once you've decided on batteries, how do you regulate the voltage

************************************************

L293D gives you the possibility to control two motors in both directions - datasheet

## Step 1: Basic Implementation

This is the most basic implementation of the chip.

As you can see, a 5V Voltage Regulator is between the battery and pins 1, 9, 16.

Pin 8 gets power before the VReg, if your motor needs for example 6V you should put 6V directly in this pin, all the other pins should not get more than 5V.

This will work with no problem at all, but if you want to do the right implementation take a look at the next example:

## Step 2: Advanced Implementation

This is the correct Implementation (with the capacitors), and note that pin 8 is feeded by unregulated voltage. This means that if your motors need more than 5V, you should power this pin with that amount of voltage, and the rest of the circuit with 5V.

## Step 3: Arduino Code

// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// --------------------------------------------------------------------------- Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};

// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

}

// --------------------------------------------------------------------------- Loop
void loop() {

drive_forward();
delay(1000);
motor_stop();
Serial.println("1");

drive_backward();
delay(1000);
motor_stop();
Serial.println("2");

turn_left();
delay(1000);
motor_stop();
Serial.println("3");

turn_right();
delay(1000);
motor_stop();
Serial.println("4");

motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
}

// --------------------------------------------------------------------------- Drive

void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

## Recommendations

• ### Arduino Class

68,129 Enrolled

• ### Space Challenge

We have a be nice policy.
Please be positive and constructive.

## Questions

This is a really informative tutorial, but I have one problem. I'm out of 100uF capacitors. Can I use a 10uF Capacitor? Or would that be a problem?

Thanks for the post. I made this circuit and it worked well. The photo was taken before I found the error on only hooking the unregulated power to one pin.

4 replies

i also make the same project,,, i need your help may you please help me ,, adnan.azeem274@gmail.com

Take a look here:

https://www.instructables.com/id/Collision-Avoidance-Robot-for-Teaching-Programming/

Hello,

Do you have a code for that car? I have the same exact one.

Here is the code for the CoderDojo Robots that use the L293D

https://github.com/dmccreary/coderdojo-robots/tree/master/src/collision-avoidance-ping

thanks, but does this work for two l293d controlling four motors.

Hi i want to motor pin speeds with serial reading or push button pull up input so can you help me to how to do it thanks

Hi Guys.

I enjoy this site very much, it gives me inspiration.

I want to build a PC Table with an Arduino where the Arduino powers a Actuator to drop and rise the screen into the Table.

I am a big gamer :)

What i am struggling with i a code and the Connections to it.

I built an H-Bridge using TIP3055 and TIP2955's

I have a external 12v power supply that i want to use for the Motor (12v-18v)

and also the Tactile push button that i want to use so that when the button is pushed, the Screen comes up out of the Table.

i am using a single button for it.

I might need to use a Pot for the speed of the Motor as it is a strong motor.

I have been searching on the net for a code as well as the Schematic, but i am failing to get any.

To be honest, i am loosing hope.

I hope someone can give me a hand.

My email is olivierjohan101@yahoo.com

why have we used capacitors

great

Can i use this circuit to control different motors (not at once), i have 9v, a 12v and a 24v. I know that the capacitors must be able to handle the voltage, but do i need capacitors with different farad ratings for each motor-setup?

Thank you for your replies!

how should i control speed (increase and decrease)? i am using 1000rpm geared motors( simple motors that work as if they are 300rpm), l293d motor drivers, 12V heavy battery, hc-05 bluetooth module.

you should use PWM - pulse width modulation
https://www.arduino.cc/en/Tutorial/PWM

How i connect our arduino to l293d motor bridge please sir help me

Hi I like your Instructables very much! Great job! I have a question that I think you may know the answer to. I need a small chip like this one that I can program and put it in a circuit so when it activates by a sensor ( in my case I want to put a light sensor (LDR))When light hits the light sensor It turns on a motor for a period of time (lets say 5 seconds). Then when the sensor finds it self in the dark it turns on the motor ( again for 5 sec) BUT IN THE opposite DIRECTION. All in All When there is lite it turns on the motor for 5 sec on one side (lets say left) when there is no light it turns the motor for 5 sec (on the oposite side, right) Do you know such a chip that will enable me to program it and make it do what I described??? Thank you! Stanislav

4 replies

You can use the same setup, simply save the states of the ldr and you can check if there was light before and have the arduino reverse the motor->

if ( pin 1 = high)
{
turn right
5 sec
}
if (pin 1 = low)
{
turn left
5 sec
}

else
{
do nothing
}

Hope I could help

Sir I am working with Arduino. I want to control god idle leg up and down with help of geared motor forward and reverse action please give me suggestions.

THANK YOU SO MUCH!!! OK, so I just upload this to the Arduino? On what pins do I connect the motor, LDR, power??? I needed someone to write the code because I am a complete n00b in programing... THANK YOU! Could you please now just help me, to say, put it together, like on what pins do I connect the motor, LDR, power... Can I remove the ATMega chip and use it with out the Arduino board, connecting the components directly to the chip? Thank you!

no you can't, it's just pseudocode so you can get an idea what you have to programm, sadly I don't own an arduino so I can't write it for you. sry