User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Pi on / off PSU relay switch

Thu Feb 21, 2013 12:23 pm

As everyone is aware the Pi has no on / off power supply switch the only way to turn it off is to physically remove the micro USB cable or to turn off the main adapter / pull plug from the wall.
Turning off from the wall isn't an option for me since I share the same socket with my Sony infrared headphones transmitter.
The lack of a power supply switch can also be an issue for battery operation since the Pi would still be providing power to the USB ports even after shutdown mode (init 0) and draining more energy unnecessarily.

After some time messing around on breadboard I have come up with a relay circuit design which I had finally committed to a circuit board and I'm happy to say works really well.
There's no need for any fancy background software or microcontroller, just a single link wire from the GPIO 14 (TXD) / UART TX pin to indicate the running state of the Pi.

To turn on the Pi you push the "on" button, when the Pi has shutdown the relay circuit automatically kills the power.
There is a built-in delay for detecting the Pi shutdown, this is because a system reboot could be mistaken momentarily as a full shutdown when it's not.
If for whatever reason you cannot shutdown the Pi maybe a system hang or kernel panic there is a "off" button to instantly kill the power.
The circuit also provides a level of protection against accidental short circuit of the Pi's 5v power rail so your battery pack / mains PSU won't fry or pop.
This works because the relay requires power to keep the switch active, a short circuit would cause a voltage drop which in turn causes the relay to lose power and it kills the switch. :)

The circuit:
Image
This circuit has been released under the Creative Commons Licence for non-commercial usage.
You are free to copy and reproduce but for personal or educational purposes only - http://creativecommons.org/licenses/by-nc/2.0/uk/

The transistor values are not critical any general purpose ones will do i.e. BC548, BC108, BC109, I happen to be using S9014C because they're dirt cheap from eBay and I still have plenty to use from my previous purchase.
The component value for diode D1 isn't critical either as long it's silicon and not a zener diode but it's required to protect the GPIO pin from being supplied +5v.
The R1 resistor and C1 capacitor are used to set the power off delay, if you want a different duration then change the component values.
The 47k resistor in the circuit is used to charge C1 this is to ensure the transistors will turn on the relay the instant you press the "on" button.
When the circuit is switched on the 47k is disconnected and the GPIO 14 (TXD) is now responsible for keeping C1 charged and thus the transistors stay turned on.
When the Pi is in shutdown mode the GPIO 14 (TXD) pin goes low and C1 discharges until the voltage drops too low for the transistors to maintain power to the relay.
If a system reboot occurs the GPIO 14 (TXD) pin also goes low, but the delay effect provide enough time for the GPIO 14 (TXD) pin to return back to high again.
Since the 47k resistor is required to charge C1 there is a very tiny standby current of approximately 75uA, a small price to pay for an instant startup from the button.
You could remove the 47k altogether but then you would have to press and hold the "on" button for at least 2 seconds which frankly isn't practical.
If you want to operate the relay as a basic on / off latching circuit with no shutdown detection then disconnect the 47k at the relay end and connect this directly to the PSU +5v rail.

Image
Image
Image
Image

and a short demonstration video:

http://s53.beta.photobucket.com/user/re ... b.mp4.html

Richard S.
Last edited by redhawk on Tue Mar 05, 2013 12:11 am, edited 1 time in total.

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: Pi on / off PSU relay switch

Thu Feb 21, 2013 1:26 pm

Hi,

thanks for posting this.

I plan to make one of these as a small project soon, so I'll start collecting the bits.
I'm new to all this, so be prepared for lots of questions !

cdlt,

theimmc
Posts: 53
Joined: Tue Feb 05, 2013 10:31 pm

Re: Pi on / off PSU relay switch

Thu Feb 21, 2013 6:37 pm

Very nice, thanks for sharing!

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: Pi on / off PSU relay switch

Mon Feb 25, 2013 12:42 pm

Hello

I've started pulling my parts list & numbers but I'm not sure about the which Relay to get.

You show a 5-6v relay in your schematic and Google/e-bay search returns lots of different shaps, sizes, number of posts sticking out, and tech specifications, etc.

Any chance for a part number or something for me to narrow the selection ?

thxs

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Pi on / off PSU relay switch

Mon Feb 25, 2013 1:38 pm

The relay I used in my project was rated at 6v although it worked fine with 5v as did another of my 6v relays so I've come to the conclusion that either should work in the circuit.

The part number is FRS1-S 6v, I had a quick look on eBay but no one sells them anymore, however there are plenty of alternatives providing they meet the require spec:
-can operate at 5v
-can handle 1A or more via the relay switch
-has a SPDT switch (single pole double throw)

Mapin seems to be the cheapest so far which is ironic considering their reputation for overpriced electronics. :)
http://www.maplin.co.uk/2a-6v-spdt-ultr ... lay-218089

eBay links:
http://www.ebay.co.uk/itm/Omron-G5V-1-5 ... 0808013194
http://www.ebay.co.uk/itm/Pack-10x-subm ... 1066868099

Just be careful when wiring a relay make sure you know which pins belong to the coil, pole and throws (the coil is more than likely to be the 2 pins in the middle).

Richard S.

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: Pi on / off PSU relay switch

Mon Feb 25, 2013 2:19 pm

perfect - thanks

... I am hoping that I will actually learn something about these parts once I start plugging them into the breadboard !

cheers

TCadd
Posts: 5
Joined: Sun Feb 24, 2013 8:10 pm

Re: Pi on / off PSU relay switch

Wed Feb 27, 2013 12:17 am

I am now wishing my electronics parts and soldering iron weren't all about a thousand miles away... It will be a few months before I can build myself one of these. Clever design, I really like it!

kieranc
Posts: 14
Joined: Sat Jan 26, 2013 12:02 pm

Re: Pi on / off PSU relay switch

Wed Feb 27, 2013 9:55 pm

I've just built one and it works perfectly, thanks very much Richard :-)
I used an Omron G2E-184P-H-M-US relay, a single BC337 relay in place of the pair of S9014C and 2k2 resistors in place of the 10k items to increase the input current for my single transistor. If my calculations are correct it should still only draw 1.5mA from the GPIO pin which is well below the 16mA limit. The BC337 has a minimum gain of 100 so 150mA available for the relay, the one I've used only requires 41mA so again, plenty of headroom.
I'll report back if it explodes and kills my dog but I'm feeling reasonably confident it won't.

ImageImage

MeMike
Posts: 11
Joined: Sat Dec 15, 2012 3:13 am

Re: Pi on / off PSU relay switch

Mon Mar 04, 2013 7:17 pm

Richard, I'm a complete noob in the electronics.
I built your circuit (which I think could be great). But have a couple of issues.

1) I'm using a Amperite PC4-1C005D2 relay , the darlington pair with 2n3904 transistors and getting 4.26v to the RPi. I'm using a regulated power supply (Wallwart) 5v 2A that is showing 4.96v going into the circuit. Any idea where I went wrong?

2) Rather than use 2x 2N3904s for the darlington can I use a mpsa13 or 14. What should the gain be? I tried a TIP120 (gain 1000) and no go.

Thanx in advance.

Mike G.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Pi on / off PSU relay switch

Mon Mar 04, 2013 9:22 pm

Hi Mike, a drop in supply voltage is usually a good indication that whatever is attached is using a lot of current.
On my board if my memory serves me correctly when the relay is active the total current usage was around 45mA to 50mA.

You need to double check your wiring perhaps the diode across the relay coil is backwards, or you're not connected to the relay coil but the switch part, or one of the transistors is inserted backwards too which is easily done since not all have the same leg arrangements.

Also if you have a digital meter there are few checks you could make:
-check to see if you have a low resistance between +Vcc and ground connections on the board with and without pressing the on button.
-use your PSU and check current usage with or without pressing the on button.
To measure current your meter is connected in series with the supply i.e take the PSU +Ve into the meter and the other meter terminal into the board +Vcc rail.
Start by using the 10A range (this usually requires your red terminal lead going into a different hole) if there is a short circuit on your board you don't blow the internal fuse.
If everything looks okay try the 200mA range (remember to return the red wire to it's original hole) and see what readings you get.

Btw: are you working on breadboard or have you soldered you components to PCB??

For troubleshooting start by removing transistor Tr1 and then check Tr2 to see if it can switch the relay on or off by itself.
For this you'll need to attach a 2.2k resistor to the base of Tr2 and the other end of the resistor would touch and untouch the +Vcc input rail.

Does the relay "click" when you touch and untouch the +Vcc rail??

As for using Darlington pair transistors like MPSA13 / MPSA14 I don't see this being a problem but I haven't tried this myself so could comment on their effectiveness.
Your TIP120 part may not be working because of problems elsewhere in the circuit.
The gain in the circuit isn't strictly that important the whole point of using Darlington pairs was to make the gain nice and high so only a little current would be required to turn on Tr2 to turn on the relay.
This takes away the need for using specific transistor part numbers so any general purpose switching or amplifier transistors should work, unless they have a really poor hFE figure. :)

Richard S.

MeMike
Posts: 11
Joined: Sat Dec 15, 2012 3:13 am

Re: Pi on / off PSU relay switch

Wed Mar 06, 2013 1:49 am

Richard, thanx for the response.
I'm working off a breadboard. Pulled the circuit apart and am rebuilding. Let's see where that gets me.
- I think I may have had a diode in backwards. On the rebuild, I'm testing as I go along and I put a diode in backward. Voltage dropped like a stone (burnt my finger removing the diode - Nobody told me they could get that hot).

- Looks like the relay I'm using draws about 100mA, Checking the relay you used, that makes sense given the coil resistance. The FRS-1 is low at roughly 60 ohm.

I'll use your comments/instructions as I go along.

Again thanx...

Mike G.

RobinGreig
Posts: 15
Joined: Sat Oct 27, 2012 2:40 am
Location: Calgary, Alberta, Canada

Re: Pi on / off PSU relay switch

Wed Mar 06, 2013 6:21 pm

Thanks for posting this, great information.

On a side note, what program did you use to generate the schematic?

I'd like to be able to do the same.

Thanks,
Robin
www.robingreig.ca
Arduino + Raspi + Amateur Radio enthusiast since 2012
VE6RBN Canadian Amateur Radio Callsign

MeMike
Posts: 11
Joined: Sat Dec 15, 2012 3:13 am

Re: Pi on / off PSU relay switch

Thu Mar 07, 2013 10:36 pm

Richard, thank for the info, and the circuit is great.. Got it to work.
Instead of 2x transistors I used a Darlington Pair MPSA14 (The 13 wouldn't work).

A question, why would adding a voltmeter (DMM actually) across the PiVcc and GND cause the circuit not to work. I was trying to measure actual voltage to the RPi.

Again thanx....
Mike G.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Pi on / off PSU relay switch

Fri Mar 08, 2013 11:24 pm

From what I can tell the MPSA14 has a much higher DC gain than the MPSA13 so it's possible the base current isn't high enough to switch on the relay properly.
Could you try your MPSA13 again, replace the 10k resistor at the base for someone a little smaller i.e. 4.7k, 2.2k and see if that helps.

As for the DMM that is a weird one digital meters usually have a really high resistance when measuring voltages (except for battery testing when a load is required) this shouldn't interfere with the circuit.

Do you have the same problem when checking TP1/TP2 with your DMM??

Richard S.

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: Pi on / off PSU relay switch

Sun Mar 10, 2013 7:30 pm

Hi

just a beginner but I've made some progress but now I am stuck around the relay. I've tried a few things but I can't understand how to take the schematic from above and physically wire this up.

I'm using a breadboard and have substituted a LED and a resistor in place of the pi for debugging purposes.

my relay is a K1BE06 and the data sheet is here>>>
http://www.anglia.com/product_search/da ... t_id=15568

for purposes of illustration I’ve show my progress so far with fritzing diagram but note the relay is not like mine (it is just to illustrate the situation)

I can’t sort out
where the 47k resistor should connect
where the 5+ power in should connect
where the power off the tactile switch should connet to the relay.

so basically anything around the relay, ….I’m lost

https://www.dropbox.com/s/ibeg494oxyi84 ... hot022.bmp

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Pi on / off PSU relay switch

Sun Mar 10, 2013 8:39 pm

I hope the following colour coded photo should help. :)

Image

The pins should represent the underside of the relay (I hope), the coil are the two pins in the middle, the switch are the four pins on the outside, take note the spacing between each pin for orientation.
The pole in green has 2 pins they are both wired together internally so you only need one or use both.
The throws are in purple and blue, when the relay is off the pins at purple and green should have a low resistance.
When the relay is activated the pins at blue and green should have a low resistance instead.
If you're still unsure then use a multi-meter (resistance setting) and check the pins at green and purple make sure this shows low resistance when the relay is off (no power).

When operating a relay it is important to have a diode across the coil, this is to cancel out any nasty high voltage spikes in the reverse direction when the coil's magnetic field coil collapses.
High voltages in the wrong direction is bad news for transistors this may kill it or damage them. :)

It's good that you're using an LED as a Pi substitute just be aware that without the signal from the GPIO14 pin the relay will switch on, wait about 4 seconds then power down.
Only when connected fully the Pi including GPIO14 can the relay stay active properly until the Pi is in shutdown mode.

Richard S.
Last edited by redhawk on Mon Mar 11, 2013 7:36 pm, edited 1 time in total.

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: Pi on / off PSU relay switch

Sun Mar 10, 2013 9:21 pm

That's great.
Thanks for that. I'll give this a try tomorrow and report back.

Cheers

bullwinkle
Posts: 117
Joined: Wed Jan 09, 2013 12:14 pm

Re: Pi on / off PSU relay switch

Mon Mar 11, 2013 6:53 am

it works .... looks like a pile of spaghetti, but it works

User avatar
mattybrad
Posts: 2
Joined: Tue Mar 12, 2013 1:07 am
Location: Oxford, England
Contact: Website

Re: Pi on / off PSU relay switch

Tue Mar 12, 2013 1:13 am

Works brilliantly! Thanks so much for sharing! Was looking for a circuit like this to use in a Pi-based MP3 player for my hi-fi; wanted a simple, chunky on-off button but also wanted to let the Pi shut down properly rather than just cutting its power completely.

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: Pi on / off PSU relay switch

Thu Mar 14, 2013 12:31 am

hi, I have a similar circuit that is a bit simplified with a 5vdc relay, 1N4001 diode and an ATTiny85 chip (running arduino code). I have a prototype working but I am new to electronics and my schematic is horrible :P.

sprfly
Posts: 5
Joined: Wed Mar 13, 2013 12:28 am

Re: Pi on / off PSU relay switch

Sun Mar 17, 2013 11:36 pm

Thanks for sharing this, it's just what I was looking for. I have included a pic of how I plan to use it in my car. I will be using it in my car to mirror my phone to a screen that I already he mounted in my car. Previously with my iPhone 4 I just had a hard wire connection to the screen. Now that I have a 5, instead of paying apple an arm and a leg for an adapter I figured I'd go wireless. I'm hoping airplay mirroring is available soon, in the mean time I may just use VNC.

Anyhow, I plan to use a DPDT relay connected to the ignition 12v to do the switching and a script running in the background to monitor GPIO 17 and initiate the shutdown. In the picture I have only drawn the top line of the PSU circuit. Thoughts/suggestions? Thanks!
Image

Edit: I was looking at this again and found a mistake; the 12v to 5v converter will have to go before the relay.

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: Pi on / off PSU relay switch

Sat Mar 30, 2013 4:40 pm

@redhawk is it possible to supply a breadboard version (image) or fritzing image? I am a noob to EE and reading schematics. My first attempt was a flop.
Thanks

JoeSchmuck
Posts: 14
Joined: Sat Jun 22, 2013 2:28 pm

Re: Pi on / off PSU relay switch

Sun Jun 30, 2013 4:33 am

This is a really nice schematic and idea. I love not having to use special code in the rpi to turn off the power.

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: Pi on / off PSU relay switch

Sun Jun 30, 2013 2:00 pm

I love the idea, and will defenitely be building a few of them for the rpis I have around here. Great idea for xbmc system that sits behind the tv, those are a pain to keep unplugging/plugging in, much easier to just put a button and press it to start it up, then it shuts itself down automatically :D

Just one question, Ive always been curious about this, but lets say like you use debian, and disable logging on the serial port so you can use it for other stuff, the other stuff will be taken off most of the time, but my question is, with it having disabled logging to the serial port, does it still work with this circuit?
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

waygood
Posts: 5
Joined: Thu Jun 21, 2012 12:08 pm

Re: Pi on / off PSU relay switch

Sun Jul 21, 2013 4:45 pm

Hi,
Before I start to buy the parts for this, the diagram says tactile switches. I googled it and they are basic on/off switches. But the video suggests to me that they are momentary ones.
Which are they? as I want to put my Pi in a multimedia pc case and the power switch on it is a momentary one.
Thanks

EDIT
Thanks for the reply (initially I missed it as it was on page 2). I also found this:
http://www.mosaic-industries.com/embedd ... controller
Last edited by waygood on Mon Jul 22, 2013 10:17 pm, edited 2 times in total.

Return to “Other projects”