By Stephan Wijman

I found a nice little Christmas tree on ThePiHut which has 25 individually controlled LED's, 24 read ones and 1 white as the star on top. There is both a pre-soldered and a DIY kit. The pre-soldered one uses SMD LED's while the DIY kit uses normal 3mm LED's.

Left the pre-soldered version, right the DIY version

The example script provided (as can be seen below) is a nice starting point. It makes all 25 LED's flicker. This is nice, but I wanted to make it a bit more automated then just that.


from gpiozero import LEDBoard
from gpiozero.tools import random_values
from signal import pause
tree = LEDBoard(*range(2,28),pwm=True)
for led in tree:
 led.source_delay = 0.1
 led.source = random_values()
pause()

I have been playing around with it and had the following idea:
There are 25 days in December until Christmas. This would be nice to then have each day, starting from the 1st of December, to have one LED light up (flickering). Then on the 25th the star will light up.
So I started playing around with that idea and came up with the script below. It will give a glowing effect on the star LED before the 1st of December. On the 1st of December LED nr 1 will light up. Then the following day nr 2, etc. On Christmas day the 25th of December the star will light up (but won't flicker).

The script can also be found at: Wijman GitLab - 3D Christmas Tree


#!/usr/bin/python

import RPi.GPIO as GPIO
import time
from datetime import datetime
from random import randint

# GPIO outputs in order of LED numbers (Star is last)
LEDS = [4,15,13,21,25,8,5,10,16,17,27,26,24,9,12,6,20,19,14,18,11,7,23,22,2]
TOTAL= len(LEDS)

# Variable used if not in December
PULSE = 0
DECREASE = 0
CLEAN = 0

# Set all GIOPs and PWMs
def setup():
    global pwm
    pwm = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    for x in range(0,TOTAL):
      GPIO.setup(LEDS[x], GPIO.OUT)
      pwm[x] = GPIO.PWM(LEDS[x], 50)
      pwm[x].start(0)

# Used in December and limit to 25 after Christmas Day
def dayofdec():
  x = datetime.today().day
  if x >= 26:
    x = 25
  return(x)

setup()

while True:
  # Get month
  MONTH = datetime.today().month
  # If Month is December start build Christmas tree
  if MONTH == 12:
    # Clear star at start of December
    if CLEAN == 0:
      pwm[24].ChangeDutyCycle(0)
      CLEAN = 1

    # Start from 1st day with 1 LED and build up to 25
    for x in range(0,dayofdec()):
      # For first 24 LEDs have full range of PWM
      if x <= 23:
        pwm[x].ChangeDutyCycle(randint(25,100))
      # For the Star no flame effect
      else:
        pwm[x].ChangeDutyCycle(100)

    time.sleep(0.1)
  # If not in December pulsate the Star
  else:
    if CLEAN == 1:
      setup()
      CLEAN = 0

    pwm[24].ChangeDutyCycle(PULSE)
    time.sleep(0.1)
    # Decrease or increase pulse value
    if DECREASE == 0:
      PULSE += 5
    else:
      PULSE -= 5
    # If max value change direction
    if PULSE == 100:
      DECREASE = 1
    # If min value change direction
    if PULSE == 0:
      DECREASE = 0
Example of flashing LEDs

This script does depend on the Raspberry Pi to have its clock set correctly.

Stephan Wijman • 31 Articles

View Articles