Raspberry Pi UPS with safe shutdown

It looks like I might finally have a functioning UPS. I say finally because it's taken way longer than planned due to both "technical difficulties" and resource availability. We're approaching exam season so my computer and bench are hijacked daily by the youngest of my offspring and I can't complain about that. This does, however, mean investigation of any technical difficulties spans weeks.

That's the excuses out of the way. Here's the short version of what happened:
  • I needed a UPS for the Raspberry Pi to handle sudden power loss and prevent file corruption.
  • The "Wemo 18650 Battery Shield" will output whilst taking power and it's cheap. I like cheap.
"Wemo 18650 Battery Shield"
  • The RPi can't be trusted to shut it's own power off so I looked for the smallest, cheapest Arduino-type board (to minimise faffing) to handle the delay-if-then logic. I got a couple of DigiSpark boards (clones as it turned out) and cobbled together the code and connections.
  • Saw some odd behaviour, maybe the DigiSpark board isn't what I need.
  • Worked around the issue, circuit and code seem to work on the bench until the RPi is actually connected (instead of manually simulating the I/O signals).
  • (some time later) Sick of the DigiSpark; I switch to the trusty Nano, then add some traffic-light LEDs for indication and because I can.
  • More odd behaviour; looks like back-feeding through the ┬ÁC inputs. I should really disconnect the GND too.
  • I transplant a small DPDT relay onto the opto-isolated "arduino" relay board and re-wire again to also break the GND this time.
Twenty-third-time lucky?
  • Dammit! Killed a Pi pin by forgetting 5V > 3V3. Moved to next GPIO and added a pot.div.
  • Success!?! Tested the UPS with the RPi two or three times, mounted both in the head and moved on to power distribution but I'm waiting on some parts that got delayed in transit, so I'm using the time to write this. Below is the schematic I did a few days(?) ago.
UPS diagram (not showing GPIO potential divider!)

Still here? Here's some notes:

A "18650 Shield" is used to maintain power to the Raspberry Pi upon removal of the main power (e-stop) allowing safe shutdown after main power-off. It uses GPIOs to trigger the RPi to shutdown, waits for the RPi to signal it is (almost) complete before opening a relay to disconnect the UPS power and prevent deep discharge of the battery.


3 - RPi_UP (DI), RPi GPIO output - high after boot, low just before complete shutdown.
2 - RPi_SD (DO), RPi GPIO input - active-high to trigger shutdown of RPi (through pot.div; 5V >> 3V3).
A0 - ADC to 18650 battery, 4V - disconnect load from battery when voltage drops.
4 - Main Power present/not present (DI).
5 - Keep alive/immediate select switch (DI)*
6 - Relay (DO) - active-high opto-isolated (N.O. contacts). Opens to disconnect battery.
7 - Amber UP LED
8 - Red SD LED
9 - Green MPP LED

*Mode selection input:
Keep alive = 0V   - waits until battery is low before shutting down and disconnecting.
Keep alive = N.C. - immediate safe shutdown and disconnect.

Raspberry Pi

RPi signals to UPS when it is up. UPS tells Pi to shutdown and waits for RPi_UP to go low before removing power.
RPi_UP=21       # GPIO output - high after boot, goes low a few seconds before complete shutdown.
RPi_SD=17       # GPIO input - active-high to trigger shutdown of RPi (through pot.div; 5V >> 3V3).

I might revisit this, to fix the low battery check, but for now...

Related: UPS


Popular Posts