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:
>>> import piface.pfio as pfio
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:
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.
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:
Simple as that!
Here is a video of it in action: