2

I will be using the Raspbian Lite OS.

I want my Users to be able to switch the device on/off.

I am using a battery source for the main application.

I was thinking of providing kiosk browser UI (which I found some sample code for) to allow the user to click on my virtual button to shut the device down when not in use.

But, 'durrh' just occurred how can i get my user to switch the device back on? Or should I be looking at some 'sleep' functionality rather than looking to completely power off?

Seamus
  • 18,728
  • 2
  • 27
  • 57

2 Answers2

3

First, the bad news: Currently, only the RPi4B has what might be considered a "sleep mode". And at 40mA, or approximately 5 watt-hours per day, this could only be considered "sleep mode" if you're still living in the era of bag phones. While the Raspberry Pi doesn't use a great deal of power when it's running, 72 watt-hours per day is beyond the capacity of batteries typically used for "mobile" (or even transportable) devices. YMMV, but you should compare these power consumption figures against your battery capacity.

There are other "tweaks" available to further reduce power consumption; e.g. some hardware can be disabled via configuration file, and clock speed can be reduced for less demanding applications. Here are some specific suggestions on reducing power consumption, and here are some more. If you're interested in minimizing power consumption in this way, determine what hardware resources you can live without, and edit your question (or post a new one) - we'll try to help.

I'll organize my answer to your question under 3 headings:

1. Brute force power removal

As far as allowing your users to switch the device on and off, perhaps the simplest solution is to wire your battery lead (positive side) through a switch. You can buy a switch that is said to do this from several vendors; e.g. this one from Amazon. The advantage this solution has over the one below is that power consumption is reduced to ZERO. However, be aware that there are a couple of cautionary notes for this approach:

  1. Powering off your RPi by removing power may create file integrity issues on your SD card, and is not considered good practice. In any event, fsck will be invoked on the next boot sequence, which will require additional time to boot.
  2. Some of the hardware sold in the "hobbyist market" is not manufactured to the highest standards. Be aware of this, and look out for new issues with your RPi after adding new hardware.

2. Reduce power consumption via shutdown command

A better alternative to the brute-force power switch described in the previous paragraph is to reduce power using (one of the) halt/poweroff/shutdown commands, and then toggle the state of GPIO3 pin with a pushbutton switch to restart the Pi. However, you should know that the RPi will continue to use power after the shutdown command takes effect (see Note 1 below). Basically, this solution takes the following steps:

  1. Halt the RPi via the command: sudo shutdown now. (see Note 2 below)

  2. Restart the RPi by connecting pin GPIO3 to Ground. This can be done using a "momentary" pushbutton switch that shorts GPIO3 to GROUND.

One clever way to configure the momentary pushbutton switch to "toggle" the RPi between OFF and ON is to monitor the state of GPIO3 in software, and execute a script that runs the shutdown command above to halt the system. Once the system is halted, the next push of the toggle button will restart the RPi. This technique is covered in more detail here.

Of course there are other ways to halt the system, and then use the momentary pushbutton to restart it. For example, you could add a line in your crontab file to shut the RPi down at a designated time (at 9:00 PM for example), then turn it on again by pressing the momentary pushbutton switch. Or you could write a script to monitor usage of some resource, and shutdown based on that usage.

Update: If you have an RPi4B, there are additional options that will further reduce power consumption in shutdown mode.

3. Add hardware to minimize RPi energy consumption

There are other solutions that, for example, use external hardware - even micro-controllers (e.g. Arduino) - to remove power from the RPi when it's not needed. These solutions do not reduce the RPi's power requirements, but they can greatly reduce its energy consumption; i.e. power over time.

Generally speaking, these energy-reduction solutions invoke a shutdown of the OS to preserve filesystem integrity, followed by disconnecting the RPi's external power source to . As of this writing, an automated re-start of the RPi is possible only from an external source; e.g. realtime clocks, sensors or other external systems (incl. "HATs").

Energy-reduction solutions are available as commercial products, or perhaps more usefully as DIY solutions that allow customization for a particular use-case:

4. In closing...

Your question has probed the interesting topic of power management on the Raspberry Pi. The holy grail of RPi power management is battery operation, but given its current hardware design this remains a challenge for many applications. However, demand from the user community, clever solutions from developers/users, and The Foundation's gradual improvements to their proprietary firmware are making useful inroads. In the meantime, a number of solutions that have been developed - some commercial and some DIY.

Suggest you try something simple to start, then build on that. You can post specific follow-up questions once you've got the basic functionality working.

Notes-


NOTE 1: How much power does the RPi consume after entering shutdown mode"?

  • In general, this depends upon which model of RPi, but...
  • For my RPi 3B+, I measure 80-90mA
  • For my RPi 4B, I measured 40-370mA.
  • Others have made measurements - for example

NOTE 2: Although halt and shutdown commands are implemented somewhat differently, there is no difference in power consumption once the machine is in this state.

Seamus
  • 18,728
  • 2
  • 27
  • 57
  • If the RasPi is in halt state (after shutdown) does it consume power? May it be possible that you add this info to the answer? – Ingo Dec 21 '18 at 18:44
  • thanks for such a comprehensive answer I did look at the GPIO route + script before and purchased the components. I was looking at all possibilities as well though. I think the way I prefer to go is to minimise what is running and 'sleep' that way. thanks again :) –  Dec 21 '18 at 18:52
  • 1
    I'm worried using the cable switch might encourage the user to regard that as the proper way to turn the device off, which all by itself it isn't. Not shutting the OS down properly will: 1) Slow your boot time because the root filesystem must then be checked when the pi is switched on again, 2) Significantly increase the risk of arbitrary filesystem corruption, which may or may not be fixable by the boot fs check. – goldilocks Dec 21 '18 at 18:57
  • @goldilocks: That's a valid point; I'll add a cautionary note. – Seamus Dec 21 '18 at 19:16
  • BTW: I have a few of those cables that came with Anker 5V 2.5A supplies, and they are all bad in the sense that no matter what supply they are used with, I get continuous undervolting on a Pi 3 (the Anker supplies themselves are also subpar, no matter what cable they are used with). Despite being sold "for the Raspberry Pi", there's some 1 star reviews there that reflect this too. – goldilocks Dec 21 '18 at 19:20
  • @Ingo: Good question. I'll edit my answer, and try to address this. Note however that `halt` and `shutdown` are not the same state, and power consumption is different (`halt` has much higher power draw in general). That said, I've not found an "official" spec on power consumption in shutdown mode, only anecdotal data. – Seamus Dec 21 '18 at 19:22
  • @goldilocks: Interesting :) what do you use them for? – Seamus Dec 21 '18 at 19:23
  • For my understanding shutdown is an action and halt (halted) is a state of the CPU. – Ingo Dec 21 '18 at 19:31
  • @Ingo: not according to [this](https://www.raspberrypi.org/forums/viewtopic.php?p=932091#p932091) – Seamus Dec 21 '18 at 19:42
  • Seems the shutdown action triggers the halt state of the CPU and the halt action of the OS doesn't trigger it ;-) – Ingo Dec 21 '18 at 20:08
  • @Seamus Not powering Pi 3's ;) I think I probably got the first one and used it with a single core machine, it was fine for that, so I got another one before I realized there was a problem (which is a shame because those switches are handy, and the power supplies were not expensive). – goldilocks Dec 21 '18 at 20:26
  • I'm sad now I thought the story behind this: https://en.wikipedia.org/wiki/Halt_and_Catch_Fire#Etymology ...involved actual magic smoke. To implement it on a Pi all you need is a jumper wire >_ – goldilocks Dec 21 '18 at 20:31
  • @Ingo: Hope I've covered your questions in my revisions; please advise. – Seamus Dec 23 '18 at 00:26
  • Just checked back in and even more interesting stuff to try and evaluate. Over the next few days I will see what works for me and will post what type of app i am running to give context as my accepted way may not be right for some one else. Thanks again and will post back soon, –  Dec 23 '18 at 08:08
  • Yes, all clear. What I referenced to was the [HLT instruction of a CPU](https://www.hardwaresecrets.com/everything-you-need-to-know-about-the-cpu-c-states-power-saving-modes/2/) for power saving. My thought was that the `halt` command from linux triggers this state but it seems it is not the case as you show. But that's all academic and not important. – Ingo Dec 23 '18 at 09:18
1

To be able to save battery power you will need additional hardware like a wittypi2 http://www.uugear.com/product/wittypi2/

Dougie
  • 4,940
  • 7
  • 14
  • 27