How do batteries work? How can we run a circuit for a long time on a small battery?
A battery is basically a bag to hold electrons. Its capacity is roughly measured in milliampere-hours (mAh) or Coulombs (1 mAh = 3.6C). One Coulomb is about ±6e18 electrons.
The electrons in the battery get used up when they flow around the wire from the – terminal to the + terminal. This is called current and is measured in milliamperes (mA). a lightbulb uses about 1000 mA (1A), a LED uses around 1 to 20 mA, and a microcontroller uses between 0.1 to 1 mA. One reason to measure battery life in mAh is that this gives a good idea of how many hours the circuit can run with a 1 mA current being taken from the battery.
Measuring the current being taken from a battery is complicated, since it typically varies with time. One nice feature of current is that it is basically independent of place. In other words, in a “series” circuit, the current is the same in all places. This is very similar to water in a single river. No matter how wide and deep a river is, the amount of water moving per second is the same, it just moves faster when it is shallow and narrow. In a parallel circuit, the current flows down two different paths, and so only the total current is the same; if a river flows around an island, then the amount of water flowing on the left versus the right might not be the same, but the total flowing around the island is the same as the total flowing before and after it.
So we can measure current “anywhere”. The first way I learned to measure current was to disconnect the circuit, and insert the multimeter into the circuit as sort of a fancy wire. When it is in current-meter mode (ammeter) it lets the current flow through it, and measures how fast the current is moving. This has two big problems: (1) you have to disconnect the circuit (and so reset the microcontroller) whenever you want to measure or not (and actually the “probe” wires are hard to hold in place), (2) the ammeter has a fuse in it to prevent too much current from flowing, and I blew the fuse. So that method basically sucks.
I had heard several times about another method, but it sounded really hacky. You measure a voltage drop across a (usually small) resistor of known resistance. Why would there be this weird method if multi-meters already had a current measurement mode? Well, after getting fed up with the the multi-meter, I tried it, and oh my goodness is it easy, accurate, and convenient. I used a 10 Ohm resistor, and found a 13.2 milliVolt drop, which is 13.2 mV / 10 Ohm = 1.32 mA, which is (a) what it was supposed to be and (b) about what my analog current meter had said (no fuse, but not very good for less than 5 mA). Google even did the conversion for me, but of course now the conversion is easy to do in your head. In fact with a 1 Ohm resistor the only conversion is to change the “mV” to an “mA”.
This was especially helpful when checking my datalogger for how well my power saving features were working: running full blast required about 1mA. My power saving reduced it to 0.93mA. Wow. So I read more of the data-sheets and discovered that there was a register bit called “Sleep Enable” and without it the “sleep” instruction just waited one cycle and moved on. Once I enabled sleep, the current dropped to about 0.22mA, and when I disabled some heartbeat LED timers (I had already removed the LED itself), it dropped down to 0.19mA. In deep sleep mode, doing nothing else, I measure it as about 0.002mA, so there is still room for improvement.
Zem got me a coin-cell battery (3 Volt model CR2032) with about 225 mAh capacity. That means my datalogger should run about 50 days right now, and probably years in deep sleep. Of course this ignores certain inconvenient details of how batteries work, but there are additional circuit components that can hide those details for only a small cost. At any rate, I expect it to run about a month per coin cell with the current design, which seems a bit wasteful, so I am still looking into lower current options and recharging options.
To indicate the scale: cost of the circuit other than the battery: $2.50. Cost of the battery: $2.50.
Update: More aggressively turned off the ADC during sleep, and now it is down to 0.005mA, or 5.7 years on the coin cell. Much better.