Battery powered pi for software hackers.

This is a guide to powering your new low-powered 5v device for those who know a little electronics, but not much. This device could be a Raspberry Pi or something like an MR3020, and the intended audience are software hackers who have a Cool Idea they can code, but need a way to power a low-powered computer either in a portable manner or located somewhere with no accessible power. I'm avoiding any complicated maths or schematics with more than a few components - this is for people who know only the most basic electronics concepts.

The problem:

The Pi and similar need 5V. Not just any 5V: It needs to be precisely 5V, regulated. This voltage must be sustained with precision in the face of varying load and spikes, and free of high-frequency noise that could cause interference with the delicate electronics of the device.

If you're reading this then you probably need to either supply this power from something portable, or you have a device with no accessible mains power and need to run it off solar.

Before you consider.

Before you look into power, you're going to need to minimise just how much you need. There are a few things you can do in order to reduce the power needs of your pi:
- Get an A. The B model has an extra chip to provide a USB hub and ethernet adaptor - this cheap is terribly demanding for power. Even when the ethernet isn't being used, it takes almost as much current as the processor itsself. Thus an A will greatly reduce the power needs of your project, almost halving them. That translates straight away to a much extended battery life, and less chance of running into problems with supply capacity. The obvious downside is the lack of ethernet or a second USB port.
- An external hard drive uses even more current than the pi itsself. Even a low-power 2.5" hard drive. If you can, do away with it - you can give the pi 64GB of storage on SD card (minus the operating system) and as much again on USB stick before the prices start to get silly. If you need that much storage, then at least do what you can in software to keep the drive spun down as much as possible. You'll also need to bridge the pi polyfuse to increase the USB port current limit.
- Enable frequency scaling. It doesn't hugely reduce power requirements (espicially on the B, thanks to that power-sucky ethernet/USB chip), but it'll shave a little off. It helps.

The quick and ugly way.

The 7805 is 'the' linear voltage regulator - it's not the best one around, but it is the most common and the one to which all others are compared. The 'linear' means two things: Firstly, that it is very simple to wire up. Secondly, that it is horrendously inefficient. If this is your power supply, you'll end up wasting as much energy as you actually use - something that must be compensated for by carrying more batteries or deploying a larger area of solar panels. This means the design is suitable only for situations in which energy is plentiful, such as inside a vehicle where the generously-sized battery and engine can spare far more than you'll need.

The 7805 needs an input voltage of at least seven volts. The efficiency depends upon the input voltage. Linear regulators work by effectively 'throwing away' excess volts to reduce to a target. For example, if you are powering your computer off a 7805 and a 9V battery, the 7805 accepts 9V in, throws away 4V, and gives the remaining 5V to the computer. In this example, only 5/9ths of the energy actually goes to the computer - for an energy efficiency of a pathetic 55%. If you're powering the computer off of a 12V car battery, you'll only use 5/12ths of the energy for an efficiency of 42%.

Another downside is heat. All that wasted energy has to go somewhere by simple conservation, and it goes into heat: The 7805 gets toasty. The energy dissipited is current * (voltage_in - voltage_out). You'll usually need a heatsink.

Notice those capacitors. The circuit would work without them, to an extent, but it'd be unstable - the power wouldn't be steady, and the device being powered would be unstable. The capacitors fix that. Exact values don't matter a great deal: They should be at least 200uF, but using too high a value won't hurt. If you're powering a 2.5" hard drive too you'll need substantially higher values to handle the sudden load when it spins up.

Switching supplies.

The 7508 and other linear regulators are terribly inefficient, and become increasingly inefficient as voltage increases. This can be fixed by the use of a switching regulator. They are more complicated, but you can get them in module form with all components included. The great thing about these is their ability to handle very high input voltages and remain efficient - even running your 5v computer off of a 24v battery bank, you can still get upwards of 90% efficiency.

Look for something like the BB-S7805 - as the number implies, it's a drop-in replacement for the 7805 linear regulator. Just much more efficient. A drop-in replacement is used in much the same manner as a linear regulator - the only difference in the circuit is a ceramic capacitor, required to block high-frequency interference that could otherwise cause the computer to be unstable.

The greater efficiency also means switching regulators don't need the bulky heatsinks of a linear regulator, and they can handle much higher current - a must when you want to run a hard drive too. If you have a 12V regulator too and a high enough voltage battery, you can even run a desktop drive - it'll suck power, but perhaps your application demands that much storage.

There are several types of switching supply, but the ones you are likely to encounter are the buck converter and the boost converter. The buck converter takes an input voltage higher than the output and reduces it, while the boost converter takes an input voltage lower than the output and increases it.

The ready-made way.

If you want a lot of energy in a small space, and the ability to recharge, what you need is a li-ion charge control circuit coupled to a boost converter for 5v output with over-discharge protection. You cannot build this. It's a very complicated circuit.

Fortunately, you don't need to. This circuit is already available widely: It's the device you'll often found sold as a battery box for recharging mobile phones. You can't build from scratch, but you can use one of those. A lot of the time you don't need to do any electronics at all: Simply plug an appropriate USB cable between battery-box and computer.

There is a downside to these: You don't know what you're getting. These are consumer devices, so they don't have a detailed datasheet. There's no way to know what lies inside without opening one up. You won't know how much current they can handle - you can be reasonably confident of 500ma, but beyond that you're down to trial-and-error. Powering a Raspberry Pi needs about 800ma peak to be reliable, and you can add as much again if you're running a 2.5" hard drive too. You also won't know how efficient they are or what capacity of battery they have (sometimes that much is labeled, not always), so you won't be able to calculate how long the battery will last - you'll just have to test it and time it.

Most of these use li-ion cells - the highest capacity available without resorting to very exotic technology. Better still, it's a very easy procedure to swap the battery for a much larger, higher-capacity li-ion, or several connected in parallel. The only thing to watch for here is the discharge protection circuit: If you've obtained your cells by disassembling a laptop battery, you'll need to make sure you've kept that and not thrown it away with the old cell. Otherwise you'll soon find your salvaged cells ruined.

Boost converters.

A boost converter takes a low voltage in, and puts a higher voltage out - the ones you may want to look at take the 3-4V from a li-ion cell and turn it into 5V for your pi. How they do this is something you should regard as magic, because you probably aren't good enough to build one. They are instead available as ready-made modules from many electronics components suppliers or the ever-useful eBay. Just take a boost converter, wire it to your li-ion cell (or bank of cells in parallel) and you're good for power.

They don't charge the cells though. Seperate Li-ion chargers are available from radio-control hobby shops. Almost every electically powered toy helicopter runs off of li-ion or li-poly cells.

If you need a boost converter, the best way to get one is usually the 'ready-made way' above. This gives you not just the converter but also a charger and a cell all in one convenient package. The only time I can imagine needing a boost converter alone would be in conjunction with a solar panel.

One problem with boost converters is their current output: Generally, it isn't much. You'll need at least 800ma to power a pi B, with a wireless adaptor or hard drive adding even more, and many boost converters just aren't up to that load. This is espicially a problem if you go the ready-made route, as you won't get detailed specifications for the mystery board inside.

Battery types.

Lead-acid: Ye olde cells - these things have been around 1859. Their energy density is very poor - that means you'll need a huge battery to get a decent amount of runtime. Combined with their extreme weight (The main functional components being big chunks of lead), these are entirely non-portable. Suitably only for stationary applications, but they do have their upsides: They are cheap, and very durable. A lead-acid will happy endure freezing, burning summer days, high surge currents, vibration and just about anything you can throw at it. An SLA will do this happily for years without maintainance, and a non-sealed battery needs only an occasional top-up with distilled water. If treated with care they'll work for decades. This makes lead-acids popular with off-grid power generation. They are cheap, too. Forget portability, but combined with a few solar panels they'll do well for a remote weather station, radio relay or similar task.

Ni-Cd: Stay well away. Fiddley things. You don't want these.

Ni-MH: The successor to the Ni-Cd, and an improvement in every way. These are your basic domestic rechargeable battery, of the type portable games consoles have been devouring for decades and parents patiently recharging. Their energy density is middling. A box full of these will run your computer for a few hours, before you need to pop them back in the charger. If you don't mind changing batteries from time to time, this will do. You'll generally want to string these in series to get enough voltage (Each cell is around 1.2V) and then use a linear or switching regulator to drop it to the 5v a pi demands. Ni-MH also has the advantage of very simple charging procedure and, if kept trickle charged, a very long shelf life indeed. It is often used in emergency lighting, because even if a NiMH battery has been sitting on charge for fifteen years it'll still be functional. Li-ion cannot promise that.

Li-ion (Or Li-poly): The Good Stuff - this is what makes your phone and laptop run so long on such a small battery. Their energy density puts all others to shame. A box of these can run your pi for days. This capacity does come with a downside: Li-ions are complicated things to use. They don't self-stabilise at a target voltage when charging, but need instead a carefully controlled charging procedure so complicated it usually requires computer control. Worse, if discharged fully they will be destroyed - a li-ion battery usually includes a circuit to cut the power before this happens. Their voltage range is also a problem - from 2.7V when at their lowest safe charge level up to as much as 4.2V when fully charged, which in turn requires a supply circuit capable of operating efficiently over this wide range of input voltages. They also lose capacity over time, regardless of usage. Vast numbers are available in discarded phones and laptops, but these will have only a fraction of their rated capacity remaining. Due to their complexity, you've little hope of building the circuitry to hook these up - you'll need to buy it ready-made. Easiest way, buy one of those battery-box phone chargers. That'll give you a li-ion cell and all the required components to make it work. If you do decide you need the sheer capacity only a big li-ion pack can provide, they are commonly used in radio-controlled car and aircraft, so you can buy batteries and chargers in the appropriate store. You can either use a one-cell ('1S' battery and a boost converter, or three cells in series and a switching regulator.

Battery construction.

The basic element of a battery is the cell. A cell doesn't provide a great deal of voltage, usually too little to be of use alone (With the exception of li-ions and related types), so these cells are connected together to form a battery. Each cell has two numbers of vital importance to you: The voltage (in volts) and the capacity (in AH or mAH). You can calculate how much energy a battery holds from 'Energy (J) = Voltage(V) * capacity(AH) * 3600.'

Cells added in series combine their voltages. For example, two 3.7V 700mAH cells in series act as one 7.4V 700mAH. This is subject to one condition which must never, ever be violated: The cells absolutely must be identical. Otherwise you get differential discharge and your cells end up ruined. This is a very strict rule - you can't even mix cells of identical model but differing dates, as they will be unequally aged. When connecting in series, deviation is unacceptable. This is why many products that take AAs have a warning not to mix battery brands.

Any number of cells connected in parallel act as one cell with their combined capacity: For example, two 3.7V 700mAH li-ion cells in parellel act as a single 3.7V 1400mAH. You do get a little more leniency when mixing cells in parallel - they still must all be of the same type, but you can mix cells of differing capacity providing their nominal voltage and chemistry match. Make sure you only connect together cells when they are both either fully charged or fully discharged, otherwise damage to the cells may result.

These two rules can be combined together and applied recursively: You can take twelve NI-MH cells, make them into two sets of six in series to combine the voltages, then combine these in parallel to get their total capacity.

Additionally to the cells, some batteries may incorporate protection circuitry of some form. This can include thermal fuses, overcurrent protection, or over-discharge protection to prevent the battery being inadvertantly damaged. Lead-acid and Ni-MH cells rarely need this, but it's essential with li-ions due to their inability to handle deep discharge - even one-cell li-ion batteries wll have a chip to prevent overdischarge, and multi-cell li-ion batteries typically have a controller chip built in to maintain equal charge across the cells as they age..

The basic battery always has two terminals, but you'll often find more. These 'extras' are used for cell balancing during the charge process or to communicate with in-battery logic that monitors the state of charge and gives information to power management systems (Or at times, acts to ensure only the original manufacturer can make replacements). In most cases they can be ignored. Some low-weight li-ion batteries also have a 'balance' connector, usually found on batteries designed for remote-control toys - this connector is used to equalise cells during charging. You can charge such a battery without using the balance connector, but doing so repeatedly will shorten battery life.

Solar power.

If your application requires long-term operation without grid power, you'll need to run off solar or something similar. The electronics for this get a bit more complicated. You'll need enough battery capacity to run your device overnight, with a good margin to handle the possibility of cloudy days, which means bulky batteries. Combined with a mount for the panels, portability isn't really an option. There isn't much to look over in power supply design: Solar panel + lead-acid(s) + charge controller + switching regulator. That's it. Easy. The charge controller regulates battery charging voltage to prevent damage - you can even get ones with built-in USB output. If you don't get one with USB out, use a 5V switching buck converter. Cheap, easy, endless power to run your weather station, radio relay, science experiment or whatever else you need a pi in the middle of nowhere for.

If you want portable solar, then it gets harder. You'll need a sizeable panel to run continuous, or you could use a charge-then-use mode - perhaps charging all day to get a few hours use in the evening. Either way, you'll have to deal with li-ion charging, and that is no easy thing. Best way is to just find a USB battery box with the panel built in. You don't want to have to build this if you can help it - it gets very complicated. Solar cells do not provide a great deal of power (If they did, we wouldn't be burning coal in power stations) so running a pi on solar continually may require too much panel area to be portable. More practical may be a timer system, where the pi activates only for a brief period (say, to take some readings) the shuts down for a few hours of charging. This requires additional electronics skill.