submit to reddit       

Pi Face

GPIO pins can only supply a limited amount of current, so it's often necessary to use some interfacing circuitry to boost the current if you want to power things like motors and lights. The Pi Face board is designed to make interfacing easy.

Before you can use a Pi Face, you need to enable your Raspberry Pi's SPI driver. You can do this by opening /etc/modprobe.d/raspi-blacklist.conf, and insert a '#' at the start of the line that says 'blacklist spibcm2708'. Save the file (you'll need to open it as root in order to be able to save it).

Next, you need to install the Pi Face libraries with these commands:

$ sudo apt-get update
$ wget http://pi.cs.man.ac.uk/download/install.txt
$ chmod +x install.txt
$ ./install.txt

When you've installed the software, shutdown your Pi and disconnect it from the power supply. Carefully attach the Pi Face board making sure to line up the pins correctly correctly before pushing them into th Pi Face socket. Then boot your Pi and run the Pi Face emulator with this command:

$ piface/scripts/piface-emulator

You can still use the wiringPi GPIO utility but if you use it with a Pi Face, you need to use the -p option. Note that when an output is enabled, an LED will light up.


Building a remote control vehicle using a Raspberry Pi

Motor circuit diagram Battery Pack
Battery Pack and Cable Raspberry Pi, Pi Face and Tank
The assembled tank Pi Face powered tank

I've built a simple remote controlled tank to illustrate some of the points that I've made in previous posts. I'm minimizing the amount of electronics I need to do by using a Pi Face board which can easily power two motors. To control the tank, I made a simple web interface which I can access from my laptop. The Pi connects to my home network using a USB wifi dongle.

The tank I'm using a very simple. It's just a chasis, a couple of motors and a battery pack.

If you want to build something like this, you'll need to install Apache, set up your wifi connection and install wiringPi. You'll also need to set up the Pi Face software.

Battery powered Raspberry Pi

The tank's battery pack can hold six AA baterries which should be enough to power the motors and the Raspberry Pi. I'm going to connect the batteries to the 5V power socket on the Pi Facewhich is not regulated, so it's important to supply the correct voltage. The motors are powered from the same batteries, but they need a 6V supply. I need a split power supply. I achieved this by connecting a jumper cable between two of the batteries in the battery pack, and using the other end of the jumper cable as the 5V output. The output doesn't have to be exactly 5V - my Pi is actually getting 5.37V. This is a little outside of tolerance, but it should be ok.

If you try this at home, I recommend that you use a multimeter to test the voltage on the cable you've just added. Different brands of batteries produce slightly different voltages, so instead of adding a connector between the 4th and 5th batteries as I did, you might want to add the cable in a different place in order to get a 5V output.

Controlling motors with a Pi Face board

I connected the motors to the Pi Face as shown in the circuit diagram on the right. I used a breadboard to connect some of the power cables to each other. The motor's cables were too thick to fit in the holes on the breadboard, so I used a connector block to link them to a jumper cable.

One lead from each motor needs to be connect to the 6V supply, and the other lead from each motor needs to be connected to the middle hole in the Pi Face relay headers. The NO (Normally Open) holes in the relay headers need to be connected to ground.

Building a web UI

The UI is pretty simple. It consists of a single web page with four icons on it. The icons are buttons which turn on one of the motors to turn left or right, both motors to go forwards, and turn both motors off in order to stop. When the icons are clicked, some Javascript code calls the CGI scripts that control the motors.

The buttons look like this:

Raspberry Pi GPIO

The HTML code for this part of the page is as follows:

  <div style="text-align:center">   <h1>Raspberry Pi GPIO</h1>     <img src="/forward.jpg" id="f" onmousedown="set01()" onmouseup="clear01()">   <br>   <img src="/left.jpg" id="l" onmousedown="set0()" onmouseup="clear01()">   <img src="/right.jpg" id="r" onmousedown="set1()" onmouseup="clear01()">   <br>   <img src="/stop.jpg" id="s" onmousedown="clear01()" onmouseup="clear01()">   </div>

Each image tag has an id associated with it, and specifies which Javascript function should handle the mouse up/mouse down events for that image. When a user clicks on an icon, the corresponding Javascript function will execute in their browser. This function invokes a CGI script running on the server that sets GPIO pins. When the user lets go of the mouse button, the browser detects the mouse up event and calls clear01(). The clear01() function invokes a script on the server that clears the GPIO pins. These Javascript functions are in the head section of the web UI:

<script Language="Javascript"> function set0() {     document.location="cgi-bin/set0.cgi"; } function set1() {     document.location="cgi-bin/set1.cgi"; } function set01() {     document.location="cgi-bin/set01.cgi"; } function clear01(event) {     document.location="cgi-bin/clear01.cgi"; } </script>

The code for the complete page is as follows:

<html> <head> <script Language="Javascript"> function set0() { document.location="cgi-bin/set0.cgi"; } function set1() { document.location="cgi-bin/set1.cgi"; } function set01() { document.location="cgi-bin/set01.cgi"; } function clear01(event) { document.location="cgi-bin/clear01.cgi"; } </script> </head> <body> <div style="text-align:center"> <h1>Raspberry Pi GPIO</h1> <img src="/forward.jpg" id="f" onmousedown="set01()" onmouseup="clear01(event)"> <br> <img src="/left.jpg" id="l" onmousedown="set0()" onmouseup="clear01(event)"> <img src="/right.jpg" id="r" onmousedown="set1()" onmouseup="clear01(event)"> <br> <img src="/stop.jpg" id="s" onmousedown="clear01(event)" onmouseup="clear01(event)"> </div> </body> </html>

Save this as /var/www/index.html (you may need to delete the existing index.html if you have only just installed the server and the default index page is still there).

Controlling the Pi Face with bash scripts

I originally wrote the CGI scripts in Python using the Piface module, but performance wasn't very good. There was a long delay between clicking on buttons and the relays being activated. I re-wrote the scripts in the bash shell scripting language using the wiringPi gpio utility to control the motors. Now when I click on the buttons on the Pi's web page, the relays are activated pretty much instantly.

The following script, set1.cgi, controls one of the motors:


gpio -p write 201 1

echo "Status: 204 No Content"
echo "Content-type: text/plain"
echo ""

When this script runs, the first line shows that the script needs to be executed by bash. The second line uses the gpio utility to turn on the second relay. Note that the -p option needs to be used when you're working with Pi Face. In set0.cgi, the equivalent line is

gpio -p write 200 1

and in set01.cgi, the equivalent lines are

gpio -p write 200 1
gpio -p write 201 1

The next two lines are the HTTP header which will be printed to a socket and sent to the browser. The line that sets the status tells the browser that this script isn't going to produce any content, so the browser should stay on the current page. If this line wasn't used, the browser would simply show a blank screen when a user clicked on a button. The next lines complete the header.

When the user lets go of the mouse button, the clear() function invokes clear01.cgi. This script contains the code that turns off both relays:

gpio -p write 200 0
gpio -p write 201 0

If you go to your Pi's IP address in your browser, you should see the web UI.


Follow me