jskjj
Posts: 4
Joined: Mon Feb 09, 2015 8:37 am

Motor Direction Control

Mon Feb 09, 2015 8:50 am

I received my first Pi a couple days ago (B+ Model). I ordered it and a few other things to give my kids some education about robotics, my kids are homeschooled so I figure it would be great to do this together. I will be using the Pi with the JBtek 8 Channel DC 5V Relay Module.

I understand how to turn the motors on and off, but I'd also like to be able to control the direction of the motor. It seems like it would take 2 relays and pins for each motor, but I'm not sure how I would wire them up to the relay board without using additional components. I'm pretty sure it can be done if I remember my electronics correctly. I tried to Google it, but I guess I wasn't using the right keywords. I'm needing a diagram and/or video to point me in the right direction. I will be using 2 motors, and wish it to do this so it can perform sharp turns(one wheel moves forward while the other reverses).

While i'm making this post, I might as well ask another question that I have. Naturally, this will be a mobile robot. What is the best way to power the Pi off batteries? Will a straight 6 volts (batteries) do it? I ordered this but it says the voltage needs to be more than 1.5v over what I need, so i'm thinking that it may not work. One guy told me he wired 6v batteries straight up to the Pi and it worked fine, but that seems rather risky to me.

Any help would be appreciated, thanks in advance.

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

Re: Motor Direction Control

Mon Feb 09, 2015 10:28 am

Most motor direction circuits I see tend to use double pole relays, yours seems to use single pole.

However I think it'll work. Connect two relays so their contacts are on 0V and the +V supply (whatever it is). Then for each relay, connect the pole to one of the motor wires.

Here's a circuit I found (not mine) which I think does what I mean (check it very carefully yourself first!):
Image

So to make the motor run, switch one one of the relays. To run it in the other direction, switch that relay off and energise the other.

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

Re: Motor Direction Control

Mon Feb 09, 2015 10:36 am

Oh by the way, I don't recommend the 6V batteries directly to the pi. Some people have tried it and it wiorks, but may be a little risky. The little switched regulator you show should work fine, though as you say 6V may not be enough to make it work well. It may need 7-8V. (You can get 8 AA, 10 AA battery boxes for example, or for more money rechargeable radio control battery packs and a suitable charger.)

Loads of people have had ideas for battery power, so keep asking if you want more ideas.

Have you seem the Magpi magazine? (Free downloadable issues.) They've shown a few robots, they mainly use transistorised motor controllers instead of relays but may give you ideas for design and battery arrangements. You could even run it off a long lead (speaker wire) to start with.

User avatar
aTao
Posts: 1094
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Motor Direction Control

Mon Feb 09, 2015 10:46 am

The relays in the picture at Amazon are single pole change over relays (as per Ravenous's drawing),
See here http://www.smokeandwires.co.nz/relays/1 ... c-10a.html for more info.
If you follow the circuit tracks with reference to the pin out you can determine if and where each of the contacts appear at the screw terminals.
If you cant figure it out,then post a picture of the track side of the relay board here and someone is sure to help.
>)))'><'(((<

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

Re: Motor Direction Control

Mon Feb 09, 2015 1:08 pm

Ravenous's relay & motor diagram will work in almost the exact same fashion as a H-bridge motor driver chip would.

You could follow any tutorials you find that use an L298N or L293D dual H-Bridge and substitute in your relay board.

I would recommend driving the Pi from a separate supply than the motors, there are many USB powerbanks that will be suitable for supplying the Pi.

Looking at the 4th picture on the amazon post we can see that for each relay there are 3 terminals, from left to right these are:
Normally Open (NO), Common(C), Normally Closed (NC).
When the relay is de-energised the Common will be connected to the Normally Closed.
When the relay is energised the Common will be connected to the Normally Open.
Doug.
Building Management Systems Engineer.

User avatar
aTao
Posts: 1094
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Motor Direction Control

Mon Feb 09, 2015 4:56 pm

BMS Doug wrote:
Looking at the 4th picture on the amazon post we can see that for each relay there are 3 terminals, from left to right these are:
Normally Open (NO), Common(C), Normally Closed (NC).
Dohh, I thought it looked like the common was on the right.
>)))'><'(((<

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Motor Direction Control

Tue Feb 10, 2015 3:01 am

BMS Doug wrote: You could follow any tutorials you find that use an L298N or L293D dual H-Bridge and substitute in your relay board.
EXCEPT for the video from TheRaspberryPiGuy on H-bridges. He connects his in a way which has caused Pis to burn out. He connects 4 wires to what he calls the enable pins. Unfortunately 2 of those are 5v pins, connected to 3.3v, non-5v-tolerant, GPIO pins. His videos are normally great, but this is one caveat. If you're aware of it, it is otherwise fine. Just look up some other videos on using H-bridges and see what those 5v pins next to the enable pins are used for. (In short: Use a jumper to connect the two, to keep it in permanent Enable mode until the jumper is removed.)

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

Re: Motor Direction Control

Tue Feb 10, 2015 2:32 pm

Tarcas wrote: EXCEPT for the video from TheRaspberryPiGuy on H-bridges. He connects his in a way which has caused Pis to burn out. He connects 4 wires to what he calls the enable pins. Unfortunately 2 of those are 5v pins, connected to 3.3v, non-5v-tolerant, GPIO pins. His videos are normally great, but this is one caveat. If you're aware of it, it is otherwise fine. Just look up some other videos on using H-bridges and see what those 5v pins next to the enable pins are used for. (In short: Use a jumper to connect the two, to keep it in permanent Enable mode until the jumper is removed.)
He did say that he was going to remove that video. It shouldn't still exist.

But the OP isn't using an H-Bridge, he is planning on using 4 relays, with each pair of relays acting as an H-Bridge. the enable pin's for the relays would work just like the input pins of the H-bridge in the tutorials, both on or both off will mean no movement, one on will result in movement in one direction, the other one on will reverse the direction.
Doug.
Building Management Systems Engineer.

jskjj
Posts: 4
Joined: Mon Feb 09, 2015 8:37 am

Re: Motor Direction Control

Thu Feb 12, 2015 9:54 pm

Thanks for the help so far! I attempted the drawing up above, but didn't work for some reason. It's possible I interpretted the pins incorrectly. I did get it to work using the schematic below. One thing I noticed is that 2 of the LEDs on the relay board light up really dim up until I execute the GPIO program, is it normal for some of the pins to have low power before they are initiated? Other thing I noticed is that the relays are switched to close (LEDs On) after I initialize, but go off when the motors are turned on, so i'm guessing the motors turn on in an open relay state. Would this harm anything? Seems like it would use more power being that the relays are engaged in the motor off state. Nevertheless, the bot is working as it should, I'll be hooking up the Ultrasonic sensor this evening. A bummer is that my power regulator won't be here for a couple weeks, it must be coming from China. Here is the schematic I went with and got to work. Oh yea, though I'll be careful not to let it happen, what would happen if I accidently activated both pins (forward and reverse) on a motor? Just wondering since accidents do happen.

Image

User avatar
aTao
Posts: 1094
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Motor Direction Control

Thu Feb 12, 2015 11:20 pm

If both relays are OFF hen both sides of the motor are connected to +,if both Relays are ON then both sides of the motor are connected to -, in either case the motor will not run and only the relays (in the second case) will draw any power. NO damage can occur with this circuit (if everything is wired correctly and working right)
When the GPIO pins initialise they can be in in any state in, out high, out low, pullup resistor on or off or pull down resistor on or off, what that means for a connected device depends entirely on the device. Although, if I remember. right, the GPIO state should resume as it was when the RPi was switched off
>)))'><'(((<

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

Re: Motor Direction Control

Fri Feb 13, 2015 12:20 am

The schematic that you got to work is functionally identical to the one posted by Ravenous.

I'm glad to hear that you got it working. As aTao says there is no danger to your pi from connecting as shown provided that nothing breaks.

The only reason I added a proviso is because I don't trust cheap relay boards.
Doug.
Building Management Systems Engineer.

Return to “Automation, sensing and robotics”