Wikijunior:Raspberry Pi/Raspberry Pi LEGO® Christmas Tree LED Lights

Tutorial by Andrew Oakley - Public Domain Nov 2017 www.cotswoldjam.org

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

Light emitting diodes (LEDs)


A 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 negative (ground).


 * ×1 red LED
 * ×1 blue LED
 * ×1 yellow LED

Resistors


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 270 Ohm resistors, but anything between 220–470 Ohms will work.


 * ×3 270 Ohm (red violet brown) resistors.

Jumper wires
You will need jumper wires to connect the LEDs to the resistors and then to the Raspberry Pi.


 * ×6 10cm female-to-female jumper wires
 * ×3 20cm female-to-female jumper wires

LEGO® bricks
Finally, you will need two LEGO® bricks to make the Christmas tree.


 * ×1 red plate 8×4 (BrinkLink item: 3035)
 * ×1 small pine tree 2×2×4 (BrinkLink item: 2435)

Connecting the LEDs and resistors
Take two short (10cm) female-to-female wires and place a resistor between them.

Do this three times so you have three long wires with a resistor in the middle.

Next, attach the short resistor wires that you just made, to the negative (short) leg of each LED.

Finally, attach the long (20cm) female-female wires to the positive (long) leg of each LED.

Bend the legs of the LEDs so that you can fit them around the LEGO® fir tree.

Connecting to the Raspberry Pi
Connect the wires with resistors to any of the GND (Ground, Negative) pins. These are shown in black on the diagram to the right.

Next, connect the positive wires – the long wires without resistors – to the following pins. GPIO 14 (pin 8) is on the fourth pin from the top-left of the Raspberry Pi when the Ethernet port is to the right:


 * Red LED – GPIO 14 (pin 8)
 * Blue LED – GPIO 15 (pin 10)
 * Yellow LED – GPIO 18 (pin 12)

Don't worry about the colours of the wires. Concentrate on the colours of the LEDs. GPIO stands for General Purpose Input Output. These pins have many uses.

The program
On your Raspberry Pi desktop, click the Raspberry menu (top right), then select Programming and click on Python 3 (IDLE).

Click File menu, Open. Double-click the  folder (you may need to scroll right to find it). Double-click the lego-xmas folder. Select  and click Open.

A new window will open. In the new window, select Run menu and click Run Module. The red LED should light up briefly, and then turn off. Let's have a look at this program.

What is this program doing?
The computer doesn't know how to do everything itself; it has to learn. These two lines teach the computer about LEDs and waiting. The places where these lessons are kept, are called Libraries. We import the LED command from the  library, and the   command from the   library.

We have to tell the computer which GPIO pin the red LED is attached to, and then we tell it to turn the LED on.

This tells the computer to wait for half a second.

When the program ends, it turns off the LEDs automatically. This auto-off feature is a part of the  library.

Lighting all the LEDs in sequence
Close the program by clicking on the  window, then select File menu, Close.

From the main window, open the  program, then run it with Run menu, Run Module.

This program lights all the LEDs one by one, waiting half a second between each.

Stop the program by holding down the CTRL key and pressing C

What is this program doing?
Notice how we define the pin numbers for the red, blue and yellow LEDs, and we switch them on and off in order.

The  command repeats a section. We say  to mean "forever".

Notice how the commands beneath the  command are indented by a couple of spaces from the left. This is called a block. The indentation lets the computer know which commands it should repeat as part of the  loop.

Also notice that while  ends with a colon.

The colon lets the computer know that it should expect a block next.

Try changing the sequence. Swap some of the  and   commands.

Change the sleep length to longer or shorter. Try adding extra,   and   commands for different LEDs.

Make your own amazing Yuletide light sequence!

Advanced extras
The program  shows you how to use the   command. Notice how  uses the   library rather than just the simple LED library.

The  command is more difficult to use, because you have to time the pulses of your different coloured LEDs so that they match up with your   time!

The program contains some comments which explain how the pulse command works.

Teacher Preparation
Downloads available from http://www.cotswoldjam.org/downloads/2017-11

To add the lego-xmas*.py program files to Raspbian, go to the terminal:

Note that  is the capital letter O, not zero. More tutorials available from http://www.cotswoldjam.org/tutorials

Cjam-lego-xmas.pdf
The original PDF for this tutorial is on Wikicommmons: