Getting Festive with some Addressable LEDs: the Fireplace

Not content with just one hardware hack this Christmas, I thought I should try to create something simpler, prettier and considerably less nerdy than the TfL Tree.

Back at the parents’ house we have a fireplace that I thought could do with a festive makeover. I found this 5m strip of LEDs on Amazon. It was surprisingly difficult to find LEDs that were well-separated: many of them were very close together, and therefore, very expensive. The set I bought had lights separated by about 3cm each, 150 LEDs in total.

The great thing with the WS2812B standard of LEDs (and similar) is that you can cut a strip to the exact length you want and solder them back together if you want. This particular strip had an adhesive back for more permanent applications, but since this was temporary I didn’t cut the strip, and I used Blu Tack to attach the strip around the fireplace. It worked well enough!

IMG_20191225_173353.jpg
I hid the strip badly behind a table and didn’t think about it again.

 

Espruino

Since this project didn’t need to talk to the Internet, I wrote a program using an Espruino Pico microcontroller that I bought after a JSOxford hardware hack day in 2015. The advantage of the Espruino over a Raspberry Pi is that it will start up and run the program instantly. It also runs JavaScript, which is probably more familiar to most people than whatever language you use to program an Arduino.

It was easy enough to write something that looked pretty nice: 20 times a second, the program picked a random LED and set it to a random colour.

Lovely.

I’d also written a few other sequences for the Espruino, and was able to switch between them using its onboard button: another very handy feature that makes the Espruino perfect for this type of project.

Here’s a simplified version of what I wrote:

A more complex example that uses the button to switch between different modes is over here.

While the Espruino was great at updating the colour of a single LED at a time, it wasn’t so great at calculating a new value for all 80 of them a reasonable number of times per second: that required a little too much processing power.

Don’t try this at home

Spoiler alert: by following these instructions you risk destroying the Espruino.

Wiring up those LEDs to the Espruino was simple too.

fireplace pins.jpg

I then plugged the Espruino’s USB plug into a power bank.

Notice how I just wired the +5v and ground wires directly from the LEDs into the Espruino? You should work out how much current your lights are likely to draw, and whether the Espruino can provide it on its own. Because I didn’t.

For whatever reason I thought it would be a good idea to see what it would look like if I put all the LEDs on 100% white. Oddly, the lights on the far side of the fireplace were a lot more yellow than I was expecting. I imagine this was because the Espruino couldn’t provide enough current. Then, in less than a minute, they all cut out. My computer said the Espruino had disconnected even though it was still plugged in, and I came to the fairly quick realisation that I’d fried my Espruino.

If you’re thinking of wiring up your lights in this same way, just be very careful about how much light you’re asking for. Keeping the LEDs dim is fine: 20% is still bright enough for indoor scenarios!

Bring on the Pi

Luckily I had a backup controller I could use to operate these lights: an original Raspberry Pi that I bought back in 2012. It hadn’t been doing much recently so I was glad I could make use of it here. I don’t have JavaScript on the Pi so I tried to rewrite what I had before, but in Python. I then decided to write something completely different instead.

You may remember from my post about TfL Tree that I wrote a library to create LED sequences. My idea was a bit more unpredictable so I didn’t bother using it in the end.

This program sends random colours from one side of the fireplace to the other. The Pi was able to fade out the lights gradually, making the light appear like it had a trail. But it’s easier to watch a video than it is for me to describe it.

With it being a first generation Raspberry Pi, its single-core processor wasn’t very fast and it seemed to struggle to animate the LEDs smoothly. Perhaps there might have been a way to make the maths more efficient, or a way to give the process higher priority, but I didn’t look into that.

Programming the Pi

There are a few ways in which you can set up your Pi to control LEDs. I’m going to share a way that worked for me.

With a terminal up, I created a directory called “led”:

mkdir ~/led
cd ~/led

I then created a new Python virtual environment. Having virtual environments adds a little complexity, but it keeps any dependencies you install apart from any other projects you have on your Pi.

python3 -m venv venv

That creates a folder called ‘venv’. Activate your virtual environment like so:

source venv/bin/activate

While this environment is activated, your default python version will be python3. Hooray!

Now install the library provided by Adafruit to control these LEDs:

pip install adafruit-circuitpython-neopixel

It’s now time to write the script. Using your text editor of choice, make a file called ‘led.py’. This is very similar to what I had running on the fireplace, but I encourage you to get creative and do something completely different!

Wiring up the LEDs is done in exactly the same way I did for TfL Tree (i.e. riskily, still without a separate power source):

tfl pins (no status)

We can now run the program, except this doesn’t work:

python led.py

The neopixel library needs to be run as sudo for some reason, but running ‘sudo python led.py’ will use the system python, not the one in your virtual environment. This will work though. Be sure to use backticks:

sudo `which python` led.py

You’ve made it this far, that’s great! Enjoy this bonus video.

Extra slice: making a program run automatically on the Raspberry Pi

To start a program on a Raspberry Pi, you’d usually need to log in through SSH and type your command. You’d then need to keep that SSH session open otherwise the program will stop. Instead, if you want the program to start when the Pi turns on, without you needing to log in, you’ll need to create a systemd service.

Make a file like this in /etc/systemd/system/ and call it something like ‘led.service’. If you used a different working directory or script name, be sure to change those in this file.

/etc/systemd/system/led.service

[Unit]
Description=led

[Service]
Type=simple
PIDFile=/var/run/led.pid
WorkingDirectory=/home/pi/led
ExecStart=/home/pi/led/venv/bin/python led.py
User=root

[Install]
WantedBy=multi-user.target

That bit in ExecStart should be the returned value after you execute ‘which python’ from inside your virtual environment.

To enable this service so that it runs on startup, this is the last command you’ll need:

sudo systemctl enable led

So now, if you restart your Raspberry Pi, your program will start running! It takes a little while to start, but once it does, it’ll be magical.