Pi 400 Xmas Tree

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.

tree on 400 front

Solder the Tree

  1. If the PCB was supplied with the connector in place, note its orientation and remove it.
  2. Look at the photo of the completed tree below and start by soldering the resistors in place (it doesn’t matter which way round they go). Trim the wires.
  3. Solder the LEDs. IMPORTANT: the long lead must go in the hole marked with a “+” symbol (the hole nearest the top of the tree). Trim the LED leads.
  4. Solder the connector in place (it will only fit one way).
  5. Check all your soldering to ensure that there are no “shorts” e.g. large blobs of solder joining the two LED leads.

Connect the tree to your Pi 400

The tree should be connected to the Pi 400’s GPIO connector as shown below. The tree should be pushed as far towards the pin 40 end as possible (i.e. towards the left in the picture below).

tree on 400 rearTAKE CARE when removing the tree not to bend the Pi’s GPIO pins: slightly wiggling the tree whilst applying backwards pressure should gradually release the tree.

Programming 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.

LED GPIO number

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.


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 top and bottom LEDs
  tree.value = (1, 0, 1, 0, 1)
  # illuminate middle LEDs
  tree.value = (0, 1, 0, 1, 0)

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)
tree.value = (1, 1, 1, 1, 1)
tree.value = (0, 0, 0, 0, 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:





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
top.value = 0.66 # 66% brightness
top.value = 1.00 # full brightness
top.value = 0 # off

Circuit diagram

tree schematic small PNG