GPIO Xmas Tree 2019

This is a simple festive add-on for your Raspberry Pi with 5 LEDs that can be controlled easily using a Scratch or Python program. If you would like to buy one, please click here.

cover2

The tree should be connected to the Raspberry Pi’s 40-pin GPIO connector as shown below. Ensure that the LEDs are facing outwards.

how connect to gpioProgramming the tree

Now for the fun part: writing a program in Scratch of Python to make the LEDs illuminate in your chosen sequence. To turn an LED on or off, you need to know its GPIO number. These are marked on the tree itself and on the picture below. To turn an LED on you will need to set its GPIO output to be HIGH. To turn the LED off, make the output be LOW.

GPIO connections

Programming the tree in Scratch

These instructions are for Scratch 3 but the process is very similar for Scratch 2. You can easily write a program to control the LEDs using Scratch:

  • Open Scratch 3.
  • Click on the blue Add an Extension icon.
  • Find Raspberry Pi GPIO and click on it.
  • Drag blocks to construct either of the sample programs shown below and then click the green flag.

Scratch example 1: flash the top LED

In this short program, the top LED is turned on by using set gpio 6 to output high and then it is turned off by the set gpio 6 to output low block.

blink top
Scratch example 2 : waterfall

With this program, the LEDs cascade down from the top of the tree to give a waterall effect. First the top LED lights (6) then the middle LEDs (13 and 19) and then the bottom LEDs (26 and 5). Finally, all the LEDs are turned off and the sequence repeats forever.

cascade

Scratch: over to you!

Now it’s your time to write your own Scratch program to make interesting effects with the LEDs. To turn on LED 6 (the top LED) use this block:

led on

To turn it off again, use the same block but change the high to low:

led off

For LEDs other than the top LED, you will need to change the 6 to one of 26, 13, 19 or 5 depending on which LED you wish to illuminate

Programming the tree in Python

In order to control the LEDs from Python we are going to use the excellent GPIO Zero library. Whilst you can treat LED separately, it is convenient to group them together into a ‘board’ of 5 LEDs. The following example does this:

from gpiozero import LEDBoard
from time import sleep

tree = LEDBoard(26, 13, 6, 19, 5)

while True:
  # illuminate on top TOP and bottom LEDs
  tree.value = (1, 0, 1, 0, 1)
  sleep(0.5)
  # illuminate middle LEDs
  tree.value = (0, 1, 0, 1, 0)
  sleep(0.5)

In this code, first we set-up the board by telling it which five GPIO pins to use as outputs. For convenience, these have been listed in left-to-right order as you look at the tree:

tree = LEDBoard(26, 13, 6, 19, 5)

We can then turn on each LED individually. For example, this code will turn on LED 26:

tree.value = (1, 0, 0, 0, 0)

and this will turn on LED 13:

tree.value = (0, 1, 0, 0, 0)

You can turn on all LEDs at once:

tree.value = (1, 1, 1, 1, 1)

or turn them all off:

tree.value = (0, 0, 0, 0, 0)

Python example 2: varying the brightness of the LEDs

When setting up the LEDBoard it is possible to add pwm=True which will enable us to set the brightness of each LED from 0 (off) to 1 (full brightness). Here is an example:

# Uses the PWM function of LEDBoard to
# illuminate the LEDs at 30% brightness
# then full brightness and then turn them
# off.

from gpiozero import LEDBoard
from time import sleep

tree = LEDBoard(26, 13, 6, 19, 5, pwm=True)

while True:
tree.value = (0.3, 0.3, 0.3, 0.3, 0.3)
sleep(1.0)
tree.value = (1, 1, 1, 1, 1)
sleep(1.0)
tree.value = (0, 0, 0, 0, 0)
sleep(1.0)

Python example 3: controlling each LED separately

Rather than using LEDBoard you can also treat LED separately. The following example does this.

from gpiozero import LED
from time import sleep

bottom_left = LED(26)
bottom_right = LED(5)
middle_left = LED(13)
middle_right = LED(19)
top = LED(6)

while True:
bottom_left.on()
sleep(0.5)
bottom_left.off()

middle_left.on()
sleep(0.5)
middle_left.off()

top.on()
sleep(0.5)
top.off()

middle_right.on()
sleep(0.5)
middle_right.off()

bottom_right.on()
sleep(0.5)
bottom_right.off()

Python example 4: adjusting the brightness of a single LED

If you are controlling each LED individually (rather than using LEDBoard) then you can use PWMLED which will allow you to adjust the LEDs brightness:

# Uses PWMLED to vary the brightness
# of the LED at the top of the tree.

from gpiozero import PWMLED
from time import sleep

top = PWMLED(6)# use the topmost LED

while True:
top.value = 0.33 # 33% brightness
sleep(1)
top.value = 0.66 # 66% brightness
sleep(1)
top.value = 1.00 # full brightness
sleep(1)
top.value = 0 # off
sleep(1)

Circuit diagram

tree schematic small PNG