Power recording for Raspberry PI HATs based on INA219
This tool records current data measured by Current/Power monitor HATs based on INA219 chip.
Example boards:
https://www.waveshare.com/wiki/Current/Power_Monitor_HAT
It is possible to log current data with 1ms sampling rate.
The tool is not able to display any data by itself, but it prepares necessary files that could be loaded by Gnuplot.
Example capture of some MCU power consumtion measured with 1ms sampling rate:
PowRec is designed to run on Raspberry PI. Tested Raspberry PIs and OSes:
- Raspberry Pi 3 Model B Rev 1.2, Raspberry Pi OS with desktop, Debian version: 11 (bullseye), 32-bit.
- Connect INA219 board.
- Enable I2C bus using raspi-config tool.
- Detect INA219 chip(s) using i2cdetect tool:
$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 41 42 43 -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
- Enable fast-mode (400kb/s) I2C bus speed.
- edit /boot/config.txt file,
- find dtparam=i2c_arm=on,
- add i2c_arm_baudrate=400000 entry, complete line should look like this:
dtparam=i2c_arm=on,i2c_arm_baudrate=400000
Switching to fast-mode is optional but required for fast sampling rates (miliseconds). Reading single current value using I2C read takes 800us using default mode and 200us using fast mode.
- Install Erlang and Elixir: sudo apt install erlang elixir
- Install Gnuplot on RaspberryPI. This step is optional, but it is comfortable to run Gnuplot on RasPI and watch results without copying data files.
The mix tool is used to manage elixir projects. Mix is delivered with elixir package.
- Clone and enter into repository folder.
- Download and compile dependencies:
$ mix deps.get
$ mix deps.compile
- Compile powrec app:
$ mix compile
PowRec is executed using mix run command:
$ mix run powrec.exs opt1 opt2 ...
Options list could be displayed using -h option:
$ mix run powrec.exs -h
PowRec 0.0.4
Usage: powrec [OPTION [ARGS]]
-h, --help print help
-m, --int-ms sampling interval in ms, default: 100
-o, --out output files prefix, default: out
-l, --low low measurement range: 16V, 0.4 A, default: false
-a, --addr INA219 I2C address, default: 64
Controls how often powrec will read data from INA219 chips. Minimum value is 1 ms. Default value is set to 100ms i.e. 10 current readings per second.
Name of recorded data file. Default value is "out". Powrec will produce following files based on this parameter:
- out.dat : recorded data, plain ASCII format, comma separated values,
- out_mA.plot : Gnuplot file presenting current data plot,
- out_V.plot : Gnuplot file presenting voltage data plot.
PowRec uses two default modes from INA219 driver library:
- 16V/0.4A range (low mode),
- 32V/2A range (default mode).
INA219 chip could be selected using I2C address value. Decimal values are required. Predefined INA219 addresses could be: 64, 65, 66, 67. Default is 64 (0x40 hex).
After executing, powrec records data in the backgroud providing also simple command line console:
PowRec 0.0.4
Enter h - for help
powrec> h
powrec commands:
d
6396
: toggle display
i: print info
h: help
q: quit program
powrec> d
powrec> ch1: 21.7 mA, 4.164 V
powrec> i
PowRec runtime information:
INA219 I2C address: 64
Sampling interval: 100 ms
Voltage range: 32 V
Current range: 2.0 A
powrec>
Recording stops after selecting "q" option.
Data plot will pop-up after executing gnuplot with produced log.plot file:
$ gnuplot out_mA.plot &
$ gnuplot out_V.plot &
INA219 chip provides several options for measurements accuracy, range etc. Those options will be slowly added in subsequent releases of powrec.
Currently PowRec is not supporting recording data from multiple INA219 chips at the same time. It is possible to run multiple PowRec instances attached do different chips (--addr option).
PowRec uses:
- INA219 driver for Elixir: https://hex.pm/packages/ina219
- Circuits I2C driver: https://hex.pm/packages/circuits_i2c
- Elixir ALE: https://hex.pm/packages/elixir_ale