SSilver2k2
Posts: 179
Joined: Wed Jun 06, 2012 1:51 am
Location: United States
Contact: Website AOL

RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 8:01 pm

So I have a lot of spare parts laying around. Arduinos, switches, heat shrink tubing, extra wire, connectors, joysticks...anything a well stocked techie closet should have.

With that said, I've been getting tired unplugging and replugging in the microUSB power supply just to reset something when I inevitably cause my Pi to lockup. So i've started pulling some parts and I've come up with this idea:

Image

I have a microUSB extension cable, cut in half, the Vcc spliced to the rocker switch, and the other wires spliced back into each other, then sealed with heat shrink tubing.

I'm going to build the protoype this weekend, but I'd love to hear some ideas and suggestions.

-Shea
My blog of various geeky things - http://blog.sheasilverman.com
PiPLAY - http://piplay.org
DeskCade.com - Mini Raspberry Pi Arcade Cabinet

User avatar
Mortimer
Posts: 924
Joined: Sun Jun 10, 2012 3:57 pm

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 8:19 pm

I think this is a good idea. As cheap as the Raspberry Pi is, it is far better to wear out a cheap 60p switch that the microUSB socket on the board.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

Pygar
Posts: 64
Joined: Fri Mar 23, 2012 3:31 pm

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 8:26 pm

I've got a power strip, with switch, just waiting for my power supply, due to arrive tomorrow... Basically, I found out nothing I'd got for my Pi, would work. The TV had no HDMI, the power supply I'd bought was anemic... I've got my fingers crossed for the SD card I readied...
"Mean kitty, bad kitty
Vicious ball of fur!
Fighty kitty, bitey kitty!
Grrr! Grrr! Grrr!"

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 8:30 pm

SSilver2k2 wrote:With that said, I've been getting tired unplugging and replugging in the microUSB power supply just to reset something when I inevitably cause my Pi to lockup.
Switch is a good idea and I like it, but you'd be far less likely to corrupt your SD card if you used another computer, or even a smartphone to ssh in and reboot or shutdown properly when it locks up.
Alex Eames RasPi.TV, RasP.iO

SSilver2k2
Posts: 179
Joined: Wed Jun 06, 2012 1:51 am
Location: United States
Contact: Website AOL

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 8:47 pm

alexeames wrote:
SSilver2k2 wrote:With that said, I've been getting tired unplugging and replugging in the microUSB power supply just to reset something when I inevitably cause my Pi to lockup.
Switch is a good idea and I like it, but you'd be far less likely to corrupt your SD card if you used another computer, or even a smartphone to ssh in and reboot or shutdown properly when it locks up.
Wheres the fun in that :lol: With some of the SDL apps i've been using, when I do a reboot through SSH, my monitor doesn't always resync for some reason. The only true fix is doing a hard reboot.

I also have a couple SD cards ready in various states and I try to backup my SD cards regularly especially after a breakthrough compile :)

Good thought though. I haven't had a card go corrupt yet, but I'll be cautious about that.
My blog of various geeky things - http://blog.sheasilverman.com
PiPLAY - http://piplay.org
DeskCade.com - Mini Raspberry Pi Arcade Cabinet

forumisto
Posts: 386
Joined: Fri Mar 16, 2012 8:41 am

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 9:00 pm

I think to put the switch before (between the wall and) the power adaptor is better idea.
When RPi is off, the power adaptor does not work.

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 9:16 pm

It's interesting to know that somebody else has had the lockup with SDL, I too have had the same problem and need to do a hard re-boot. Perhaps we should start another thread about this problem; I thought it was my code! Still the switch idea is a good one.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: RaspberryPi On/Off Switch Idea

Tue Jun 19, 2012 10:12 pm

For better or worse, there is a belief out there that ARM based "computers" (quoted especially because the underlying theme here is that we don't know whether these things are really computers or not - are they computers or are they appliances?) don't need power switches.

I say this all this way, because I have another ARM based Linux box that, like the Pi, also has no power switch and the same issue arises: Do I risk OS corruption if I don't shut down "properly"?
Note, incidentally, that on this other box, not only is there no power switch, but there is no "shutdown" command (not even "init 0", because "init" is really busybox). There is a "reboot" command, but no "shutdown" command.

I should also point out that I rarely shutdown any of my computers, anyway, since I always just run them until they crash (which, despite everyone's best intentions, does always, eventually, happen). So, in a way, the whole concept of "graceful shutdown" is moot for me anyway.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

gr3ener
Posts: 15
Joined: Thu Jun 14, 2012 2:02 am

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 12:40 am

I eventually want to come up with a way that if I press the shutdown button/switch, it'll send a halt command to the system, wait a few seconds (allowing the system to shutdown) then kill power. Seems like a fun little project to do.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 1:06 am

gr3ener wrote:I eventually want to come up with a way that if I press the shutdown button/switch, it'll send a halt command to the system, wait a few seconds (allowing the system to shutdown) then kill power. Seems like a fun little project to do.
Yeah. A couple of years ago (my, where does the time go!) I started working on something similar (but with many more features) using a PIC-based circuit and some MOSFETs. Got the hardware working with the help of a friend, but never quite got round to finishing the software-side (my first time learning PIC16 assembler).
It'd also be useful for the RasPi though, so maybe I'll try to resurrect it...

gr3ener
Posts: 15
Joined: Thu Jun 14, 2012 2:02 am

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 2:32 am

I'd imagine it wouldn't be too hard, some hardware creations running alongside some code connected via GPIO.

I'm about to start a project, but once I'm done I'm definitely going to be doing this!

MarkA
Posts: 15
Joined: Thu Jan 12, 2012 2:32 pm

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 8:11 am

Hi all, I usually lurk, as I have nothing of value to add, but hopefully this will be of interest.
I have been playing with a Teensy microcontroller recently for another application. It has some nice features, such as acting as a HID client (programmable keyboard), and the board has a usb socket ready for use. I could imagine that if it was plugged into the Raspberry Pi (via a hub?), fitted with a push button and the appropriate power circuitry, then pushing the button could cause the teensy to initiate an ssh connection and send the shutdown command. After a suitable pause it could turn the power off to both units. We could even get some leds to flash giving status information.

The teensy is small, relatively cheap and easy to program via arduino code. It has other functionality, such as serial port capability, pwm and many IP ports, so could have use in a number of projects.

If you did not want to kill the power, just send the shutdown command, then no additional circuitry would be required. I may even have a go myself this weekend.


User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 8:45 am

The only reason that you would want to shutdown a computer before removing the power, is if it wrote to disk (or SD) at various random times and, more so, if it cached disk writes until later. In general appliances such as routers, set-top boxes, washing machines and toasters do not do that; they only ever write configuration data and they only do that when you tell them to.

That Raspberry Pi, on the other hand, is a real general purpose computer. As such it has many tasks running simultaneously, any of which can write data at any time, and disk/SD writes may be cached. However the file system used has journaling, and it can usually be successfully recovered, so most of the time switching off the RaspPi will not cause any problem.

In the case of the RaspPi there is one further failure mode. The SD card is smart and handles wear-leveling itself. Therefore it can be writing data to the flash memory and moving pointers to data around a short time after data is written to the SD. If power is removed during this process, it is possible to corrupt the SD so thoroughly that no computer can recover it.

Therefore the Raspberry Pi must be shut-down before power is removed. You wont usually have a problem not doing so, but Murphy's law ensures that you might go six months or a year without problems, until that one day when you have the most to lose...

It is possible, with two relays and two GPIO lines, to switch the RaspPi on when a switch is closed and for is to shut-down gracefully when the switch is opened. A separate microcontroller is not necessary.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 9:27 am

Oops, missed the edit window. Here's the circuit diagram.
Image (click to enlarge)

R2 must have a 3.3V coil and be capable of being switched by 5mA or so. R1 has a coil suitable for whatever you are switching -- 5V or mains.

GND is the same as 0V if you are switching 5V. It is Neutral if you are switching mains.

Don't fool with mains unless you know what you are doing. It would be easy to kill your RaspPi or yourself.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Wed Jun 20, 2012 10:30 am

The reason that my project uses/used a separate microcontroller, is so that the microcontroller can also restore power :) (which is a function I'd planned to use in several different ways)

SSilver2k2
Posts: 179
Joined: Wed Jun 06, 2012 1:51 am
Location: United States
Contact: Website AOL

Re: RaspberryPi On/Off Switch Idea

Mon Jul 23, 2012 1:31 am

I know it's been a while but I had time today and actually made the cable. It works! :lol:

Image

http://blog.sheasilverman.com/?p=152
My blog of various geeky things - http://blog.sheasilverman.com
PiPLAY - http://piplay.org
DeskCade.com - Mini Raspberry Pi Arcade Cabinet

User avatar
bonelifer
Posts: 128
Joined: Thu Jan 12, 2012 4:01 am
Location: http://www.phpbb.com/community/memberli ... e&u=147848
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Mon Jul 23, 2012 1:54 am

If anyone doesn't want to build it themselves, I found this:
http://www.amazon.com/gp/product/B005VS ... 01_s00_i02

Add a few adapters and no need to cut wires.

donde
Posts: 79
Joined: Sat Feb 18, 2012 3:57 am

Re: RaspberryPi On/Off Switch Idea

Mon Jul 23, 2012 2:08 am

I think to put the switch before (between the wall and) the power adaptor is better idea.
When RPi is off, the power adaptor does not work.
I think it would be better to plug in the PS into the AC first. And then plug it into R-Pi. This is because a voltage spike might be generated the instant it is switched on.

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Mon Jul 23, 2012 3:34 pm

rurwin,

While the schematic is nice, there are some things you need to provide newbies who aren't already experienced with this level of electronics.

The standard nomenclature for relays on schematics is the letter "U", not "R", which becomes obvious as you get into your explanation referring to relays R1 and R2 ... while there are also resistors R1 and R2 in the schematic. You should additionally include the wiring of the coils in the relays since they are being switched as part of the circuit, if my cursory glance at the schematic is correct.

I haven't looked closely to see if it's the case in your schematic, but, you really need to be careful with relay coils connected to digital circuits. As you may be aware, but, newbies won't, when a coil (in a relay in this case) is deenergized, the magnetic field in a coil collapses and a very brief, potentially high voltage (and corresponding current, depending on the resistance down-circuit) is instantaneously introduced. If that voltage/current is presented directly across digital semiconductor junctions, it will blow them out if they're not protected by a "crowbar" circuit (essentially a Zener diode that begins conducting above its rated trigger voltage, shunting the current to ground).

Some digital devices have such protection built into their I/O ports, but, I seem to recall that many, if not all, of the GPIO pins are not so protected on the BCM2835. Given the minimal relative cost differential between semiconductor components vs. relays, I would go with a silicon-controlled rectifier (SCR) to turn DC power on/off, or a Triac (two SCRs "nose-to-tail" in parallel with a common gate input in one package) to turn AC power on/off. Your warnings about AC power are very applicable - AC circuits, especially those wired in proximity to low-voltage circuits, should only be worked on by those who really know what they're doing and are well-versed in the National Electric Code requirements to prevent electrical and mechanical shock, short-circuit, and thermal issues. Opto-isolators must be used to keep such AC and DC circuits separated - there is no truly safe alternative.

Also, it would be very helpful to include the scripts that perform the actual shutdown, etc. Other than those semi-minor gripes, a good solution to the posed problem.
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Mon Jul 23, 2012 9:16 pm

Hi Jim, congratulations on the Jam, and I hope you get your RaspPi back.

The only resistor in the circuit is not given a name, just a value. While not using R for relay would probably confuse the literati less, it would probably confuse newbies more. YMMV.

I take your point about relay diodes; there should probably be some. In my defence I have never needed or used them for 6522 VIA ports on the BBC Micro and such. Did those have more protection than the RaspPi? I don't know. Would a 3.3V 5mA relay develop enough back EMF to be a problem? I don't know that either, I suspect you'd probably get away with it, but a reversed diode across the coil would probably be worth the effort to fit. There is an old saying "Beware The Programmer Who Carries A Screwdriver." I am he.

Regarding the scripts, if I had written any I would share them. I did look into how Debian handles shutdown, and I found the relevant halt utility (not the halt command) was or could be a Bash script. Similarly a script could be used for the polling utility. They would be simple matters of echo'ing to and cat'ing from the GPIO lines via the GPIO driver and the Linux filesystem.

A triac would work to turn the AC on and off, but then your "safety extra-low voltage" circuit would be intimately connected to your mains circuit, since it requires a voltage relative to the A1 pin to switch. One mis-wired extension cable and your RaspPi would be live. That is of course correctable, but not I think cheaply or simply in comparison to a relay. Neither a triac nor an SCR would work to switch DC, since it would never turn off. The advantages of relays are they are simple, easy to understand, they have zero leakage current and almost zero on resistance, and they very effectively isolate the controlling circuit from the controlled circuit. Also you can see and hear them operate and they very rarely half-operate.

User avatar
Jim Manley
Posts: 1600
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, and Powell, Wyoming, USA, plus The Universe
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Tue Jul 24, 2012 8:49 am

rurwin wrote:While not using R for relay would probably confuse the literati less, it would probably confuse newbies more. YMMV.
That's why God, or some other entity smarter than us, made parts lists/bills of materials (BOMs for newbies).
I take your point about relay diodes; there should probably be some. In my defence I have never needed or used them for 6522 VIA ports on the BBC Micro and such. Did those have more protection than the RaspPi? I don't know.

You could almost see the junctions with the naked eye in BBC Micro era components! The early days of TTL were much different from low-power Schottky and today's CMOS (complementary metal oxide semiconductor for newbies - much lower power and higher density) technology
Would a 3.3V 5mA relay develop enough back EMF to be a problem? I don't know that either, I suspect you'd probably get away with it, but a reversed diode across the coil would probably be worth the effort to fit. There is an old saying "Beware The Programmer Who Carries A Screwdriver." I am he.
Well, I shouldn't have to tell you what Murphy thinks about this ... hey, get away from that, you drunken kibitzer! :D
Regarding the scripts, if I had written any I would share them. I did look into how Debian handles shutdown, and I found the relevant halt utility (not the halt command) was or could be a Bash script. Similarly a script could be used for the polling utility. They would be simple matters of echo'ing to and cat'ing from the GPIO lines via the GPIO driver and the Linux filesystem.
These are easy for us to imagine, but, I think posters and commenters need to remember who the target audience of our babbling really is. I'm a bit torn between being too pedestrian (hence my verbosity) and compact and confusing, and I always wind up landing in the former camp. I don't think of it as spoon-feeding, it's more like appetite-whetting. Give them something simple that works and then hint at expansion options left to the reader. It's amazing how far and high kids can leap once they realize that tripping and falling is not only an acceptable option, it's encouraged. Getting them over that first hump is the challenge - once they make it over, they go on autopilot.
A triac would work to turn the AC on and off, but then your "safety extra-low voltage" circuit would be intimately connected to your mains circuit, since it requires a voltage relative to the A1 pin to switch. One mis-wired extension cable and your RaspPi would be live. That is of course correctable, but not I think cheaply or simply in comparison to a relay.
That's why I said opto-isolators were absolutely required.
Neither a triac nor an SCR would work to switch DC, since it would never turn off.
An SCR would work (a Triac would just be a waste of money, since half of it would never see a reverse-polarity voltage), but, you need an appropriate RC network to condition the transitions, which is what you're actually interested in, not the voltage itself. The active and passive components pretty much equal in cost what's needed to get relays to work while ensuring no back EMF problems.
The advantages of relays are they are simple, easy to understand, they have zero leakage current and almost zero on resistance, and they very effectively isolate the controlling circuit from the controlled circuit. Also you can see and hear them operate and they very rarely half-operate.
Relays wear out much sooner than active components (albeit over years even with frequent operations using today's higher-quality manufacturing). Depending on the configuration, hold current in spring-returned (momentary-contact) relays is much higher than trigger/gate current in CMOS active components (which only require a voltage at fractions of a microamp). If you can design to use bistable relays, that's fine, but, they're susceptible to vibration, so applications can be limited.

Stick LEDs and/or piezo transducers in the circuits to see/hear what's going on - they cost essentially nothing these days, and will last decades in continuous use with no mechanical issues (probably centuries in intermittent use). I'm surprised we haven't seen packages with LEDs built into the top surfaces of packages for simpler microcontrollers/microprocessors long before now.

At least that's my story, and I'm stickin' to it! ;)
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!

Wolfram23
Posts: 73
Joined: Thu Jul 19, 2012 6:50 pm

Re: RaspberryPi On/Off Switch Idea

Tue Jul 24, 2012 4:20 pm

Interesting discussion here. As someone who knows relatively little about electronics, I can confirm that most of what you're talking about is going over my head (although I understand the gist of it).

I'm planning to put my RPi in a car, so it's already on a 12V DC circuit. Trying to figure out the most efficient 12V to 5V adapter, as well as how to auto sleep/wake the RPi.

The idea I have is to use the 12V to 5V on the main 12V Battery line (constant power), with a second adapter 12V to 5V (or does it need to be 3.3V?) on the ignition line. Then when ignition goes live, the RPi wakes up, and when it goes dead, the RPi goes to sleep. I'd also consider a physical switch in case I won't be driving my car for more than several days, just to minimize battery drain.

Anyway I'm sure the hardwire side of it is just a little more reading away, but as for software interfacing with the GPIO pins, I really have no idea. Would be great if you guys can expand on that.

ksangeelee
Posts: 192
Joined: Sun Dec 25, 2011 5:25 pm
Location: Edinburgh, UK
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Wed Jul 25, 2012 1:13 am

rurwin wrote: R2 must have a 3.3V coil and be capable of being switched by 5mA or so.
Do you know any specific relays that can switch with such low currents? The lowest I've been able to find needs something like 30ma at 3.3V (or was it 40? - in any case, much higher).

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: RaspberryPi On/Off Switch Idea

Wed Jul 25, 2012 5:55 am

I'm a theoretician. ;-)

No. I did browse the RS site, and like you I found only 30mA or so. It may need a transistor.

Return to “General discussion”