2012-08-26

Raspberry Pi controlled lamp

I'm a software guy and I'm scared of electronics. At the same time I find it exciting when software actually does something in the real world. (That's why I love Lego Mindstorms.) Home automation is my lifelong dream. I'd like to be able to use my computer to turn the lights on and off, roll up the window blinds and remotely control everything else that can be controlled.

That dream will have to wait, but today I used my Raspberry Pi to make a small proof of concept.



One of the many cool things about the Raspberry Pi is that it has some general purpose I/O pins exposed that can be used to communicate with the outside world using various protocols. The simplest thing you can do is to set a single pin to a high or low state. When combined with something called a relay, that is enough to control an electrical device plugged into a wall socket (or at least to turn it on and off). I used this relay from Seeedstudio. The relay has three pins (actually four, but one is unused), labeled VCC, GND and SIG. I connected VCC to the 5V pin on the Pi and GND to the ground pin. The SIG pin must be connected to one of several GPIO pins available. You can find a detailed description of Raspberry Pi's GPIO pins on the wiki. I connected the SIG pin from the relay to GPIO18, which is the sixth pin from the left in the top row.

Then I butchered a power strip. I removed the insulation from a segment of the cable, cut one of the cables inside, stripped some insulation from the ends and put them into the screw terminal on the relay. This is the part I was most uneasy about. You should obviously exercise caution when working with stuff that will be connected to mains voltage (extra caution when it actually is connected).

With everything attached, the only part remaining is software. The GPIO pins can be controlled through special files in the /sys filesystem, but I used a library called WiringPi. It includes the gpio command line tool, which makes working with GPIO pins easy.

First you have to configure the pin for output:

gpio export 18 out

Then you can set the state to high with:

gpio -g write 18 1

And to low with:

gpio -g write 18 0

Thus switching the relay on and off. I wrapped those commands in some trivial CGI scripts for the purpose of demonstration and controlled the lamp via a web browser.

10 comments:

  1. I keep reading about warning of not using 5V equipment with the Raspberry Pi, but then I get a bit confused when the GPIO pinouts have 3.3V and 5V. Did you encounter any issues around that?

    ReplyDelete
    Replies
    1. In this case the 5V never goes back to the Pi, it just powers the relay. And 3.3V from the GPIO pin happens to be enough to trigger the relay (even though it theoretically expects 5V on the signal pin). If you had some part that communicates with the Pi in the other direction and uses 5V level logic then you'd probably have to use a level shifter to avoid frying the Pi.

      Why is 5V exposed at all? I guess for things like this and also you could probably power the Pi through that pin instead of the micro USB port.

      Delete
  2. Love how I can relate with "software that can actually do something in the real world is exciting" and the electronics part also scare me since I am just a kid still new with the volts and stuff. Inspiring blog btw!

    ReplyDelete
  3. I am very new to electronics. If I can't get hold of the relay you used what other types can be used? Is there a general type?

    Thanks in advance!

    ReplyDelete
  4. Here is a similar implementation, with two channel relay:
    http://www.youtube.com/watch?v=bhLUsuEnkW4
    http://www.kamami.pl/index.php?ukey=product&productID=184704

    ReplyDelete
  5. Hi, is the relay the only piece I need to buy? ie. I buy the relay, splice the wire, connect it to the Pi, and voila?

    ReplyDelete
    Replies
    1. Yeah. You'll need some jumper wires to connect the relay to the Pi, but that's it.

      Delete
    2. Thanks for the quick reply. I ordered one and can't wait to get it up and running.

      Delete
  6. Can you put in your code for this programm?

    ReplyDelete
  7. Very cool, but which light are you using or which one would work with this.
    Also what is the light wire plugged in to in other words what is that plug socket + what is the code ?

    Thanks will be trying this soon probably !!!! :)

    ReplyDelete