# Can an IMP measure the capacity of a battery?

Hi,
I’m building a very simple button application. The IMP is always in a deep sleep and a push button, connected to Pin1 and the 3.3V pin (april board), is configured as DIGITAL_IN_WAKEUP. Until here everything is all fine.
Now I want this to be powered by a battery, maybe 4 x 1.5V AA, and I would like to measure its capacity right after each push of a button. I would add an LED that flashes as soon as the battery level drops below a certain point.

1. Is this possible and if so how?
2. Is this the best way to make the most battery efficient button?

thanks,
pieter

AGENT
`function doSomething(data){ server.log("doSomething: "+data); } device.on("sendTime",doSomething);`

DEVICE
`function toAgent(){
server.log(“button pressed”);
local d = date();
local min = d[“min”];
local hour = d[“hour”];
local sec = d[“sec”];
local t = hour+":"+min+":"+sec;
agent.send(“sendTime”, t);
}
if (hardware.wakereason() == WAKEREASON_PIN1) {
server.log(“It happened!”);
toAgent();
}

hardware.pin1.configure(DIGITAL_IN_WAKEUP);

imp.configure(“Time Button”, [], []);
imp.onidle(function() { server.sleepuntil(6, 0); } );`

I don’t really think you can measure battery ‘capacity’ directly as you can only measure voltage with the imp. (Perhaps there’s an indirect way you could estimate it by doing some math on successive samples … but I’d have to think this thru more).

Measuring battery ‘voltage’ however is straightforward, just make sure you don’t exceed 3.3v applied to the I/O pins. As you’re using 4 AA’s, I’d suggest perhaps a 2x resistive voltage divider to drop the level to something that’s pin-compatible. You can then make your battery level threshold decision based on this level.

It’s certainly possible to measure battery voltage, although it requires a couple extra components. Here’s the circuit you’re going to build (it’s a voltage divider circuit that’s controlled by a couple FETs):

And here’s the code you would use to read it (assuming you used pins 2 and 5 as in the circuit diagram):

`const BATMAX = 59577.0; // 65535 * 3.0 / 3.3

bat <- hardware.pin5;
bat_en <- hardware.pin2;

bat.configure(ANALOG_IN);
bat_en.configure(DIGITAL_OUT);
bat_en.write(0);

// reads battery level
function batteryLevel() {
bat_en.write(1);

local batteryLevel = bat.read() * 1.0; // read and cast to floating point
local batteryPercent = batteryLevel / MAXBAT;
local batteryVoltage = 3.3 * batteryPercent;

bat_en.write(0);

return { voltage = batteryVoltage, percent = batteryPercent };
}`

NOTE: I coded this in the forum text editor, it’s possible there are errors…

The April voltage regulator will take about 24ua even when the Imp is sleeping. Not bad but it increases to about 200ua when the battery voltage drops below 3.5v.

If you’re getting 4xAAs below 3.5v, then you have 4 very very dead batteries

Hey, I like the FET idea … thanks, didn’t think of that. Keeps the quiescent current low, and improves the divider accuracy. I just used a high R value passive divider, fudging values a bit to compensate for the couple ua input current. Couple more components here … but much more elegant

Thanks for all the pointers.
But what would be the best battery solution?
I would prefer to use standard rechargeable ones.

The Nora reference design notes have some interesting battery information:

https://electricimp.com/docs/hardware/resources/reference-designs/nora/

Has anyone used hardware.voltage() ?
I couldn’t find anything in the forums.
Could it be the solution to what I want to achieve?
Warn me when the battery drops below …

http://electricimp.com/docs/api/hardware/voltage/

Well, hardware.voltage() measures the input voltage to the imp – so, obviously, if the imp is connected straight to the battery then this will be the battery voltage, but most designs put some kind of regulator or power-supply chip between the battery and the imp. In that case, hardware.voltage() would measure the output side of the power-supply chip (which will be 3.3V until the battery is too dead to run the imp at all), and not the input side where the battery is.

Peter

Re: the Sparkfun INA169 - with an apparent sleep current of 60 ua, (i.e. roughly 10x that of a deep-sleep imp), I’m not sure how useful this would be in a battery-based design.