One of the essential properties of a mobile, Raspberry Pi-based robot is that it needs to run on battery power - trailing a power cord around is not much use.
The problem is that the Pi takes an appreciable amount of current (say 500mA, depending on activity and attached peripherals), and needs a pretty narrow input voltage range (5V +/- 0.25V, or so). Because battery voltage varies pretty wildly depending on the current charge level, running directly from a battery is not really sensible.
So, I set about looking into various options for converting standard battery voltages into something suitable for the Pi.
The traditional approach, back when I was first tinkering with electronics about 30 years ago, would be to put together enough batteries to get a significantly higher voltage than 5V (say, 4x nonrechargeable AA to get 6V, or 6x rechargeable AA for 7.2V), and then run that through a linear regulator (e.g. 7805-series IC) to get a steady 5V.
There are 2 main problems with this approach.
Fortunately, there are much better approaches nowadays, in the form of switched-mode regulators, which are much much more efficient, even at high currents.
Using an RC-model UBEC
Decent radio-controlled models, especially aircraft, often need an efficient, stable-voltage power supply running from a small, light battery. The standard approach for this is to use a rechargeable battery linked through a device known as a UBEC (Ultimate Battery Eliminator Circuit), which takes a higher voltage than the required output, and very efficiently down-converts it. Whereas a linear regulator feeding 500mA output from a 6V input would draw 500mA (leading to wasted power of (6-5)x0.5 = 0.5W), a UBEC will not need to draw the full 500mA from the input battery, and so wastes very little power.
Because UBECs are so commonly used for RC models, you can pick them up very cheaply, and they can generally handle some pretty high currents. For instance, I found a 4A model on eBay for about £1.50 including postage.
The drawback is that you need to supply more input voltage than the desired output voltage, which means you may need a lot of cells in the battery pack. Still, this is a very cheap option and works well.
Using a DC-DC converter
If weight is a priority, then keeping the number of battery cells to a minimum is important. Fortunately, there's a device called a DC-DC converter that works in a very similar way to a UBEC, but can work from an input voltage that's lower than the required output voltage. These are also typically really tiny.
Looking on eBay again, I found some really nice ones, which include a female USB-A socket. This means that you can use the same USB lead that you're probably using to power your Raspberry Pi, without any modifications. The price here was around £2.50, with free postage. Input voltage is 3-5V (ideal for 3x rechargeable AA), and output current is up to 1A, which should be plenty.
Using an integrated battery box
Finally, there are various solutions available which use rechargeable batteries plus a DC-DC converter, in a dedicated housing. These can be pretty nice, because they don't require any specialist assembly (e.g. soldering) - some even have the batteries already built in. The option I chose uses high capacity "18650" Lithium Ion cells (e.g. these, around £10 for a pair from eBay), and cost around £8 including postage. It can supply up to 2.5A, which is more than enough, and again has a built-in USB-B socket for easy connection, as well as a convenient USB-miniA socket for easy charging. Another nice feature of this type of box is that you can stick in anything from 1-4 cells, depending on how much battery life you need.
A drawback is that these boxes can be quite large. The one I chose is about the same size as the box that my Pi came in from Farnell.
If you do go for the 18650 option, then it's worth shopping around carefully. Some brands, most notably Ultrafire, have a poor reputation for quality and don't seem to live up to their rated capacities. These types of batteries are also prone to fire or explosion if used improperly - so you'll want to be very careful to look after them, and it's worth making sure that you're not using a dodgy brand.
Battery life calculations
I haven't experimentally verified any battery life figures for any of these options yet, although I have tested that my Pi runs happily from each of them (except, so far, for the UBEC).
When calculating theoretical battery life, because you're converting voltages, you can't go simply by the milliamp-hour (mAh) ratings printed on the battery. It's simplest to convert to watt-hours, which is simply voltage multiplied by the mAh figure. The RasPi needs approx 500mA at 5V, which is 0.5 x 5 = 2.5 watts. Assuming perfect efficiency in the converter (they're usually at least 90% efficient), a 1.5V AA cell with 1000mAh capacity would be able to supply 1.5Wh - i.e. run a RasPi for approx 1.5 / 2.5 = 0.6 hours (or 36 mins) on its own. With a switched-mode converter (i.e. any of the last 3 options), it doesn't really matter whether you connect multiple cells in series or in parallel - in each case, you're roughly multiplying the available capacity by the number of cells used.
Here's an easy side-by-side comparison of the options listed above. I hope it helps you to figure out an appropriate battery power solution for your Pi project.
Approach | Approx Cost | Relative Size/Weight | Ease of assembly | Approx lifetime (hrs) | Hardware price per hour of battery life |
UBEC + 6xAA (rechargeable, 1.2V / 1800mAh each) | £6 | Medium | Hardest - custom connectors at each end | 5.18 | £1.16 |
Mini DC-DC + 3xAA (rechargeable, 1.2V / 1800mAh each) | £5 | Smallest | Moderate - some soldering of input wires | 2.59 | £1.93 |
Battery box + 1x18650 (rechargeable, 3.7V / 2800mAh) | £13 | Largest | Trivial | 4.1 | £3.17 |
Battery box + 4x18650 (rechargeable, 3.7V / 2800mAh each) | £28 | Largest | Trivial | 16.5 | £1.70 |
Monitoring charge level
When running from batteries, it's wise to try to monitor the current charge level, so that you can estimate the battery life remaining. You can do this by observing the voltage across the battery - this will fall as the battery discharges. Apart from allowing for non-linear discharge curves (each cell type behaves differently, and has a different voltage range), there are two main difficulties with this when running a Pi from a voltage convertor.
- The input voltage at the Pi is always going to be a steady 5V, by design. So you need to connect wires from the input battery to your charge monitoring circuit, rather than being able to measure the voltage at the input to the Pi. For integrated battery boxes, this requires drilling some holes in the box to access the battery.
- The Pi does not have an analogue-to-digital convertor built in, so you can't directly measure the voltage using the Pi. You can get small, cheap, standalone ADC chips that are accessible using the Pi's GPIO pins (e.g. using I2C), which is probably the cheapest option. Personally, I have a lot of ATTiny85 microcontrollers lying around (essentially a mini-Arduino), and I'll probably look at using one of those to measure the analogue voltage, convert to a percentage remaining indication using software on the ATTiny, and then communicate that level to the Pi over I2C.
Unfortunately, you can't properly power down the Pi purely from software, so there's also a potential mini-project to provide a software-controllable, latching off-switch. Personally, I expect to just use the manual off-switch built into the battery box. If you're using Li 18650 cells, then it's worth getting the 'protected' type, as these automatically cut out at low voltages.