Continuing Raspberry Pi

Posted on January 1, 2012
Tags:

In my last post I dis­cussed how you could use the Rasp­berry Pi to con­trol a LED using python. Now let’s build up on the last ex­per­i­ment. Turns out you can use the exact same method to con­trol a relay (which you can use to switch on/off lamps or other elec­trical util­i­ties which aren’t driven on 5V). To make it a little more in­ter­esting I will show you how you can con­trol that relay through a web in­ter­face, which we will host on the Rasp­berry Pi using Python.

Seeedstudio relay

I’m using this relay con­troller from Seeed­studio, which is made for their Ar­duino Shield: Grove, but it can be used without the shield. To con­nect it just con­nect the pins the fol­lowing way (pinout can be found here:
1. VCC to 5V Power on the Rasp­berry Pi
2. GND to Ground
3. SIG (sig­nal) to GPIO18 (same as I used in my pre­vious post)

Now that the relay is set up let’s make it talk. Down­load the fol­lowing script on your Rasp­berry Pi:

wget http­s://raw.github.­com/sirm­c/Sirm­c-s-pub­lic-­fold­er/­mas­ter/Rasp­ber­ry%20Pi/­light.py  

You need to ex­port the pin:

gpio ex­port 18 out  

Then you can to set up your pin mode and test it:

gpio -g mode 18 out  
gpio -g write 18 1  

If you hear a click from your re­lay, it is work­ing! Now just run the script (to end the script press CTR­L-C), and make sure your Rasp­berry Pi is con­nected to your net­work:

python light.py  

You should have the IP ad­dress of your rasp­berry pi, if you don’t have it you can find it with if­con­fig.
On an­other com­puter (or your smart­phone) con­nected to your net­work go to the ad­dress: http://Rasp­ber­ryPi­IP:8051
You should see two but­tons: On and Off. Test to click on them… Wasn’t that cool!

You can con­nect a lamp or some­thing else to your re­lay. It’s now up to you to start au­tomating your home using the rasp­berry pi

Here is a (blurry) video showing the set up.

blog comments powered by Disqus