Power detection

I connected a Raspberry Pi to the UPS-Lite that I’d identified in the concepts blog post. Right out of the box I could switch off power and the Pi would stay alive!

UPS-Lite schematic

I moved on to detecting the power state changes with the Pi: I wanted to send MMQT messages with my AWS IoT client when these events occurred.

Documentation on the UPS-Lite was thin on the ground, but I’d found a tweet from the manufacturer that indicated I could detect this on the Pi’s GPIO7:

I selected the higher-level gpiozero python library over RPi.GPIO . It appears to be the recommended library these days, and critically for me, it has a simple edge detection API. This allowed me to declaratively assign callbacks to be executed when an input pin changes value - tidy.

from gpiozero import DigitalInputDevice  # noqa: E501, pylint: disable=import-error,import-outside-toplevel
_POWER_PIN = 4  # gpiozero uses Broadcom pin numbering

power_status_input = DigitalInputDevice(_POWER_PIN, bounce_time=0.2)
power_status_input.when_activated = _publish_update
power_status_input.when_deactivated = _publish_update

def _publish_update(self, device: DigitalInputDevice) -> None:
    print("Status changed!")

I tore my hair out trying to get that pin detection to work. I found the manufacturer’s GitHub repo , but the example code demonstrated reading the voltage/capacity of the battery, not the power status.

I double-checked the pin numbering system , and confirmed it had to be pin BCM4/WiringPi7, not least because I could see BCM7 wasn’t connected to by a pogo pin.

I manually read the pin a few times and whilst knocking it about found the voltage seemed to be floating: sometimes it was high, and sometimes low. Going really spare I got my multi-meter out and confirmed it.

I studied the owners manual, written in Chinese, pouring over the same wiring schematic I’d found in the tweet: everything looked kosher.

Finally, I found a translated manual that revealed my error:

Also UPS-Lite insertion detecting function with a power adaptor, the insertion of the power pi io4 (BCM number) detects the high level, when pulled low, enabling the weld shorting function requires two back of the UPS disc, as shown below in detail.

UPS-Lite solder pads

I needed to short the two solder pads on the back of the board to have the pogo pin carry the power status signal! If my electronics knowledge had been better this is actually obvious from the schematic:

UPS-Lite schematic

…I’m telling you, I felt like a right fool.