# How to measure battery capacity with Arduino

Battery capacity measurement can be useful in many situations. And it is not hard, only requires enough time to discharge battery completely with know resistance and a way to measure voltage in the circuit. Ohm's law will to the rest: `I = U / R`

Let's say, we want to measure standard AA 1.5V alkaline battery capacity. Why 1.5V? They are common, made by many manufacturers and sold for different prices. And not always more expensive is better. To do this, we will need:

- AA 1.5V battery
- resistor to discharge it. We need high current to discharge battery in reasonable time, so low resistance is suggested. On the other hand, high current means o lot of heat, so we need a resistor that can survive this. I suggest using 2.2Ohm 5W ceramic resistors.
- Arduino to measure voltage in circuit. Any Arduino or plain ATmega or ATtiny with A/D converter will do.

So, first a simple electrical circuit:

And some code that will be run every second:

```
voltage = 5.0 * ((float) analogRead(V_METER)) / 1024.0;
float current = voltage / R_LOAD;
joules += voltage * current;
float wattHours = (joules / 3600.0) * 1000.0;
```

And here how it work:

- We need to measure voltage in circuit. This is why, in first step, we read 10bit A/D converter and scale output to 5V. Why 5V? Arduino Uno works on 5V, and it is the reference voltage here,
- Next, lets compute current using Ohm's law
`I = U/R`

, - With know current current we can compute work using
`P = U * I`

and store it in*joules*variable, - Last step is to change joules to Watt hours.

If instead of Watt hours we want Ampere hours, there is no need to count joules. Instead of that, sum current and final value divide by 3600 (there are 3600 seconds in one hour). Like this:

```
voltage = 5.0 * ((float) analogRead(V_METER)) / 1024.0;
float current = voltage / R_LOAD;
ampereSeconds += current;
float ampereHours = ampereSeconds / 3600.0;
```

Full code is available here

## Notes

- this circuit allows to measure batteries with voltage up to 5V. Anything above it will damage A/D converter
- to measure higher voltages, voltage divider will be required
- with higher voltage, power loss on resistor will increase. It will get very hot and might burn

If you have any comments or questions, or just want to say "Hi, Pawel, good job!" you can always drop me a tweet!