# L293D won't turn motor backwards

My small robot has two motors controlled by an L293D and that is controlled via a Raspberry Pi. They will both go forwards but only one will go backwards.

I've tried different motors and tried different sockets in the breadboard, no luck. Either the L293D's chip is broken (but then it wouldn't go forwards) or I've wired it wrong.

I followed the tutorial, Controlling DC Motors Using Python With a Raspberry Pi, exactly.

Here is a run down of what works. Let the 2 motors be A and B:

When I use a python script (see end of post) both motors go "forwards". When I change the values in the Python script, so the pin set to HIGH and the pin set to LOW are swapped, motor A will go "backwards", this is expected. However, motor B will not move at all.

If I then swap both motors' wiring then the original python script will make both go backwards but swapping the pins in the code will make motor A go forwards but motor B won't move.

So basically, motor A will go forwards or backwards depending on the python code but motor B can only be changed by physically changing the wires.

This is forwards.py

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor2A = 23
Motor2B = 21
Motor2E = 19

Motor1A = 18
Motor1B = 16
Motor1E = 22

GPIO.setup(Motor1A, GPIO.OUT)
GPIO.setup(Motor1B, GPIO.OUT)
GPIO.setup(Motor1E, GPIO.OUT)

GPIO.setup(Motor2A, GPIO.OUT)
GPIO.setup(Motor2B, GPIO.OUT)
GPIO.setup(Motor2E, GPIO.OUT)

print("ON")
GPIO.output(Motor1A, GPIO.HIGH)
GPIO.output(Motor1B, GPIO.LOW)
GPIO.output(Motor1E, GPIO.HIGH)

GPIO.output(Motor2A, GPIO.HIGH)
GPIO.output(Motor2B, GPIO.LOW)
GPIO.output(Motor2E, GPIO.HIGH)


And this is backwards.py

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor2A = 21
Motor2B = 23
Motor2E = 19

Motor1A = 16
Motor1B = 18
Motor1E = 22

GPIO.setup(Motor1A, GPIO.OUT)
GPIO.setup(Motor1B, GPIO.OUT)
GPIO.setup(Motor1E, GPIO.OUT)

GPIO.setup(Motor2A, GPIO.OUT)
GPIO.setup(Motor2B, GPIO.OUT)
GPIO.setup(Motor2E, GPIO.OUT)

print("ON")
GPIO.output(Motor1A, GPIO.HIGH)
GPIO.output(Motor1B, GPIO.LOW)
GPIO.output(Motor1E, GPIO.HIGH)

GPIO.output(Motor2A, GPIO.HIGH)
GPIO.output(Motor2B, GPIO.LOW)
GPIO.output(Motor2E, GPIO.HIGH)


If you see this diff https://www.diffchecker.com/skmx6084, you can see the difference:

Below are some pictures. You can use the colour of the cables to link them between pictures

• Either you have a bug in your code or check the wiring between the Raspi and the L293D. Everything else literally makes no sense. Jan 30 '16 at 22:09
• @TobiasK I've checked the wiring and the code is the same code but GPIO the other way round (so the one that was HIGH us now LOW) Jan 30 '16 at 22:37
• Maybe one pin does not work and the L293D is reading the floating pin as a low-level and therefor switching it correctly. Swap the two controller wires from the raspi (and let the enable) and see wether the motor is now able to drive only backwards. Jan 30 '16 at 23:30
• @TobiasK Yes it now goes only backwards Jan 31 '16 at 0:19
• @Greenonline I'll post the code when I get home. I basically used the code in the tutorial, set 1E and 2E to HIGH (the enable PINS) and switched around 2A and 1A with 2B and 1B respectively. That made motor 1 go backwards but motor 2 did nothing. When I changed the physical wires the same situation occurred, but reversed, they would go backwards noe but only one would go forwards. Feb 1 '16 at 13:01

The motor driver chip you state you are using, the L293D, is a "quadruple half H driver." This means that, instead of two full H circuits capable of driving a motor forward and reverse, you have four half H circuits, which are only capable of driving a motor in one direction.

You even speculate in your post,

Either the L293D's chip is broken (but then it wouldn't go forwards) or I've wired it wrong.

I'm going to guess that you have not wired the motors to the driver chip correctly. This is just a guess, though, because you haven't posted pictures or schematics of your wiring.

In looking at a diagram of the chip, it looks like maybe an easy place to go wrong would be to wire into a ground pin on the chip instead of to one of the motor outputs.

You can see in the picture above from the datasheet (linked above) that the "heat sink and ground" pins are right beside the driver output pins. If you happened to wire one of your motor leads to pins 4, 5, 12, or 13 instead of 3, 6, 11, or 14, then the motor would still turn one direction (HIGH to LOW), but would not turn in another direction (LOW to LOW) because it's not connected to the driver pin that should be HIGH.

Again, pure speculation on my part, but it would seem to explain all of your symptoms. Please take a picture of your wiring and edit your question to include it.

## :EDIT:

It's hard to tell in your pictures, and I can't see which way the chip is oriented, but it looks like:

1. Gold wires are Vcc
2. The teal wire is Pin 19, which you have as "Motor 2E" - or motor 2 enable, HOWEVER, it's not plugged in to the (3,4EN) or (1,2EN) pin on the L293D chip. Those are pins 9 and 1, respectively, and they are located on the corners of the chip. It looks like it's maybe plugged in to 4A (or 2A, again I can't tell the orientation of the chip).
3. Purple with maybe a white stripe is Pin 23, which you have listed as "Motor 2B", but that wire goes to a corner of the chip, which is where the motor enable is located.

So it looks like to me that when you set "Motor 2E", to enable motor 2, you are actually NOT enabling motor two, but instead you are setting maybe what you're calling 2A in your code. Then, when you think you are going "forward", you set "2A" LOW and "2B" high - what you're calling "2B" is actually the motor enable, and that's why it's turning on.

When you try to turn the other way, it looks like you're setting "2A" HIGH and "2B" LOW, but again what you're calling "2B" is actually the motor enable pin, so you're disabling the motor.

Try swapping 2B and 2E in your code (swap 23 and 19).

If that doesn't work, please post clearer pictures and we can troubleshoot some more. Particularly, I'm looking to see the chip's orientation and a better (crisper and better lit) shot of the wires where the enter the Raspberry Pi. Also, you have 3 white wires or very light gray and it's hard to tell where they're going.

• Ive added pictures Feb 2 '16 at 20:00
• I'll type up a list of which pin I've connected to what Feb 3 '16 at 7:26
• Switching 2A and E fixed it. Thanks for the help Feb 3 '16 at 8:14

There are a few tests that will help in this sort of situation. Try either of these.

Test 1. Get a logic probe (a simple low-cost device that will serve you for many years!) and use it carefully on the L293D. Test each of the two Enable pins and each of the four Input pins, and make sure each of those pins reads either High or Low as you would expect. This is the most certain way to find a poor connection or incorrect wires.

(Note about the logic probe: always make sure the probe tip is placed centrally and firmly on top of the IC pin you are testing; if you slip sideways and "short" the probe tip between two pins, you could damage an output pin on the Raspberry Pi, depending on the situation.)

Test 2. First disconnect the Raspberry Pi completely and put it aside. Take the wires that go to the motor controller inputs, and physically connect each of these to power, either zero volts or the VCC connection. You are effectively sending the signals by hand instead of by software. If the motor now works as expected, then perhaps your Raspberry Pi has a damaged output pin. If not, either you have misunderstood the connections of there is a problem with the driver.

(As you probably don't have a logic prove, I suggest you at least try test 2 in the first instance. That alone will probably help.)

• I'm not home right now but I have tried different pins on the Pi (and obviously changed my code to reflect that) and I've tried different pins in the same breadboard. Feb 2 '16 at 14:27
• Another possibility is to try just one motor - try it on one side of the controller, checking both directions. Then swap the same motor to the other side and try in both directions there. (To see if there is a problem either with the driver chip, or one of the motors...)
– Andy
Feb 2 '16 at 14:32
• I've added pictures to the question. I'll try what you said in a bit. I've also realised it could be the cables, so I'll mess with those too Feb 2 '16 at 20:00
• Hard to tell exactly what's wrong from the pictures, but I can't see any obvious problems. I recommend rewiring everything to connect up only one half of the driver - to drive just one motor. Then tear it all down and rewire for the other half, again only one motor. In each case check forward/reverse operation...
– Andy
Feb 3 '16 at 7:47
• ALSO with thin wires and breadboards, sometimes current gets limited and only one motor in a pair appears to work properly... so I strongly recommend trying with just one motor, if you haven't tried it yet. (As I mentioned earlier.) This test really is worthwhile...
– Andy
Feb 3 '16 at 7:51