HowTo: Arduino + L293D + DC motor

Hi all

I spent one whole day trying to learn how to interface an L293D motor driver chip with the Arduino and control a DC motor. I did get it working in the end though. It might be a bit tricky for starters like me to do this, so I would like to share how I did it and here's the link to my blog entry on this: http://akashxav.com/2009/04/18/arduino-l293d-dc-motor/ with code, circuit diagram, videos and pics.

Components you'll need:
Arduino, DC Motor, L293D chip, an external power source for the DC motor, an LED (optional)

The L293D's datasheet can be googled and might come in handy :slight_smile:

Thanks to Vladmir for valuable help.

P.S: When you feel everything is right but the circuit is still not working, just tighten the motor driver chip on the breadboard. Coz I spent half-a-day not knowing of this problem.

--
SingAlong

for(int i=255; i>=0; i–)

Probably ought to read something like

for(int i=255; i>=0; i–-)

or

for(int i=255; i>=0; --i)

Oh ya!

Thanks a lot. Corrected it.

Apparently I don't know how that i- thing worked without errors. I know that 'i-' is valid in C. But shouldn't it work different from i--? I'll try running the program with i-- and see the difference

hey no! :slight_smile:

I checked my post when I edited it. I had typed-in "i--" but Wordpress seems to have converted it to a different character (to the longer dash). Just a Wordpress thing. I corrected it.

I just used the HTML code for - instead of using it directly. I was using the HTML editor not the visual one :slight_smile:

Thanks for pointing it out

Is your circuit diagram correct?
You've got Arduino pin 7 going to 293D pin 6, which is an output, and 293D pin 7 (input) going to motor.

Shouldn't it be Arduino pin 7 to 293D pin 7 and 293D pin 6 to motor?

I'm not sure this is the best way of driving this chip.

well, i'll recheck that again :frowning:

I'll try doing the same thing again today anyway. bu my photos (zoomed on flickr) say that the circuit diagram is right

The diagram is wrong.

The motor should be across pins 3 and 6 of the L293D
You should be applying a PWM signal to either pin 2 or pin 7 of the L293D, and holding the other pin LOW,
So, if PWM is to pin 2, then pin 7 should be LOW and vice versa.

The enable pin (pin 1) should be HIGH.

well, Ya I just checked with the datasheet. PIN 6 and 7 have been exchanged.

But I was told in a previous thread that PWM is to be applied to Pin-1. It applies to the motor connected to half the chip. There's an enable pin for another motor.

I applied PWM to pin-1 and it worked fine. the video :slight_smile:

Apparently you can pwm by applying a pwm signal either to the enable pin or as mentioned by AWOL, to either pin 2 or 7.

Anyhow, I'm trying to use AWOL's approach but am having problems with bi-directional control. I can get the motor running in the proper direction by setting the direction pin (pin 2) to LOW and pwm'ing the other (pin 7). But to set the opposite direction, I set the direction pin to HIGH and invert the pwm. In this case I get no motor rotation.

Any ideas? Maybe I need to try another IC in case I damaged it somehow?

Thanks!

Dennis

I've thought about this a little further and I suppose the proper result could be achieved as follows:

Forward: pin 2 LOW, pin 7 PWM
Reverse: pin 2 PWM, pin 7 LOW

I'm just not sure if you can re-configure a PWM pin on the fly to act as a digital LOW (hrmmm... set duty cycle to 0?). May give this a try...

I wrote this a while ago, to provide a simple bi-directional motor speed control with an L293D and a simple pot.
There's a dead zone in the centre of the pot's travel where the power is completely off.

It's a bit rough, but works:

void loop ()
{
    int raw = analogRead (potPin) - 511;
    int absRaw = abs(raw);
    int speed = (absRaw < DEAD_ZONE) ? 0 : map (absRaw, DEAD_ZONE, 512, 0, 255);
        if (speed == 0) {
                digitalWrite (enaPin, LOW);
                digitalWrite (pwmPin1, LOW);
                digitalWrite (pwmPin2, LOW); 
        } else {
                digitalWrite (enaPin, HIGH);
                if (raw < 0) {
                        digitalWrite (pwmPin1, LOW);
                        analogWrite  (pwmPin2, speed);
                } else {
                        digitalWrite (pwmPin2, LOW);
                        analogWrite  (pwmPin1, speed);
                }
        }
}

hey thanks!

I was going to tinker with a pot this week. Useful code to start with

Turns out I had a bad L293D IC. I replaced it and everything works fine. Phew!

By the way, I found this code very useful for running the L293D w/ PWM:

http://www.arduino.cc/playground/Main/DirectionalMotorControlWithAL293D

It has some scary stuff in there, mucking around with the timers and such, but it gives you much more control with your PWM. It is not commented well, so to try to get to grips with it you really need to take a look at the ATMEL datasheet (start around p. 136):

These pages were also helpful:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1214327529
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234764073

I will post fully commented code with all the details once I get everything sorted out.

Dennis

As promised, I've posted my own implementation of a closed-loop DC motor controller that makes use of the L293D driver.

That post is here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245511066

Dennis

Hi all,

I've read a lot of stuff about the best way to control a DC motor with Arduino.
Finally I decided to put the things together using a external power supply and it worked. BUT... something (wrong, strange or ok) happens.

I've connected Arduino to USB. The Arduino 5V goes to L293D 5+VCC (IC PIN 16). And the external power source goes to IC PIN 8.
When everything is power-up, it works OK. But if I power off ONLY the external supply, the motor keeps moving (slower than before).
Is it OK? When there is no external power supply the IC uses the logic supply?
Or may I have a bad-connection somewhere?

Thanks in advance.

I'm not able to test your situation in my current setup easily but it sure sounds suspicious. You might try another IC.

Personally, I've had a lot of troubles with the L293D and have gone thru maybe 8 of these things. I think I'm operating close to the power dissipation limits of the device (33v, I'm not sure how much current I'm drawing).

Thus, I've moved on to the more robust L298 and have had much better results. It operates pretty much identically to the L293D, though you need to add your own protection diodes.

Dennis

I did it. Change to other L293D and without the "motor supply power" (PIN 8) the motor is still moving. Weird, uh?

Other suggetions?

Hi, can i use L293B for this tutorial?
http://www.eleparts.co.kr/epdf/IC/L293B.pdf

It should be fine, but you'll need to add protection diodes per the figures (the "D" version has them built in).