Wikijunior:Raspberry Pi/Raspberry Pi LEGO® Traffic Lights

Tutorial by Andrew Oakley Public Domain 26 Sept 2015 www.cotswoldjam.org

LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this tutorial nor Cotswold Raspberry Jam.

Introduction
This tutorial shows you how to build a set of traffic lights based around LEDs (lights) and a Raspberry Pi computer. The lights can be placed inside a LEGO® Technic brick and used with other LEGO® models.

Conventions
At the end of each line, you will usually have to press the Enter key.

Your tutor should have prepared your Raspberry Pi for you. If not, see the section "Preparation" at the end.

Components
The light-emitting diode (LED) has a short leg and a long leg. If you feel around the rim, you'll also find a flat edge. The short leg and flat edge always connect to the negative (ground). We use 5mm LEDs because they fit into LEGO® Technic holes.

The resistor can be connected any way around. Lower value (lower Ohm) resistors will allow more current through and will make the LED brighter; higher values will let less current through and make it dimmer. We're using a 270 Ohm resistor but anything between 220–470 Ohms will work fine. We will use one resistor connected to the ground to cover all 3 LEDs.

These three LEGO® bricks can be made into a traffic light, and the 5mm LEDs fit inside the holes of the Technic brick.



The part numbers for these bricks – which can be purchased second-hand on sites such as eBay or Bricklink.com – are:


 * 3701 – Technic, Brick, 1×4 with holes, black
 * 3460 – Plate, 1×8, grey
 * 3022 – Plate, 2×2, grey

Push the LEDs into the Technic brick so that the short legs of each LED are at the bottom.

You will need to connect the short legs to a single resistor and then to one of the Raspberry Pi’s ground pins. Your tutor may have made up a special "pigtail" cable to do this, or you may use a breadboard.

Ground (negative) wires
Pigtail cable Your tutor may have made up this cable using a DuPont crimper





It has three female ends on one side, which plug into the short legs of the LEDs, a resistor in the middle, and a single female end on the other side which plugs into pin 9 (ground) of the Raspberry Pi. A short bit of clear sticky tape wraps around the resistor to hold it in place.

Breadboard

Signal (positive) wires
Once you’ve got the resistor and ground connected to the LEDs’ short legs, you can test any LED by using a female-to-female jumper wire to connect the long leg to pin 1 (live 3.3 Volts) on the Raspberry Pi (assuming the Raspberry Pi is powered on).

However, we don’t want the lights on all the time; we need them to be controlled by a program. Disconnect pin 1 once you’re finished testing the lights.

To control the LEDs from our program, use female-to-female jumper wires to connect the long (positive) legs of the LEDs to the following pins on the Raspberry Pi:


 * Pin 11 — Red
 * Pin 13 — Yellow
 * Pin 15 — Green

The program
Power up your Raspberry Pi, log in and go to the desktop. If you need to log in, the default username is  and the password is. If the desktop still does not appear, type  and press the Enter key.

From the menu, select Programming – Python 3. Then use File, New Window to create a new program.

Type in the following program, or alternatively you can use File, Open to open the traffic1.py program in the python/traffic folder.

Use File, Save to save this program as. Here's what this program is doing:

This command teaches the Raspberry Pi about its GPIO pins, so it can turn the LEDs on and off, and about time, so we can tell it to wait for a few seconds between each colour.

Here we’re telling the computer to use call the pins by the numbers in the order they are laid out on the board (there is an alternative way of numbering them called BCM). Next we tell it not to warn us if the GPIO pins have already been used.

Now we're telling the Raspberry Pi which pins are used for which coloured LEDs. Note that we don’t have to state that we're using pin 9 for ground; it doesn’t matter.

Finally we tell the Raspberry Pi to prepare the red, yellow and green pins for output. If we were getting input, such as reading a switch, then this bit would look different.

This part of the program just gets everything ready. It doesn’t actually turn any of the LEDs on. We'll do that next.

Use File, Save As to save the program as. Don't worry it it tells you that the program already exists – save it anyway.

Lighting up one LED
Let's start by lighting up the yellow LED. At the bottom of the program, add these lines:

HIGH turns a light on. LOW turns it off.

Save the program as.

Now leave the Python window open, and start a terminal session by going to the main desktop Menu - Accessories - Terminal. You should see a black window open.

In the terminal, type:

You should see the yellow LED light up for two seconds, and then the LED will turn off and the program will end. You can repeat the program by entering  again.

Try changing the program to light up the red or green LED.

Flashing an LED on and off repeatedly
Now let’s change the program so that the last lines read:

Save this program as. Go back to the black terminal window and run it with:

The yellow LED should flash on and off every 2 seconds.

Exit the program by holding down the CTRL key and tapping the C key. This may leave the yellow light on! Don’t worry about that.

The full sequence
The full sequence for British traffic lights is:


 * Red (stop) — for a long time
 * Red and Yellow together (get ready to go) — for a short time
 * Green (go) — for a long time
 * Yellow on its own (get ready to stop) — for a short time

Let's change our program to do this full sequence:

That's it! Save this program as. Go back to the terminal window and run it with:

Exit the program by holding down the CTRL key and tapping the C key.

You can use the alloff.py program to turn all the lights off after you've stopped the program. Also there is the  program which uses some advanced techniques to turn off the lights when the program is stopped.

Further adventures
Here’s some ideas for other things you could do:

Run two sets of lights from one Raspberry Pi
You could use pins 12, 16 and 18 (plus pin 14 for ground) to control another set of lights.

For a T-junction you need 3 sets of lights. For a crossroads, 4 sets
On a Model B+ or Pi 2 you could use pins 36, 38 and 40 (plus pin 34 for ground) to control a third set of lights, or you could use two Raspberry Pis.

If you had more than one Raspberry Pi, how could you synchronise the lights? How could two Raspberry Pis communicate between each other?

How could you detect a toy car waiting at the lights?
Does your strategy work if it is a plastic LEGO® car or a metal toy car, or both? Does it matter if the car is heavy or light?

What about if it was a bicycle or a horse? How do traffic lights in the real world detect cycles and horses – do they have another strategy, or do they ignore that problem?

How could you run your program automatically when you turn on your Raspberry Pi?
Try searching for information on the  command: https://www.adminschoice.com/crontab-quick-reference

Preparation
The files for this tutorial can be found at: http://www.cotswoldjam.org/downloads/2015-09/

Please create a  folder under the home/pi directory, then unzip the files into that directory. This can be done in the LXTerminal:

Files
The original PDF for this tutorial is on Wikimedia Commons: