Raspberry Pi – More fun with Lego Motors!

Posted on May 2, 2016 by Adam under Uncategorized

In a previous blog post I talked about using the Raspberry Pi with the PiFace digital board to control some Lego motors (using the two on-board relays). This worked well, but there was a small problem – the motors could only go in one direction.

In order to make the motors be able to go both forwards and backwards I needed to find a way to reverse the polarity of the 9v current. To do this, we need to use something called a “H bridge”. There are lots of clever ways of building a H bridge, but my electronic skills aren’t up to much, so I decided to go with a solution I found on this page – which is nice and simple as it uses just relays and no other components.

This is nice and simple, but it introduces a new problem… each motor now needs two relays (one for forwards and one for backwards), and our PiFace Digital board only has two relays on it. I wanted to drive two lego motors (one on each wheel of my robot), so I needed to add more relays.

Luckily, simple relay boards can be bought online very cheaply – there are loads available, for example this one from amazon.

In terms of connecting the relay board, I found a really useful forum post here explaining the easiest way of connecting it to the PiFace outputs (see the comment from nickhills at Mon Apr 15, 2013 11:23 pm). This requires a separate 5v power feed for the relays, so I butchered a USB cable to provide me with 5v. To keep the wiring neat I re-used an old floppy drive cable which plugs nicely into the header pins on the relay board, and I then connected the relevant wires into the PiFace screw terminals:

02-05-2016 08-56-21_0004

Now, all I needed to do was to follow the wiring from the relay H-bridge article, and connect the lego motors to the relay screw terminals. I also connected the lego buttons (see previous post) to act as inputs for controlling the robot:

02-05-2016 09-27-49_0001

Now, we can use scratch (making sure you follow the instructions on the PiFace site for setting up and starting the piface scratch handler) to create a simple script to process the inputs (from the Lego buttons) and send the relevant signals to the relays (and on to the motors):


And here it is in action!

Finally, Laura had some fun creating another script in scratch to programme a sequence of actions (a bit like a home-made BigTrak!):


Leave a comment

No comments

Leave a Comment