User avatar
benderman
Posts: 15
Joined: Wed Dec 17, 2014 8:19 am

Motor control code

Wed Feb 18, 2015 10:38 am

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

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Motor control code

Wed Feb 18, 2015 10:42 am

Your code is okay.

User avatar
benderman
Posts: 15
Joined: Wed Dec 17, 2014 8:19 am

Re: Motor control code

Wed Feb 18, 2015 10:52 am

Thank you.

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

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Motor control code

Wed Feb 18, 2015 11:05 am

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.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Motor control code

Wed Feb 18, 2015 11:07 am

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.
Doug.
Building Management Systems Engineer.

danjperron
Posts: 3405
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Motor control code

Wed Feb 18, 2015 1:11 pm

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.

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: Motor control code

Thu Feb 19, 2015 11:08 am

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: )

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Motor control code

Thu Feb 19, 2015 12:03 pm

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.

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: Motor control code

Thu Feb 19, 2015 12:15 pm

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.)

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Motor control code

Thu Feb 19, 2015 1:01 pm

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.
Doug.
Building Management Systems Engineer.

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: Motor control code

Thu Feb 19, 2015 1:37 pm

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.

Return to “Automation, sensing and robotics”