Brrrr! Our chestnuts are roasting on an open fire and Jack Frost is biting rather more than our toes. Which can only mean one thing: Winter has well and truly arrived.
So along with the sparkly mornings, we also have the white stuff to look forward to. Snow. You know, the thing that brings modern life to a crunching standstill.
That made us think: surely we could knock-up some pretty cool festive thermal measuring device which would warn of impending snowfall?
Well, armed with our trusty little Raspberry Pi and a few bits and pieces, we did just that.
Pull-on your Christmas jumper, pour yourself a snowball and check out how you too could amaze your friends by predicting the snow ahead of the game…
We started off by putting our plan together.
It seemed a good idea to use LEDs hooked up to our Pi’s GPIO pins to show temperature readings from the DS18B20. With this we could create a display showing temperatures swinging from -5ºC to +5ºC. We also thought it would be cool for an extra six LEDs to flash to when there air hit freezing point (0º) and snow is most likely.
Being sentimental souls, we thought we’d make our festive thermometer in the shape of a snowflake. Which also made life a little harder, but what the heck – it’s Christmas.
Straight off we knew we’d have to make sure our Pi was snuggling up to our acrylic snowflake. So we simply mounted it on the back – and gave the snowflake a large centre to hide all the gubbins.
Looking at the plan we figured out we’d need 13 GPIO pins from our Pi. This accounted for the 11 temperature indicating LEDs, the six LEDs which would flash when there was a high chance of snow and the temperature sensor itself.
Our biggest job was going to be writing the Python script. As you may know, our knowledge of Python is sketchy at best, so writing multiple functions was going to be fun. Not.
Anyway, on with the show.
Coding was our first task – and we started with an easy function. We just needed it to take readings from the DS18B20, and we’d already done this when we built a Raspberry Pi 1-Wire thermostat. So we simply used the code from that project as the basis for our first function.
After testing (check) we started on the next one. Here we used print with our getTemp() function in the while loop. Fortunately, this correctly displayed temperature readings from the DS18B20 – so we knew it was working just fine.
OK, this was going well. Onto our next function.
The second function we look at creating was updateThermometer(). This would take readings from the sensor and use it to figure out which LEDs to illuminate. But we needed to set up the all the GPIO pins before we could create this function.
This took about half a lifetime. To be fair, it was our own fault since, rather than lighting just one LED when the temperature is above or below zero, we wanted ALL the LEDs from zero to that temperature to be glowing brightly.
Here’s the list variables which define which LEDs will be on and which will be off for each temperature:
When the updateThermometer() function is called, it then uses the parameter passed to it (temperature) to decided which LEDs need to be turned on and which need to be turned off.
After testing our updated thermometer, we could move on to creating the third and final function…our (cue the dramatic music) snowAlert().
This neat function takes a parameter and, when called with “on”, switches on the snow alert. The alert LEDs blink five times and then stay illuminated. When anything else is passed as a parameter, the alert LEDs are switched off.
The main program runs forever in a while loop, calling the getTemp() function to get the temperature and printing it out. Next, it calls the updateThermometer() function and passes this to update the temperature.
Then it checks to see if the temperature has dropped to zero. If it has, it calls snowAlert() to illuminate the alert LEDs. But if the temperature has risen from zero, it turns off the alert. It then updates the previous Temp variable, sleeps for 0.5 seconds and then starts all over.
So much for the programming.
Now for the nuts and bolts of actually constructing the thing.
We began by prototyping our snowflake in good old MDF. When we were satisfied the size was just right to hide the Pi AND look simply stunning, we took a deep breath and laser cut our shape from (mmm!) frosted white acrylic. We attached four hex standoffs and a circular plate behind the snowflake to fix the Raspberry Pi and breadboard to.
(Top tip: use nylon screws – they’ll stand out less than metal ones.)
Of course we had to locate the DS18B20 temperature sensor outdoors, so we fit it into a metal capsule with a screw lid. After drilling a cable hole in the lid and threading the cable through, we then sealed the hole with epoxy resin.
Next came the wiring bit.
Each LED anode on the temperature scale is connected to a resistor and a GPIO pin. The cathodes are all connected together and to GND.
All six alert LEDs have a resistor connected to the anode on one end and to the resistor on the other, and all are connected together with all the cathodes connected to GND. Six LEDs in parallel will draw more current than a GPIO pin can safely handle, so we hooked up the snow alert GPIO pin to the base of a PNP transistor via a 1K resistor, with the emitter connected to 3.3v. The transistor collector is connected to the resistors which are connected to anodes of the six LEDs.
Really, this was like a scene from The Hurt Locker at times. But we got there.
Finally, it was time for testing.
We tested our thermometer by dunking the sensor in a bowl of ice cubes and staring at the output of the Python script with racked concentration. To start with, all the LEDs from 0 to 5+ were illuminated and, as the temperature dropped below 5ºC, one by one the LEDs were extinguished. When it hit zero, there was just a single LED lit on the scale – and (to much cheering and backslapping) the snow alert LEDs flashed.
You can check out our acrylic design and Python code on GitHub.