Christmas is a great time of year, right? Forget presents and jingly music though, the excitement I get is from pound stores stocking hackable LED strings that have so many potential uses.

You can use them just as battery operated lights, add a solar panel to have rechargeable lights, add a USB socket and have them as a TV surround light— or connect them to a Raspberry Pi! They come in loads of colours and can be used under counters, over units, on the balcony, behind the TV or hanging down; just anywhere you want light. They don’t have to be solely a Christmas thing too, they can decorate plants and look stylish all year round.

This article is about how I created Raspberry Pi controlled Christmas lights for just £1 (Raspberry Pi not included!) and how you can too with just a Pi and a pound.

The end result

The Initial Cause

In my building, most people have a small soft toy or other nice thing outside their door. I have a fox outside mine. As well as some understated balcony lights, I wanted to do something on the corridor side, and thought why not add some lights to my fox. I looked at battery operated lights for safety reasons, but they have problems:

  • require turning on and off
  • will run out of battery (especially when you forget to turn them off!)
  • can be a sign of when you are in and when you are out

I wanted lights that would not run out of battery, and would come on and off when I set, so I would come home to them on, guests would see them on, and they would turn off when everyone is asleep.

I could just get mains powered lights with a timeswitch, but my version is much more fun…

Building the project

The main parts of this project are:

  • The LED light string itself
  • How it connects to the Raspberry Pi
  • How it comes on and off automatically

Get me light

Given I wanted the lights to be controlled by the Raspberry Pi, I looked into what sort of voltage the Pi could provide on it’s software controlled pins, and the answer is 3.3v. If I want to switch higher voltage, I can use a transistor with not much effort. But I’m super lazy, so a 3. 3v LED string will be ideal.

Time to spend that pound!

When shopping, the first thing I looked at was the battery pack — LED strings typically take between two and three AA batteries, and given AA batteries provide 1.5v, the options are a 3v LED string or a 4.5v LED string. Given that the Raspberry Pi can provide 3.3v as an output, a light string that takes two AA batteries and needs 3v to run is close enough to 3.3v that the Pi provides. So with that I bought some quirky purple ones, got home and turned the soldering iron on.

Give me power

The next step is to power the lights. I want it controlled by the Raspberry Pi, and for that I’m using it’s GPIO (General Purpose Input and Output) — these are pins on the board that can both supply power (output) and listen for voltages (input). I’ll be using it to supply power to the lights.

(Source: http://www.megaleecher.net/)

Using a pinout of the GPIO socket, which explains what each pin is connected to, I chose a GPIO pin to connect my lights to. I’m looking at the green GPIO pins; and for this project I chose pin 18 as that also has PWM( Pulse Width Modulation)which effectively gives me scope to “fade” the LEDs if I want to later on(it could be a nice animation for them to fade up and down).

Connecting to the GPIO pins can be done in a few ways, from literally soldering a single socket to a wire and inserting on to the pin, or by using a breakout board, something I already had, connected to a breadboard (something I’m using in a separate Pi project that controls my lights).

My breakout board, connected to a circuit board already that is another project!

Now I knew how I’d be controlling the lights, I had the physical distance of the between my Pi and the lights outside my front door to overcome.

I had a couple of options:

  • cut off the battery pack and solder a new wire going to the Pi
  • add a USB plug on the lights, use a USB extension cable I already happened to have to connect it to a socket on the breadboard.

Given I didn’t have enough length of wire, but did have a long USB extender cable, I chose to use that, adding a USB plug on the lights, and a socket on the Pi, both recovered from other projects.

Note the USB socket secured to the ethernet cable, with cables going to the ground and GPIO 18. I also added a LED to show if the lights were on, by just conencting to the same GPIO pin.

I then wrote a simple Python script that turns GPIO pin 18 on and saved this as a script called LEDon.py:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.HIGH)

I saved this as a script and noted it’s absolute file location (/home/pi/christmasLED/LEDon.py).

I did exactly the same for the off script, but setting LOW instead of HIGH:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)

For reference, I used this tutorial here: https://thepihut.com/blogs/raspberry-pi-tutorials/27968772-turning-on-an-led-with-your-raspberry-pis-gpio-pins

I then connected pin 18 to the positive USB wire, and ground to negative, using a USB pinout, and connected everything together. I then ran the LEDon script, and the lights came on! Success! (NOTE: I’m using 3.3v supply from the Pi on a USB cable which normally supplies 5v — so this isn’t something that can be used for other USB products. If I wanted to do that, I could add a resistor to the lights and use a transistor to switch the current).

python LEDon.py

I also chose to add in a red LED on the breadboard to show if the outside lights were on.

Give me timings

To make the lights turn on and off automatically, I chose to use the cron feature that can be found on Linux. My Raspberry Pi is running Raspian, and so using cron was a simple choice and required no installation.

Simply put, cron is a task that runs every minute. You can edit and view it by running the command below:

crontab -e

crontab -e

This file contains lines, each one consists of a rule and a command. Cron runs through each line every minute, and checks if the rules match the current time. If they do then the command is run. The rules take the form below, each part separated by a space.

mm hh DoM MoY DoW /path/to/command

In order:

  • mm = Minute (0–59)
  • hh = Hour (0–23)
  • DoM = Day Of Month (1–31)
  • Month Of Year (1–12)
  • Day Of Week (0–7, where 7=0)
  • /path/to/command — this and anything onwards is executed if the current time matches the rules beforehand.

Values are mentioned above, but can also use the asterisk (*) which means “all”, and will match any number. Example cron rules:

0 6 * * * python /run/this/python/script/at/six/am/every/day.py
* * * 12 * python /run/every/minute/in/december.py
0 0 * 12 * python /run/at/midnight/every/day/in/december.py
15 8 * * 0 python /run/once/a/week/at/eight/fifteen/on/sunday.py

For this project, I wanted the lights to come on at 10am, and turn off at midnight — so I used these rules.

0 10 * * * python /home/pi/christmasLED/LEDon.py
0  0 * * * python /home/pi/christmasLED/LEDoff.py

I then added an extra cron rule for 1 minute ahead of the current time, saved and waited by my lights… a minute later they turned on! Success! I had verified cron was working, so I deleted the test rule.

(Note: I could have added December as the month in the cron rules so the lights only illuminated in December, but as this was temporary I chose to not do that and just remove the cron lines when Christmas was over and done with.)

Summary

I now have a LED string light outside my door, connected via a USB cable extender to my Raspberry Pi’s GPIO pin 18. Every minute, the cron script is run on the Pi, and when either of the two rules match, the respective on or off script is run. The on script sets GPIO pin 18 to HIGH — this means that pin starts providing 3.3v. The off script sets pin 18 to LOW, stopping the voltage.

I now have a small Christmas decoration that I don’t have to think about changing batteries, running out of power, turning on and off. It’s a wonderfully over-engineered solution that kept me out of trouble for another evening.

 
Categories: Uncategorized