Control Your Motors With L293D and Arduino

569,237

552

152

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);
}

2 People Made This Project!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
user

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

Tips

Questions

151 Comments

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.

robot-v1.JPG

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.

Please Guys.

My email is olivierjohan101@yahoo.com