Auto shutdown (when USB supply voltage drops)

hello, I’m making a basic adhoc USB UPS using 4 x NiMH AA batteries to reduce the likelihood of SD corruption from main power trips. Is there a way of to automatically shut down the pi when the voltage drops tripping the ‘not enough power’ event? (and then subsequently getting it to reboot when the voltage recovers to 5V) thanks

Hi Matthew, are you suggesting using the low/under voltage detection on pi to trigger the shutdown? It is a tempting solution because everything required is present already for experimentation, my concern would be how consistent is the behaviour, or is it too late potentially and it would be more reliable to monitor the power supply to detect the switch from PSU to batteries and signal the Pi via a GPIO pin?

Take a look at the gpio-shutdown overlay, IIRC the low voltage detection on Pi is also indicated on one of the GPIO pins.

Edit: Hmmmm, I suspect this is Pi variant and kernel version specific, Low voltage detection, read voltage level - Raspberry Pi Forums

If it is not already an option on the emonpi PCB dare I suggest considering supply monitoring and perhaps LiPo battery management if the board design is revisited?

(my) Objective: prevent SD card corruption during main power supply outage
Method: auto shutdown when under/low voltage is occurring on USB supply (bellow 4.63V) The hardware is outside the unit, not inside. External supply current can be maintain by batteries of any chemistry, supply voltage will be deliberately lowered to about 4.5V to trigger event when main power supply is lost. (some well placed 3A schottky diodes can do this on their own)
Bonus: When supply is restored to 5V, would like it to automatically reboot.
Notes: I’m relatively new to this pi hardware, I’m mainly a hardware technician rather than software, but can string a few characters together when need be! Hoping to find something already done on the internet so I can just copy and paste it somewhere… Or at least find a way to submit a request to the devs to get an option released in the Emoncms so I can play with it on the admin tab.

edit: found this, no idea where to put:

The author of the post you linked refers to “cron” also referred to as crontab, which is a mechanism for running commands on a regular basis, see crontab(5) — cron — Debian jessie — Debian Manpages

They have placed the script under the /root/ directory however so long as the path is consistent with the command path in the /etc/crontab file and the directory and file permissions are set appropriately then you can place the script anywhere within reason.

The problem with using this approach is it polls which introduces two less desirable features, 1) depending upon the frequency at which the power_monitor script is executed by cron a delay is introduced between low voltage occurring and the system becoming aware and performing the shutdown, 2) the more frequent the polling to check for low voltage the more work the system is performing. You could still follow this approach as a proof of concept / get you going.

An event driven solution would be optimal / desirable, it would probably be worth the devs looking into how the low voltage desktop indicator is triggered in Raspbian / PiOS and then integrate this into the systemd shutdown logic (I haven’t looked at the emoncms image yet as I’m a relatively new user, this aspect is more Linux distro specific)

Edit: The kernel driver Rapberrypi-hwmon abstracts the HW differences, and polls the hardware, but this then calls hwmon_notify_event(), which can then be handled in user space, I need a proper keyboard and screen to dig further, it’s painful on a tablet. I shall try to bottom this aspect out ASAP.

Polling once a minute would probably be adequate. (With event driven, maybe add a long delay though with bigger batteries to allow for frequent power outages.) I have access to my emonpi via the SSH console on my PC but not exactly quite sure what to do next… Don’t really want to start entering commands and get lost half way through needing to be bailed out by not realising some of it is not correct.
Questions: I’m guessing the Emoncms shutdown button in the admin tab does exactly the same thing to what I’m trying to do here? Or is there more to the shut down that needs to be considered because of the other boards attached to it? Will doing this effect the firmware or updating? thanks
Bonus: a step by step guide would be awesome!

1 Like

Yes, it, and the hardware one via the display, do a controlled stop of the Raspberry Pi in order that a write to the SD card is not in progress when the power is removed. The Atmel ATMega328P (the ‘emon’ part) isn’t shut down and can safely have the power removed at any time.

OK, the hardware bits are coming in soon, I can begin assembly and conducting checks today.
This is the basic circuit but will need to make refinements as I go along:
Feedback welcome! (I’m using KiCad)

(edit: improved image clarity)
(edit2: updated layout)

Hello, I’ve finished it:

  • The inline Schottky diode from 5V supply was removed as it was causing abnormal voltage drops and no reverse current was detected back into the adapter that came with the emonPi Energy Monitor.
  • I added a switch to isolate the batteries. Revised circuit above.
  • There is no low voltage cut out so if left unpowered for a prolonged period will destroy the batteries. (I’ve added a bonus active circuit below with MOSFET low voltage lockout option if you need it, just change the Zener to what ever you want, use the lower activation threshold in the datasheet for them.)
  • I think this cost less than £50 for parts.

Screenshot 2022-03-15 165800

1 Like

Apologies for my lack of follow ups Matthew, I am juggling a change of job and attempting to install my own emonpi, though I have pulled the source and taken a look at the raspberrypi-hwmon driver. Hopefully it is a case of add a udev rule to trigger our shutdown script but if not then we can add our own code to monitor the hwmon sysfs entries.