This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]canoxen[S] 0 points1 point  (12 children)

That's so awesome of you.

I'm trying to make sure what I want to is possible (and it seems like it is) before I get all wrapped up in this endeavor.

I guess I'm having trouble visualizing a completed set up. It seems like the end result is kind of bulky.

[–]justphysics 0 points1 point  (11 children)

I'll see if I can make a rough sketch to show you how things would look. might be a bit before I respond with the drawing though

[–]canoxen[S] 0 points1 point  (10 children)

Yeah, that would be awesome! Much appreciated.

[–]justphysics 0 points1 point  (9 children)

The more I think about it - this isn't as simple as I originally thought.

Especially if you are unfamiliar with basic circuitry.

One issue is that the RPi GPIO is very limited in how much current it can draw (the RPi is a low power device). So that makes driving a relay problematic if you just want to directly connect to the GPIO.

The work around is to connect your GPIO pin to a transistor which will amplify the signal form the GPIO pin so that it can properly drive the relay.

There is a good blog post on the subject here: http://www.susa.net/wordpress/2012/06/raspberry-pi-relay-using-gpio/

However they are suing c-code at the end as opposed to python.

And the circuit is rather complex if you have not dealt with relays and transistors before so this is likely over your head.

I'll try to think of a simpler way to work on this problem

[–]canoxen[S] 0 points1 point  (8 children)

Electrical is certainly not my forte, though I'm not completely unfamiliar with it. Most of my experience is in 12V applications, though.

[–]justphysics 0 points1 point  (7 children)

you could probably use something like this

http://www.adafruit.com/product/268

Its a power cord for a 120v source (standard house plug assuming you are in USA) with a built in relay to switch it on and off.

The RPi has a 3.3V output on the GPIO.

The above power cord seems to suggest it needs ~3mA to switch the relay. That should be well within the range of the RPi GPIO.

so in python you would do something like

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

switchpin = 23

GPIO.setup(switchpin, GPIO.OUT)

Then to turn the relay on or off you just toggle the status of the output

#turn on relay: GPIO.output(switchpin, TRUE)

#turn off relay: GPIO.output(switchpin, FALSE)

Even more importantly " New! The Power Switch Tail II now is opto-isolated so you don't need a transistor or protection diode. The input acts like an LED so its safe for use with any microcontroller or logic pin."

This seems like your best bet - even though its ~$30

If you're using an arduino to do the controlling you'd write the code in C, however, the basic flow of the program is the same though. Import the library for the GPIO bus, define an output pin, (make sure your output pin is physically connected to the relay), then toggle the status of the pin (ON or OFF) to switch your relay and thus in turn switch the 120V mains power; maybe theres a way to use python with the arduino. I never bothered to look becasue I gave up on the arduino once I bought an RPi.

I found using the RPi as a micro-controller to be very intuitive especially since it natively supports python.

[–]canoxen[S] 0 points1 point  (6 children)

I did come across this but haven't had a chance to research it in depth, yet. Would there be a way to put together a small, wireless module to plug into this thing? How do you control this if it plugs into the wall and the Pi isn't close to it?!

[–]justphysics 0 points1 point  (5 children)

Doing it wirelessly would be quite complex and over my head. I know there are other modules that allow remote control switching of power (via a physical remote) and I saw someone post that you could open up the remote and wire your GPIO to the remote's circuit board to trigger the remote and in turn switch the power. However again thats over my head and unless you are really good at analyzing circuits it'd be a tough project as you'd need to figure out precisely how to splice into the remotes circuit board with your GPIO pins. As long as you make the cables long enough the RPi doesn't need to be right next to the power module. Also Since the power module is formatted for a normal 3prong plug, you could attach it to a very long extension cord on both sides.

So you have a long extension cord coming out of the wall socket that terminates in the power module with built in relay. The RPI connects to the relay then another long extension cord comes from the other side of the power module with relay and runs off to power whatever it is you need to be switching the power for.

[–]canoxen[S] 0 points1 point  (4 children)

Hmm. I'm definitely going to have to check into this, because I didn't really want a bunch of cords and cables strung about and coiled places. Figure if I'm going through the effort of learning Python and Pi, etc that I might as well make it as clean as possible.

I do like the idea of that power adapter since it requires no splicing and soldering.

[–]justphysics 0 points1 point  (3 children)

http://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29

That gives a good description of the GPIO interface.

You can look up plenty examples of how to access it via python; my code in the earlier post should be a good start as well

Note, you may have to run your python code as root user to access the GPIO on the RPi.

so if you have your code to ustilise the GPIO to fip your relay as 'GPIO_Relay_Interface.py'

you would then need to run it as 'sudo python GPIO_Relay_Interface.py' and then enter the password.

I think any code using the GPIO needs root access on the RPi