Out in Space

The Raspberry Pi Zero is a frugal little computer. But without a power socket, you might be surprised how quickly it can drain a battery. Active power management is the order of the day.

A Raspberry Pi Zero (W) can require upwards of 100mA – depending on what it is doing. This may not sound like much at first, but given a standard industrial LiPo cell rated at 2,200mAh, that computer won’t even last a day. This makes it all the more important to use the battery’s energy sparingly. HATs (Hardware Attached on Top) by PiJuice, which have been around for some time, have the circuitry to help you do this. In this article, we take a look at the PiJuice Zero, a pHAT that is optimized for the Raspberry Pi Zero (Figure 1).

b01_pijuice.tif
Figure 1: The PiJuice Zero HAT helps a Pi Zero with power management.

Use Cases

But first, let’s take a brief look at potential application scenarios for a power management HAT: A Raspberry Pi that is connected to a power outlet but needs to run continuously requires UPS protection (UPS being an uninterruptible power supply). If worst comes to worst, the back-up battery can step in at lightning speed and bridge the gap until the power grid is available again.

A second use case is in normal battery-based operation, for example when the Pi is running far away from civilization. The HAT must supply the board with a stable voltage, even if the battery voltage gradually drops. Ideally, the HAT also measures the charge status and reports it to the Pi. A monitoring program can then shut down the computer in good time, which in turn protects the battery from deep discharge. In both examples, there is also a requirement for sophisticated charging management. Instead of replacing the battery, in the second case a solar panel typically recharges the battery, while the UPS uses the power grid for this task.

Finally, the HAT must also be able to disconnect the Raspberry Pi from the power supply after shutdown, because all Pis prior to the Pi 5 still consume far too much power even after shutdown. Similarly, an automatic wake-up (which means reconnecting the battery) may be necessary, for example, timer-controlled at sunrise or when the battery is recharged. A Pi that runs for one hour a day instead of 24x7 will extend battery life from less than one day to almost three weeks.

Setup and Commissioning

Important information for a quick start can be found in the Quick Start Guide, while a detailed description of the hardware and software is available from the manufacturer’s GitHub repository. You will need a suitable rechargeable battery for the pHAT; connect it to the matching socket on the right (with pins marked VBAT, GND, and NTC; see Figure 1). The pHAT supports batteries with a built-in thermistor. If the battery becomes too hot during charging, the HAT switches off for safety reasons.

However, protected batteries are not necessarily easy to buy – of the usual suspects, only Conrad had them on offer in my country. No one here sells the manufacturer’s original batteries; the only source of supply we were able to find in the EU was Botland.de, a Polish distributor. Fortunately, the thermistor is optional, and batteries without this protection work just as well. Nevertheless, protection is recommended for unmonitored long-term use.

Apart from this hurdle, commissioning the basic functionality is child’s play: Just connect the HAT to the Pi and the battery to the HAT. If the Raspberry Pi is connected to the mains, you can use its micro USB socket and the matching socket on the HAT. As an alternative to the USB socket on the HAT, you can use the J4 connection directly to the right of it: You can feed in up to 10V that way, which is ideal for connecting solar panels.

Pressing the SW1 button on the left-hand side of the pHAT tells it to switch the power on. Pressing and holding it for 20 seconds triggers a hard power down. The system shuts down after 10 seconds, provided that you installed the appropriate software. Connection J5 for an additional button (called SW2 in the software) is located slightly lower down.

The P3 header also plays an interesting role. You could connect your own microcontroller here, or even just a sensor that can communicate with the chip on the HAT via the two IO1/IO2 pins. A microcontroller could even control the wake-up function via the IO2 pin if required.

Software

The basic functions such as UPS or battery operation are completely autonomous, irrespective of whether you install the additional software. However, the software offers options that can turn out to be essential, or simply more practical, for many applications. A clean shutdown at the push of a button is just one of many features. For example, you can use the software to configure the buttons if you want to reduce the button hold time for shutdown compared with the default.

The software is open source and available on GitHub. You do not need to download and install it manually, because it is also included in the operating system’s standard package sources. The packages you need are pijuice-base (which is required for headless operation) and pijuice-gui.

The GUI app has been excellently integrated into the desktop, and that includes an area in the status bar (Figure 2). The battery status is shown on the left, and the icon to the right launches the GUI. The user interface itself is made up of a series of tabs, each of which configures different aspects. As an example, Figure 3 shows wake-up time configuration. But be careful: Times must be configured in UTC.

b04_pijuice.tif
Figure 2: The PiJuice GUI application adds icons to the desktop’s status bar.
Figure 3: In the GUI configuration program, you can define wake-up times, for example.

In headless mode, you can use a terminal-based, menu-driven configuration program (Figure 4). But there is not much cause for you to rely on this, because you can choose either a Python module for integration with your own programs or a command-line tool for querying and changing important values. Alternatively, you can create a configuration on a Pi with a desktop, save it to a file, and then load it on the headless computer. The command line helps you with handling the last two steps. All of this is exemplary and leaves nothing to be desired.

Figure 4: A terminal-based configuration tool lets you use headless Pis, too.

Although the manufacturer has thought of everything, the software falls short of perfection. This is partly due to the nature of the matter: The hardware is complex and has many functions. It is not always possible to reflect this consistently in the software. While there is a detailed description of each setting, it is not always clear what exactly it will do. But this is nit-picking, considering the fact that other hardware manufacturers often leave users out in the cold without any documentation. I would recommend planning some time for the configuration work and extensive testing of your use case.

Test Run and Conclusion

The pHAT completed a small trial run with power cuts, time-controlled wake-up, and charging of the LiPo cell without any problems. The current consumption from the battery when switched off and without USB was approximately 0.5mA, which matches the data from the documentation. The only problem was current peaks that cause a sudden voltage drop – the HAT was unable to absorb them via the LiPo cell. In other words, the PiJuice cannot work miracles.

As expected, a circuit board with so many components is not cheap. The pHAT comes at £30 / $38 (plus VAT). There is also a larger HAT for regular Raspberry Pis that will set you back £70 / $90 (plus VAT). It comes with a battery which puts the price difference into perspective. You should also consider delivery charges. But price alone is not the decisive factor – having a working overall system consisting of hardware, software, and additional components is more important. The manufacturer not only sells its HATs, but also matching batteries and solar panels. Even if you go for a complete system, the price is still likely to be south of £150 or $200.

If you only need a subset of the functions, you can shop around for cheaper alternatives. There is a whole range of UPS and RTC HATs with wake-up function. With all these HATs and breakouts on offer, you will definitely want to take a look at the software before you buy. This is what separates the wheat from the chaff, and the best HAT is ultimately of no use to you if the software does not support your specific use case.