Continuing Raspberry Pi

Posted on January 1, 2012

In my last post I discussed how you could use the Raspberry Pi to control a LED using python. Now let’s build up on the last experiment. Turns out you can use the exact same method to control a relay (which you can use to switch on/off lamps or other electrical utilities which aren’t driven on 5V). To make it a little more interesting I will show you how you can control that relay through a web interface, which we will host on the Raspberry Pi using Python.

Seeedstudio relay

I’m using this relay controller from Seeedstudio, which is made for their Arduino Shield: Grove, but it can be used without the shield. To connect it just connect the pins the following way (pinout can be found here:
1. VCC to 5V Power on the Raspberry Pi
2. GND to Ground
3. SIG (signal) to GPIO18 (same as I used in my previous post)

Now that the relay is set up let’s make it talk. Download the following script on your Raspberry Pi:

wget https://raw.github.com/sirmc/Sirmc-s-public-folder/master/Raspberry%20Pi/light.py  

You need to export the pin:

gpio export 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 relay, it is working! Now just run the script (to end the script press CTRL-C), and make sure your Raspberry Pi is connected to your network:

python light.py  

You should have the IP address of your raspberry pi, if you don’t have it you can find it with ifconfig.
On another computer (or your smartphone) connected to your network go to the address: http://RaspberryPiIP:8051
You should see two buttons: On and Off. Test to click on them… Wasn’t that cool!

You can connect a lamp or something else to your relay. It’s now up to you to start automating your home using the raspberry pi

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

blog comments powered by Disqus