Cheerlights for Raspberry Pi

I decided to do Christmas a little more ‘whole-heartedly’ this year and, having found some left-over RGB LED strip in the workshop (cut from a set that was used in a recent observatory build) I decided to hook them up to the Internet for the amusement of my kids.  It ended up in the front window and every time the colour changed, there were whoops of excitement!

Cheerlights - The Window

CheerLights is an “Internet of Things” project created by Hans Scharler that allows people’s lights all across the world to synchronise to one colour set by Twitter.  It generally uses the “ThingSpeak” platform, which uses an API to listen to Twitter and updates a “ThingSpeak Channel” with a colour. Devices connected to ThingSpeak can listen for this and of course are programmed to change colour when a new tweet is posted.

I, however, didn’t know any of this when I started the project, and only knew that I wanted to be able to change the colour of my LEDs remotely.  I also knew that it was fairly easy to query Twitter and thus figured that I could tweet to my own lights.  Only upon completing this project (Let’s call that “neilslights”) did I discover that 10,000 other lights (allegedly) were already doing the same thing via Cheerlights.  It was easy for me to ‘conform’ – merely changing my own keyword (neilslights) to “cheerlights” and the job was done.

So, to be clear, my project is only “Cheerlights” in the sense that it looks for posts with that tag on Twitter.  It doesn’t use Thingspeak – just a Raspberry Pi, connected to the internet.

Hardware Requirements

  • A suitable “LED Driver” (i.e. high quality 12V power supply – more later)
  • A Raspberry Pi (I used a model A+)
  • Some components:
    • 3x N-Type MOSFET transistors, with particular characteristics
    • Some form of prototyping board (I used a Ciseco “Humble Pi”)
    • Some 4-core cable (e.g. telephone wire)
    • A left-over 12V strip of RGB LEDs (mine was cut from this product)
    • Components for a 5V voltage regulator (optional, see below)
  • Soldering iron & associated equipment
  • Network connection (I used a little WiFi dongle)

Prerequisites:

  • “NOOBS” or some form of Raspbian running on the Pi
  • WiFi up and running and connected to a network (or a connected Ethernet cable if available on your Raspberry Pi)
  • SSH access to the Pi, or a directly connected console (keyboard, monitor and mouse)

Steps:

1. Build the circuit

You will need to decide whether you wish to run the whole project from the 12V LED Driver (more difficult) or just the LEDs, in which case you would power the Pi separately, e.g. via a USB power adapter. Either is possible, but consider:

  • It is neater to power the whole thing from one power source
  • If you power the Pi via GPIO then it is easier to blow up the Pi if you get something wrong!

I used an LED Driver to power the whole thing.  I figured this circuit was straightforward enough that I could probably avoid blowing up my Pi 🙂

The LED Driver

It took me some considerable research to tie down what exactly the “magic” was that differentiates an LED Driver from a regular 12V power supply. The answer is that there is very little! Suffice it to say that LEDs are damaged if they receive too much current, so upward fluctuations in voltage (say, if the LEDs received 15V instead of the 12V) can mean that too much current flows through the LEDs and (at best) shortens their life or (at worst) kills them altogether.  Normal power supplies tend not to be particularly tightly regulated or smoothed, so prolonged use can be bad for LEDs. A “Constant Voltage” LED Driver contains these extra components to regulate the power tightly to the correct range for the LEDs.  Subject to the rated wattage, it is of course possible to run other stuff from an LED Driver.

HOWEVER a “Constant Current” LED Driver is not the same thing – it regulates only the current and the voltage can fluctuate… this is probably not suitable for this project – so make sure you go for the “Constant Voltage” type. If it’s unmarked, but states that the output is 12V DC, you’re fine. I can’t find the one I purchased but this one is similar.

The Voltage Regulator

If you decide to power the Pi from the 12V LED Driver you must have something to step down the 12V power source to the 5V the Pi requires.  The best way to do this is a Voltage Regulator.  Note: all voltage regulators are not the same!

The classic Voltage Regulator is the L7805.  It basically divides up the voltage by applying internal resistance so that the load received 5V and the remaining volts are “spent” internally.  These are useful in circuits where roughly the right voltage is supplied in the first place, but additional regulation is needed to ensure there aren’t any voltage surges that might damage components.  You can also use them to step down from higher voltages, but LOADS of heat is generated.  Consider that with a 12V source, more than half the power (and thus the heat) is being disposed of INSIDE the voltage regulator.  This makes the L7805 useful for up to about one amp – but any more and it would overheat.  I think they are technically rated for about 1.5A and include a thermal shutdown, for obvious reasons!  Bear in mind that the Pi can draw upwards of 1A, particularly if USB devices are connected.

A much better choice is a “Buck Regulator” – specifically Texas Instruments’ LM2956.  These are the types of regulator that are used in car chargers for mobile phones and tablets, where currents in excess of 1A are required.  They are much more frugal – in that power isn’t dissipated as heat, but rather it is rapidly switched on and off, then smoothed, thanks to four external components, resulting in a very accurate output voltage and very little heat, and you can draw up to 3A.  The components I used are:

  • 1x LM2956 Buck Regulator
  • 1x 680uF Electrolytic Capacitor
  • 1x 220uF Electrolytic Capacitor
  • 1x 1N5822 Schottky Rectifier diode (the specified 1N5824 isn’t available any more)
  • 1x 33uH Inductor
Image credit: Texas Instruments

Image credit: Texas Instruments

The circuit is straightforward and I built it in one corner of the Humble Pi board, feeding the 5V back to the appropriate pin of the Raspberry Pi’s GPIO connector (and of course the ground rail connected to one of the GND pins).

Voltage Regulator Circuit Closeup

A test with the multimeter shows that this clever little circuit generates 4.996V. Not bad!  I shalln’t bore you with the layout of the connectors on the board – you can work that out yourself from the circuit diagram – and anyway, I bodged it a bit :-).

The LED Controller

If you’re reading this, you’re probably familiar with the Raspberry Pi’s GPIO connector, but suffice to say that it includes several “GPIO Pins” which can be programmed as outputs.  These outputs can then be switched to be either “ON” – when 3.3V is present on the pin (relative to ground), or “OFF” when you get 0V.  This is fantastic functionality, and enough to turn an LED on or off.  Unfortunately, it’s nowhere near powerful enough to switch on a whole string of high-brightness LEDs: there aren’t enough volts (12V is requred) or current (the LEDs use about 1A).

The way around this, is to connect a well-chosen MOSFET transistor to each of the GPIO pins we intend to use.  I chose pins 18 for red, 22 for green & 23 for blue, as you’ll see in the code at the end.

How do we choose an appropriate MOSFET?  Good question – and it’s one I got wrong the first time around.  The key things here are:

  • Type: We require an N-Channel MOSFET transistor
  • Drain-Source voltage: It must be able to handle at least 12V
  • Drain-Source current: It must be able to handle at least 3A (because your LED string might be longer than mine)
  • Gate switching voltage: It must switch on at 1V (ONE VOLT).

This last one caught me out.  I had some MOSFETs that switched at 2-4V – which sounded like enough, but in practice it didn’t work.  All MOSFETs have a switch-on curve whereby they can be partially on at some voltages, and to switch on fully, they must have a higher voltage.  You can find a graph of this curve in the datasheet for the one you are using – for mine (an IRF540Z) it turns out that it won’t be fully ON until it has at least 5V at the gate.  No good when our Raspberry Pi is only 3.3v.  I spent some time messing about, including installing a transistor between the Pi and the MOSFET, before finally giving up and going to find a different MOSFET!  The one I settled on was the IRL2703PBF.  It worked perfectly 🙂

Our MOSFET has three pins, the GATE, SOURCE and DRAIN.  Current flows into the MOSFET through the DRAIN and out through the SOURCE.  This is very easy to get wrong because the words don’t really sound much like what they are.  Just remember that current doesn’t drain AWAY from the MOSFET, it drains INTO it.

MOSFET Pins

In our circuit, each GATE is connected to a GPIO pin (directly).  The SOURCE is connected straight to ground, and each DRAIN is connected to one of the colour connectors on the LED strip. No other components are required.

Cheerlights

The LED strip has a fourth connector, usually labelled +12V, to which we connect the 12V supply from our LED Driver.  And we’re done!  Here’s what my finished board looks like (you can see all the mess I made changing over the transistors and messing about trying to make the wrong MOSFETs work)!

You can see that on the Humble Pi, there are two rows that go straight across the middle – power, and beneath that, ground.  I connected the power row to 12V (rather than the 5V from the regulator) to make it easy to connect the LEDs – which are connected via the white/blue wire.  You can see three little bare wire jumpers that connect pin 3 of each MOSFET to the ground rail.  Finally, the three blue cables connect pin 1 of each MOSFET to its GPIO pin.

The cable is (obviously) telephone wire I happened to have lying around.  Soldering it onto the LED strip was a bit of a pain, as I had to scrape away the clear flexible plastic layer that sits over the connectors, but this was achieved with a Stanley knife.  Don’t breathe the fumes when applying the soldering iron to it!

2. Testing the Circuit

So of course, before connecting your Raspberry Pi, I would recommend giving the circuit a test by plugging in the 12V power source. You should have:

  • 5V on the 5V GPIO pin to supply the Raspberry Pi, relative to one of the GND pins.
  • 0V on pin 3 of each of the MOSFETs
  • No LEDs lit!
  • 0V at pin 1 of each MOSFET
  • About 12V at pin 2 of each MOSFET

Grab a piece of wire with stripped ends and temporarily jump the 5V pin to pin 1 (the GATE) of each MOSFET in turn.  Each colour of LED should illuminate whilst it is touching and go out again when it is removed.

If your LEDs don’t light, it might be because you connected them incorrectly. Check the polarity and make sure you connected your 12V to the right connector on the strip!

3. Connecting your Raspberry Pi

In the prerequisites, I mentioned that you would need to have your Pi up and running with NOOBS or some other Raspbian installed, and connected to the Internet.

You’ll also need to install the “python-twitter” library – which is done using the simple command:

sudo pip install python-twitter

You may also need to install “Requests” – this allows you to suppress certain Twitter warnings which otherwise show in the output.

sudo pip install requests

Finally, here is the Python code, which you should save as a file with a filename ending in “.py” of course.  Because the python-twitter library hasn’t yet been updated to Python 3.x <<rolls eyes>>, bear in mind that you will need to run it using Python 2.7.  So, if Python 2 isn’t the default on your Pi (it is on most), make sure you explicitly run it in that version, or you’ll get errors.

The code is very heavily commented, so it should be fairly obvious what each part achieves, but do leave a reply below if you want anything clarified!

I commented in the code that the Twitter Search API is a bit flaky – or words to that effect.  My experience of the Search API is that it doesn’t always display the latest tweet – in fact some are just ignored and never make it into the search results.  There seemed to be a pattern to it, but I couldn’t be sure. The reality, though, is that most tweets worked okay, so it was “good enough” for Christmas lights. If you wanted it to be absolutely spot-on you could perhaps consider querying the official Cheerlights channel on ThingSpeak – I didn’t try that.  Twitter themselves suggest that it is better to read their “stream” rather than using the search, because no results are omitted.  That looked a little too complex for a project of this level, but if you figure it out, let me know 🙂

There are also more than just the one Twitter library for Python – however I settled on this one (python-twitter) for the sake of ease – the setup was pretty dead simple.  If you install one of the others, the classes will be different, so you’ll have to modify the code.

Why not leave a comment below and let me know what you do with it?  Questions will be answered reasonably frequently, too, so don’t hesitate to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *