A couple of the projects I have been working on recently have a requirement to run from a portable power supply. Now finding a power supply that can power the Pi is quite a simple task there are a large number out there to chose from and each have their own merits, but as usual I wanted something that was readily available in as many places as possible.
My final choice was the Adafruit PowerBoost 500C as it has a built in charging circuit to enable the device to be charged over night, I have since learnt that there is now a 1000C out there should you need a little more power.
So some basic info on the 500C mainly taken from the Adafruit site for reference
- Synchronous operation means you can disconnect the output completely by connecting the ENable pin to ground. This will completely turn off the output
- 2A internal switch (~2.5A peak limiting) means you can get 500mA+ from a 3.7V LiPoly/LiIon battery. We had no problem drawing 1000mA, just make sure your battery can handle it!
- Low battery indicator LED lights up red when the voltage dips below 3.2V, optimized for LiPo/LiIon battery usage
- Onboard 500mA charge-rate ‘iOS’ data resistors. Solder in the USB connector and you can plug in any iPhone or iPod for 500mA charge rate. Not suggested for large iPads.
- Full breakout for battery in, control pins and power out
- 90%+ operating efficiency in most cases (see datasheet for efficiency graphs), and low quiescent current: 5mA when enabled and power LED is on, 20uA when disabled (power and low batt LED are off)
There’s three power voltages, the USB input for charging the battery (4.75-5.25V whatever is coming out of the USB port), the battery itself (3-4.2V) and the output (5-5.2V)
- USB – this is the micro USB 5V power pin. It’s the pin that is used to charge the battery, NOT the output power! You can use this if you want to grab power from the microUSB port when it is plugged in
- GND – this is the power ground. This boost converter is not ‘isolated’ – the ground input is the same as the ground output
- BAT – this is the battery input, connected directly to the JST connector. For most Lithium batteries, this will range from 3.0V when near-dead to 4.2V when fully-charged. Higher voltages will let you draw more current and in general, are more efficient. Try to keep the wires going to this pin nice and short – 3″ or less is best!
- 5V – this is the boosted output. When the board is running, the voltage will be 5.2V approximately. It may dip down to 5V as the current draw starts to go up (over 500mA). When the board is disabled, this output is ‘floating’ but you should still try not to apply a voltage to it while the board is disabled. There’s a green LED connected to this pin which will let you know when there’s power output
There’s two ‘control’ pins.
- EN – this is the ‘enable’ pin. By default it is pulled ‘high’ to VBAT. To turn off the booster, connect this pin to ground. The switch can be as small as you like, it is just a signal. Contrast this to an inline power switch which would have to be able to handle up to 2A of current! When the chip is disabled the output is completely disconnected from the input.
- LBO – not a leveraged buy out! this is the Low Battery Output. By default it is pulled high to BAT but when the charger detects a low voltage (under 3.2V) the pin will drop down to 0V. You can use this to signal when its time to shut down or alert the user that the battery is low. There is also a red LED connected to this pin.
There are four onboard LEDs.
- The Blue LED sits next to the USB connector socket, and indicates the 5V output power state.
- The Red LED is next to the battery JST port and indicates when the battery voltage is below 3.2VDC (Low Battery Output)
- The Yellow LED is next to the microUSB connector and indicates when the battery is being charged
- The Green LED is also next to the microUSB connector and indicates when the battery is done charging (all full)
It comes without the USB connector soldered onto the output side, but other than that connect up your LiPo/LiIon battery to either the JST connector or the positive (+) BAT pin and the negative (-) to a GND and away you go power to your device is ready to go.
The PowerBoost 500C does not come with an on/off switch for the output, but its very easy to add one.
We will be turning the boost on/off via the ENABLE pin, so the switch does not have to carry any power, it is only signalling to the boost converter what to do. This means the switch can be small. Use any switch you like, if it is 0.1″ spacing thats ideal. If your switch only has two pins instead of three, tie one pin to GND and the other pin to EN – when the switch is closed, the power will turn off.
All fine and nice if your device is not headless and you can run any shutdown commands you want to before flicking the switch, so how do we get around that?
We have to tie it back into the Pi itself and use the Pi to signal back to the 500C once the shutdown has been completed via the EN pin and cut power thus saving the battery. I’m going to use the Pi Shutdown Button code that I hacked together, to control the shutdown of the Pi and modify it slightly to enable me to send a signal over to the 500C to kill the power and save the battery
#! /usr/bin/python #pipower.py import time import os import RPi.GPIO as GPIO #set the signal pin high GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP) #the shutdown script GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP) def Shutdown(channel): os.system("sudo reboot") GPIO.add_event_detect(20, GPIO.FALLING, callback = Shutdown, bouncetime = 2000) while 1: time.sleep(1)
Notice that here we are using a
reboot not a
shutdown command, the reason for this is, as part of the reboot the GPIO pins are reinitialized, giving us the pull to GND that we need to signal to the 500C 😉
If you wire this up right now your Pi is not even going to boot! there is a little more work to do this time, we need to add a switch to control the On/Off state of the device and a diode to protect the Pi from the battery
Notice that I used On/Off there? as unlike the Pi Shutdown Button where the device would be left in a halted state this is going to turn the device off once the shutdown is compete, only to be powered back on with human intervention.
The circuit diagram looks something like this;
In it’s drawn state the device will be powered on once the boot sequence has completed and the script above is running GPIO21 will have been pulled High, flicking the switch removes the connection between BAT and EN but the signal is maintained from GPIO21 the other pole of the switch in turn causes GPIO20 to be pulled to GND forcing the pin low and initiating the
os.system("sudo reboot") in the code causing the Pi to halt all running applications and reboot, as part of the reboot process GPIO21 will get reinitialized pulling it to 0 then signalling to the EN of the 500C to powerdown. At this point shutdown and power off are complete.
Flicking the switch back again makes a connection between BAT and EN of the 500C causing it to power up, and the Pi boots as one would expect when supplying it with power, with the diode stopping GPIO21 being forced High at this point, once boot is complete and
pipower.py has been called turning the switch to the off position will cause a graceful shutdown and power off, prior to that it’s just the same as pulling the power plug!
Now we want to get
pipower.py to run every time the Pi is booted up so that it’s sitting there waiting for the signal to shutdown your device, for this we need to create a shell script that will start the Python script with root access. Put the shell script in the same directory as your
save the following as
#!/bin/sh #pipower.sh cd / cd home/pi sudo python pipower.py
Lets test the shell script
sudo chmod 755 /home/pi/pipower.sh
To make the script executable then
sudo sh /home/pi/pipower.sh
To run it, flick your switch and your Pi should begin it’s shutdown process and then power off.
Now we are going to use crontab to autostart the script. You are going to want to open the crontab editor under the root user by using the following command;
sudo crontab -e
Append the following line to the end of the file
@reboot sh /home/pi/pipower.sh
Thats about it your done, enjoy and stay safe