Build a firefly light

NOTE: you'll also need two strings of battery-powered (3-5V) LED lights (we recommend the IKEA SÄRDAL lights) and a large glass jar to put your lights in (the IKEA ENSIDIG jars) work really well).

Here we'll be making a pretty little firefly light in a jar, with two strings of LED lights that pulse on and off gently. The whole thing is wireless, so it makes a lovely decorative light to sit on a shelf during a party, or out on your patio on a (dry) summer evening.

Firefly light

Soldering the header onto your Pi Zero

The first thing we'll do is to solder the 40 pin male header onto your Pi Zero. The Explorer pHAT with LiPo SHIM attached with its female header will plug onto the Pi Zero's male header later.

Pi Zero and header

Stick the shorter ends of the header pins through the holes on the Pi Zero, from the top side (the side with the micro SD slot and micro USB ports on top), and then use a piece of Blu-Tack to hold it in place and help hold it steady on your desk.

Flip the whole thing over, so that you're now looking at the underside of the Pi Zero. We've got a guide on how to solder Pi Zero pHATs here, so if you're unsure of how to solder then check that out for some tips. Solder each of the 40 pins, beginning with one of the corners and making sure that the header is straight before proceeding. If it's squint after soldering this first pin, then use your soldering iron to carefully re-melt the joint and straighten the header. You're aiming for a nice dome of solder that covers the whole of the copper ring surrounding the hole.

Soldering the extra long header onto your Explorer pHAT

We're using an extra long female header for the Explorer pHAT because it will give us enough extra pin protruding to solder the LiPo SHIM on top.

The female header will be going underneath the Explorer pHAT, so stick the pins on the header through from the bottom side of the board (the side without the big Explorer logo), so that they protrude through the top of the board. Again, a piece of Blu-Tack can help to hold the header in place on the underside of the Explorer pHAT.

Solder all 40 pins, just the same as you did for the male header on the Pi Zero, ensuring that the whole of the copper ring around the hole is covered with solder.

Explorer pHAT soldered

NOTE: don't solder the single row of header to the pins on the bottom edge of Explorer pHAT, as we'll be soldering our LED lights straight into these holes.

Soldering the LiPo SHIM onto your Explorer pHAT

We're going to solder the LiPo SHIM on top of the extra pins protruding through the top of the Explorer pHAT that we just soldered. It's important to note that the LiPo SHIM should be soldered onto the first 8 pins of the header. These will be the leftmost 8 pins if you're holding the Explorer pHAT so that you can read the Explorer text on top of the board.

LiPo SHIM

Use the little rubber foot to space the LiPo SHIM a little way of the Explorer pHAT board. Stick it on the underside of the LiPo SHIM, and then slip the board down over the leftmost 8 pins, with the white JST connector (for the battery) facing upwards.

Solder the 8 pins of the LiPo SHIM, again ensuring that you have sufficient coverage of the pins.

ZLiPo SHIM soldered

Preparing and soldering the LED lights

These instructions will apply to the IKEA SÄRDAL LED lights, but they should be work for similar battery-powered LED light strings.

Using a small pair of snips, cut the two wires of each of the two sets of LED lights as close to the battery box as possible. Using a wire stripper, or carefully using the snips again, trim a good couple of centimetres of the insulation off each of the wires and twist the strands together.

Stripped LED wires

We're going to be using the motor channels of the Explorer pHAT to drive our lights because they provide a convenient way of controlling the brightness of the LEDs, and have the added advantage that we can connect the wires either way round and just drive the motor channels in the appropriate direction.

Taking your first string of LEDs, push each wire through each of the two holes of motor channel one from the top side through to the bottom, and bend the wire protruding through the bottom a little to hold them in place. Now solder each wire, again ensuring that you use sufficient solder. Trim the excess wire from the bottom side using your snips.

Repeat the process, soldering the second string of LEDs to the second motor channel and trim the excess wire again.

Soldered LED wires

Putting it all together

Pieces

Plug your Explorer pHAT/LiPo SHIM board onto your Pi Zero and insert a micro-SD card with the latest version of Raspbian on it.

Don't use your LiPo battery quite yet, as we'll need to install the Explorer pHAT library and write our code to control the light first. Use a micro USB power supply, and boot up you Pi Zero, ensuring that you have either a USB Wi-Fi dongle or ethernet adapter attached for internet access (you'll need that to download and install the Explorer pHAT library).

Open a terminal, and type the following to install the Explorer pHAT library:

curl https://get.pimoroni.com/explorerhat | bash

Follow the prompts that appear, typing y (or n) at the appropriate points.

Now we'll test that the LED lights are soldered correctly and figure out which way to drive each string of lights.

Again in the terminal, type python and enter, and then enter the following code:

import time
import explorerhat as eh

eh.motor.one.forwards()
eh.motor.two.forwards()
time.sleep(5)
eh.motor.one.forwards(0)
eh.motor.two.forwards(0)

Note which, if any of the LED strings light up. If both did, you've lucked out! If either didn't, then make a note of which one/s didn't, and then run the previous lines of code, changing forwards to backwards. You should now see both strings of LEDs lighting up!

Let's write our script to gently pulse the LED strings alternately on and off. We're using the motor channels of Explorer pHAT because they provide a really convenient way to PWM the LEDs on and off (ramp up and down their brightness).

Open a new text file, and copy the code below into it. Save it as something like firefly.py in your home directory (/home/pi/).

import time
import explorerhat as eh

while True:
    for i in range(101) + range(99, -1, -1):
        eh.motor.one.forwards(i)
        eh.motor.two.forwards(100 - i)
        time.sleep(0.01)

Our while True loop will run constantly, and the for loop inside it creates a list of integers from 0 to 100 to 0 which will ramp up and down the brightness of the LEDs. We drive motor one at the current value of i and motor two at 100-i, so that the two motors alternately pulse. The time.sleep(0.01) sets the speed of the pulsing, so you can make that number larger to slow the pulsing or smaller to speed it up.

You'll not want to have to connect a keyboard and mouse up every time you want to set the code running, but there's a quick and easy solution to run the code whenever your Pi Zero reboots.

Cron is a Unix program that is used to schedule jobs, and it has a convenient @reboot function that allows you to run a script whenever your Pi boots.

Open a terminal, and type crontab -e to edit your crontab. Scroll all the way to the bottom of the file, past all of the lines that begin #, and add the following line (assuming your code is at /home/pi/firefly.py):

@reboot python /home/pi/firefly.py &

Close and save your crontab (if you're using nano then press control-x, y and enter to exit and save).

Now, shutdown your Pi, unplug the power supply and plug your LiPo or LiIon battery into the JST connector on the LiPo SHIM.

You should see the activity light on your Pi Zero blinking to indicate that it's booting and, after a few seconds, the LEDs should start pulsing.

Pop the whole lot into your jar and your firefly light is complete!

Taking it further

Why not use a regular power supply (so that you can have it on constantly) and modify the code so that the light switches on and off automatiocally at dusk and dawn?

Or why not make it an internet-connected lights that blinks when you're mentioned on Twitter?

That's all folks!

Search above to find more great tutorials and guides.

Plasma 2040

Swathe everything in rainbows with this all-in-one, USB-C powered controller for WS2812/Neopixel and APA102/Dotstar addressable LED strip.