Page 1 of 1

Motor control code

Posted: Wed Feb 18, 2015 10:38 am
by benderman
Hi Guys,

I wonder if you guys could review my code? Both motors run OK forward but only motor2 runs backwards. I'm using an L293D to run two motors and I have checked all the connections and I think the code is OK but was wondering if there are any obvious errors?

Code: Select all

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD)

Motor1A = 16
Motor1B = 18
Motor1E = 22

Motor2A = 23
Motor2B = 21
Motor2E = 19

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 "Going forwards"


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)

sleep(2)

print "Going backwards"


GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor1E,GPIO.HIGH)

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


sleep(2)

print "Stopping motor"
GPIO.output(Motor1E,GPIO.LOW)
GPIO.output(Motor2E,GPIO.LOW)

GPIO.cleanup()
Cheers,

Ben

Re: Motor control code

Posted: Wed Feb 18, 2015 10:42 am
by joan
Your code is okay.

Re: Motor control code

Posted: Wed Feb 18, 2015 10:52 am
by benderman
Thank you.

So the connections must be OK if motor2 runs forwards? Can part of an L293 fail or is it all or nothing?

Re: Motor control code

Posted: Wed Feb 18, 2015 11:05 am
by joan
benderman wrote:Thank you.

So the connections must be OK if motor2 runs forwards? Can part of an L293 fail or is it all or nothing?
That's not necessarily true. I suspect your connections. Close-up photo(s) showing the L293D (I hope) and the Pi connections would be useful.

If you are using a L293 I hope you have added diode protection. If not that could fry your Pi.

Re: Motor control code

Posted: Wed Feb 18, 2015 11:07 am
by BMS Doug
benderman wrote:Thank you.

So the connections must be OK if motor2 runs forwards?
No.

the chances are there is an issue with your Motor 2B connection (Motor2B = 21) as it seems that you are receiving a high on pin 23 (motor runs forwards) but not on pin 21 (motor won't run backwards).

alternatively you may be accidently sending a permanent high signal to Motor 2A connection (Motor2A = 23) which would also cause the problem you describe.
Can part of an L293 fail or is it all or nothing?
It's possible but a wiring error is more likely.

Re: Motor control code

Posted: Wed Feb 18, 2015 1:11 pm
by danjperron
The simplest thing will be to use a multimeter and toggle each output one by one and check the voltage on each of them.


This is no more than 2 minutes.

A multimeter is an handy tool to have anyway.

Re: Motor control code

Posted: Thu Feb 19, 2015 11:08 am
by Ravenous
Possibly stupid question as my head is a little fragile today - does sleep(2) mean 2 seconds, or 2 milliseconds?

(If so, that's the problem - if not, ignore me and I'll go back to sleep :lol: )

Re: Motor control code

Posted: Thu Feb 19, 2015 12:03 pm
by joan
Ravenous wrote:Possibly stupid question as my head is a little fragile today - does sleep(2) mean 2 seconds, or 2 milliseconds?

(If so, that's the problem - if not, ignore me and I'll go back to sleep :lol: )
It's an import from the Python time module. The units are seconds, may be fractional, e.g. sleep(0.01) for a hundredth of a second.

Re: Motor control code

Posted: Thu Feb 19, 2015 12:15 pm
by Ravenous
D'oh, yes 2 seconds then. :oops:

For the OP, another way to test the logic levels at the inputs, maybe wire an LED and resistor (500 ohms-1K?) and connect that between each input and ground. Would this work, or would it just pull down the inputs? (I use a cheapo Logic Probe for this, but that's no use to the OP as he's unlikely to already have one.)

Re: Motor control code

Posted: Thu Feb 19, 2015 1:01 pm
by BMS Doug
Ravenous wrote:D'oh, yes 2 seconds then. :oops:

For the OP, another way to test the logic levels at the inputs, maybe wire an LED and resistor (500 ohms-1K?) and connect that between each input and ground. Would this work, or would it just pull down the inputs? (I use a cheapo Logic Probe for this, but that's no use to the OP as he's unlikely to already have one.)
I had exactly this setup when I was testing my programs to control my motor project, 4 LEDs (2 red, two green) that I connected to the relevant GPIO pins to check the enable signals.

Re: Motor control code

Posted: Thu Feb 19, 2015 1:37 pm
by Ravenous
Thanks, good to know the simple resistors work without affecting things.

Another thing I forgot to suggest (very forgetful today) is you can always test the motor + driver separate from the raspi - disconnect the raspi completely and use wires to put +V and 0V to each input as necessary.