Hatherly.com

Blog



Raspberry Pi Controlling Lego Motors Using Scratch

Posted on February 5, 2013 by Adam under RaspberryPi

I recently bought a Raspberry Pi and have been having great fun finding fun things to do with it. One great thing about the Pi is the GPIO port which allows other things to be easily connected to the Raspberry Pi. One great add-on that I have also bought is a PiFace Digital [link]. This fits nicely on top of the Pi and gives you 8 digital inputs and 8 digital outputs (2 of which can drive relays on the board).

I decided to use this to drive some Lego motors, so I could make a simple robot and control it from the Pi.

The first step after plugging the PiFace board into the Pi was to download a customised Raspian image. You can find links to this and other useful documents here. Once I booted the image I was able to switch the LEDs on the PiFace on and off from Python by typing:

$ python
>>> import piface.pfio as pfio
>>> pfio.init()
>>> pfio.LED(1).turn_on()
>>> pfio.LED(1).turn_off()
>>>

I then decided to see if I could control some lego motors, and control them from Scratch. I had two old 9v lego motors with connectors, so I started by connecting the positive wire from a 9v battery to the middle connector on each of the two relays, connected longer wires to the “Normally open” connector on each of the relays, and connected the negative battery wire to the other connector on both motors:

Raspberry Pi and PiFace board with 9v battery connected to relays to control Lego motors

Raspberry Pi and PiFace board with 9v battery connected to relays to control Lego motors

I then opened up Scratch. The next step is to enable a “mesh” connection. To do this, within Scratch Shift-Click the Share menu and click “Host Mesh”.

Now, go back to the desktop and double click the “Scratch Mesh Handler”. This will start listening to the inputs and outputs on the PiFace and relay them to Scratch.

Next, I made a simple “robot” – which was just a simple car with two motors and some gears to slow them down to a more manageable speed.

Lego Robot Controlled by Raspberry Pi

Lego Robot Controlled by Raspberry Pi

Mext, I created a simple script in Scratch to make the robot move forward, turn around and then come back again. First, you need to create some variables for the outputs. Create two variables called piface-output1 and piface-output2, you can then use these to control the two motors. This is the full script:

Scratch Script

Scratch Script

Simple as that!

Here is a video of it in action:



  |  3 Comments


3 Comments:


Matt Wales on February 5, 2013

Excellent stuff. Reminds me of doing parallel and serial port programming on the BBC-B at school. Hope the pi starts a whole new generation of programmers and electronic engineers.

mark on April 27, 2013

Does this require a breadboard too? Also how do the motors connect to the piface connectors?

Adam on April 28, 2013

Hi, no breadboard required. The Piface has simple screw terminals for attaching the wires. The 9v positive battery wire goes to the centre connector for both of the two relays. The negative battery wire goes directly to one of the terminals on each of the two batteries (I used a black lego connector block with one end cut off to attach the wires to). Then you connect the "normally open" connector in one relay to the other terminal on one motor, and repeat for the other relay and motor. Hope that makes sense - the relay is just simply connecting and disconnecting the battery to the motor.




Leave a Comment