Don’t try this unless you know what you are doing. Mains electricity can kill you.
Since publishing this blog post, I’ve had some feedback about the way I’ve done this and it needs improvements and a redesign to make it safer. Please don’t copy this. I’ve removed some photos so I’m not setting a bad example.
Back to the original post
Ever since getting my Gertboard in late September, I’ve been thinking about using a Raspberry Pi to switch “proper things”. By “proper things” I mean real, useful, BIG things using mains electricity.
The Gertboard has a relay controller, but to actually switch anything larger than a few millivolts you need to attach relay(s).
What’s a relay?
A relay is a device which allows a low power circuit to switch a higher power circuit. In the most popular relays, the switch is controlled by an electromagnet switch (solenoid). When there’s current flowing through the low power circuit, an electromagnetic solenoid switch flips from one position to another, switching on/off the higher power circuit (depending on how it’s set up). It makes an unmistakeably characteristic “click” sound. relay-click Click the play button to hear the relay click sound.
Learning from the Gertboard
So I bought some relays to experiment with. The first thing I noticed was that the feet of the relays don’t line up very nicely with a standard breadboard. So they had to be bent a bit to make connections. I can see people breaking these with frequent plugging in and out of breadboards.
I did some preliminary tests with the Python version of the Gertboard ocol (open collector) program I wrote, using it to switch leds on and off. I love leds, but they’re a bit tame aren’t they? I want to switch some “proper things”. That means connecting up to mains electricity.
The other thing the Gertboard taught me was how to use a Darlington Array. Without going into great detail (because I don’t know it ) a Darlington array is a collection of Darlington Pairs. A Darlington Pair is a pair of transistors used for amplifying a very low switching current up to a useful level.
The Darlington Array on the Gertboard has 8 Darlington Pairs on it, but 2 are not connected (which is why it can run 6 relays, not 8). I got hold of some cheap Darlington Array chips and decided to do some experiments directly interfacing to the Pi. But first there was some hardware work to do.
Mains electricity + breadboard = bad idea
There’s no way you’d get me (or anyone sensible) plugging mains-connected wires into a breadboard. Too hazardous. No. What was needed was proper screw terminals and the relay soldered down onto a proper board. A PCB (printed circuit board) with holes in the right place for relays and screw terminals for the mains wires would be a good solution, but I made do with stripboard for the time being.
So I decided to solder up a 5 Volt relay, Darlington Array and screw terminals onto a piece of stripboard. I still had to bend the legs on the relay a bit, but, once soldered down, they’re there for good. You’ve already seen the top side of the board.
A quick test with a mains powered lamp, with a hacked plug, provided proof of concept. A click, not a bang!
YAY – it works just like it should.
Then, much piddling about in a live Python environment ensued; enjoying the simple satisfaction of being able to switch a lamp on and off at the touch of a keyboard.
This opens up all sorts of possibilities
Then my mind started wandering…
If you can use your Pi to switch on a lamp…
…and your Pi is connected to the internet…
…and you have a smartphone…
…you can log into your Pi and switch it on/off with that
The possibilities for remote and/or automated control of virtually anything just became a reality. The possibilities are limited only by our imagination (and maybe our budget ).
OK, but I need a demo for the next Milton Keynes Jam
So my daydream about remotely controlling the universe by connecting relays to my RasPi met with the harsh reality that the MK Jammers would be expecting me to come up with something…
- and hopefully just plain fun.
Last time we had flag waving, but, although that’s extremely cool, that is soooooooo September. The world’s moved on. We need something more.
- We need a RasPi-switched, mains-powered fan, run by a Python script and temperature sensor. But that’s not enough…
- We also need a RasPi-switched, mains-powered lamp, running from the same script, with a light sensor to tell it when it’s dark enough to switch on. But that’s still not enough…
- We need a beeper to alert us when the fan is going to spin up (I’ve got no guard on it – hate those things). More…
- We need different beeps for fan and lamp. More…
- We need an indicator led, pulsed with the beeper. More…
Ain’t you getting a bit greedy?
OK, let’s have some custom sounds as well (even if it’s only recordings of me talking). Just so we can have it super silly, fun, eye-catching, attention-grabbing, noisy, interactive, memorable…
And it’s all got to be safely packaged. So here’s what we ended up with…