Problem to solve

I have made a small ESP8266 based outdoor sensor that runs on batteries. I wanted to see how much power it uses when it powers up from sleep to help me do some calculations on battery lifetime.

In my toolbox i found an INA219 breakout board that would help me do my measurements.

The solution

I designed a datalogger based on the INA219 hooked up to an ESP8266. This microcontroller does all the logging and sends the data to a client program on my PC over Wifi.

The PC program can start/stop logging or even set up a trigger on the ESP. After datalogging is ended data is written to a CSV file for further inspection.

It’s possible to do about 500 current/voltage samples per minute and calculate accumulated mAh and mWh.

Example result

Here is a measurement on my outdoor sensor. I set the trigger to start sampling when current was bigger than 1mA (it only uses 20uA in sleep) . Here is the Excel graph of the voltage and current over the 3000ms timeperiod it was awake:

Download Raw CVS File here. Download Excel file here.

From this measurement I could see in the CSV that it consumed 0.064mAh in the 3 seconds wakeup period. A fast calculation for a 1Ah battery (1000mAh/0.064mAh) gives 15625 wakeups.

Electronics

The electronics is very simple. I guess it’s best shown with a picture of the breadboard:

ESP8266

The Arduino code for the ESP is found here on my github.

When downloading the code for the ESP, please rename “Setup-template.h” to “Setup.h” and edit your wifi settings and the IP of the client PC.

The code has 2 main parts.

Powersensor.h

Takes care of all the sampling. It is configured via Wifi:

Shunt resistor (0.1R on the cheap breakout boards)

Bus and Shunt sampling speed ( Refer to datasheet ) from 84uS to 68.10mS

Gain on Shunt. (40mv to 320mv or 1-8 division)

Voltage range 16/32 Volt

It can be started, stopped or told to trigger on either voltage, current or power.

Communication.h

Listen on UDP for instructions what to do:

Configure

Start sampling

Stop sampling

Set a trigger

It also handles sending measurements over UDP to the PC client.

PC Software

The Visual Studio C# software for the PC is found here on my github.

When started up it asks for some parameters. I was too lazy to do input validation – this is a tool for me 🙂 The parameters are persistent between startup.

After this comes the main control. It should be self explanatory.

Whenever the stop button is pressed a CSV file is saved in the storage folder.

It seemed like a lot of work to implement plotting/graphing in C#. It’s much faster to export to Excel and make the nice graphs there.

CSV-format:

Time in milliseconds since first reading Number of I2C readings before a new measurement is retreived (If this is lower than 2, then we missed some samples. mAh and mWh can’t be trusted! Voltage in V Current over shunt in mA Power consumption mW Power consumption over time mAh Power consumption over time mWh

Limitations

The ADC can go very fast on the INA219, but there seems to be some limitations in the ESP that prevents very fast readings. This means that if we want to catch ALL samplings, we can not go faster than 2.13ms samplings.

It’s especially important to catch all samples if you want the accumulated mAh and mWh.

My trick is to set a fast sampling (eg 84us) on the bus voltage and a slower (2.13ms) on the shunt. Doing this ensures that the current is sampled most of the time. I use interpolation where I assume that the current stays constant while measuring Voltage. The accumulated current in mAh is calculated from the samplingtime, the voltage and the current.

All the time the INA219 is measuring voltage it can’t measure current – thats why I normally do superfast/not precise voltage measurements and long current samplings.