Kraggy123
Posts: 34
Joined: Wed Feb 28, 2018 5:50 pm

Code to power down Pi

Sat Mar 03, 2018 6:32 pm

Hi I for my current project I need a code that when matched with a switch, will put the Raspberry Pi in a low power state and then when pressed again will reboot and power back up the Pi(without turning off the Pi completely for obvious reasons).

I've tried and failed to produce a script that would do this.

If anyone could help I would really really appreciate it.

pcmanbob
Posts: 9885
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Code to power down Pi

Sat Mar 03, 2018 7:25 pm

The pi does not have a low power stated it's either on or off.

So your either have the OS running or you the OS shutdown.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Kraggy123
Posts: 34
Joined: Wed Feb 28, 2018 5:50 pm

Re: Code to power down Pi

Sat Mar 03, 2018 7:56 pm

I thought when you manually click shut down on the Pi it went into a low power mode? As the LEDS are still light up and it is still being powered; rather than completely cutting off power.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Code to power down Pi

Sat Mar 03, 2018 8:12 pm

Kraggy123 wrote:
Sat Mar 03, 2018 7:56 pm
I thought when you manually click shut down on the Pi it went into a low power mode? As the LEDS are still light up and it is still being powered; rather than completely cutting off power.
Yes, obviously, there is a "low power state".

What previous posters meant to say is that there is no "low power state" from which you can recover. And by "recover", they mean: get back up and running without doing a full power cycle/reboot.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Kraggy123
Posts: 34
Joined: Wed Feb 28, 2018 5:50 pm

Re: Code to power down Pi

Sat Mar 03, 2018 8:27 pm

Ah ok, I don't mind it completely rebooting, simply need a code to do it.

klricks
Posts: 7234
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Code to power down Pi

Sat Mar 03, 2018 8:50 pm

You can add a single momentary type switch between pin 5 and 6 which will safely shutdown the RPi and then start it up again when the button is pressed again.
All without any script or coding.
In order to enable this feature add this line to /boot/config.txt

Code: Select all

dtoverlay=gpio-shutdown
As mentioned when shutdown, the OS and of course your script is not running. Though you could configure the RPi to start your script on boot.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

alphanumeric
Posts: 2650
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Code to power down Pi

Sun Mar 04, 2018 1:04 pm

Does this PIN 5 and 6 switch interfere with i2c in any way? Pin 5 is i2c SCL.

Kraggy123
Posts: 34
Joined: Wed Feb 28, 2018 5:50 pm

Re: Code to power down Pi

Sun Mar 04, 2018 4:49 pm

I added a button onto Pins 5 and 6 like you said and then proceeded to edit the config.text, except I can't seem to get it to work.
I'm confident its a problem on my part but I can't figure out where.

klricks
Posts: 7234
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Code to power down Pi

Sun Mar 04, 2018 10:01 pm

alphanumeric wrote:
Sun Mar 04, 2018 1:04 pm
Does this PIN 5 and 6 switch interfere with i2c in any way? Pin 5 is i2c SCL.
Probably does, but you can change the shutdown pin to something else.
For example this will change the shutdown pin to GPIO 26 (physical pin 37)

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=26
However you can't change the startup pin from GPIO 3 (physical pin 5). That always works regardless.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

klricks
Posts: 7234
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Code to power down Pi

Sun Mar 04, 2018 10:16 pm

Kraggy123 wrote:
Sun Mar 04, 2018 4:49 pm
I added a button onto Pins 5 and 6 like you said and then proceeded to edit the config.text, except I can't seem to get it to work.
I'm confident its a problem on my part but I can't figure out where.
You must reboot in normal way after making the change to config.txt
You need to have a fairly recent install of Raspbian Stretch.
If the overlay file: dtoverlay=gpio-shutdown is missing from /boot/overlays/ then the OS needs to be updated.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

RonR
Posts: 1559
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: Code to power down Pi

Mon Mar 05, 2018 8:55 am

klricks wrote:
Sun Mar 04, 2018 10:16 pm
You need to have a fairly recent install of Raspbian Stretch.
If the overlay file: dtoverlay=gpio-shutdown is missing from /boot/overlays/ then the OS needs to be updated.
For Jessie (and possibly early versions of Stretch), see:

viewtopic.php?t=191145&p=1200106#p1200106

alphanumeric
Posts: 2650
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Code to power down Pi

Mon Mar 05, 2018 9:57 am

klricks wrote:
Sun Mar 04, 2018 10:01 pm
alphanumeric wrote:
Sun Mar 04, 2018 1:04 pm
Does this PIN 5 and 6 switch interfere with i2c in any way? Pin 5 is i2c SCL.
Probably does, but you can change the shutdown pin to something else.
For example this will change the shutdown pin to GPIO 26 (physical pin 37)

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=26
However you can't change the startup pin from GPIO 3 (physical pin 5). That always works regardless.
Great. thanks for the extra info.

timrowledge
Posts: 1354
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Code to power down Pi

Thu Mar 08, 2018 9:52 pm

I've been trying to get this pin5/6 (and really, aren't we supposed to use the BCM nubers for these things? As in GPIO3 & GND) with some confusion.

Initally it simply wouldn't work at all and eventually I thought to look in `dmesg` to see if it listed any helpful errors. It doesn't often do any good but just often enough that I live in hope. Anyway, this time I saw -

Code: Select all

pi@motioneye:~ $ dmesg | grep shutdown
[    3.424604] pinctrl-bcm2835 3f200000.gpio: pin gpio3 already requested by 3f804000.i2c; cannot claim for soc:shutdown_button
[    3.424622] pinctrl-bcm2835 3f200000.gpio: pin-3 (soc:shutdown_button) status -22
[    3.424641] gpio-keys soc:shutdown_button: Error applying setting, reverse things back
[    3.424670] gpio-keys: probe of soc:shutdown_button failed with error -22
which lead me to try disabling i2c in raspi-config. That made the button work - but left me without i2c.
I wondered if it might be an ordering thing and so moved the dtoverlay=gpio-shutdown higher in the config.txt file. No luck.

So far it seems you can have a button or i2c. I read a hint that GPIO1 might also trigger a restart (it doesn't) but I did move my shutdown button to GPIO1/gnd and edited the dtoverlay to be

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=1
which worked ok for the shutdown. It turns out that another button on GPIO3/gnd is ok to do the restart even with i2c enabled, so one option is to have two buttons. It certainly offers more blinkenlightsparkle if you have lighted buttons.

The obvious question is whether one can simply combine the wiring and have a single button that would connect GPIO3, GPIO1 and a gnd? If your Pi is burbling away with some i2c device attached and you short the i2c clock line at the same time as GPIO1 would it do anything nasty? Seems like it might be a bit rude to me...

It's really unfortunate that one useful thing is prevented by another useful thing.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
Milliways
Posts: 570
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: Code to power down Pi

Tue Mar 20, 2018 3:10 am

klricks wrote:
Sun Mar 04, 2018 10:01 pm
alphanumeric wrote:
Sun Mar 04, 2018 1:04 pm
Does this PIN 5 and 6 switch interfere with i2c in any way? Pin 5 is i2c SCL.
Probably does, but you can change the shutdown pin to something else.
For example this will change the shutdown pin to GPIO 26 (physical pin 37)

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=26
However you can't change the startup pin from GPIO 3 (physical pin 5). That always works regardless.
Does not ALWAYS work - gpio-poweroff disables restart.

Return to “Advanced users”