Arduino UNO current/power logger

This is a pretty simple project combing and Arduino UNO and an INA226 power monitor IC. With a little bit of tweaking it can log measuring at a sample rate of 1.5 kHz

Hardware
- Arduino UNO
- INA266 breakout board (from AliExpress or similar, e.g. https://www.aliexpress.com/item/1005003065372229.html?spm=a2g0o.order_list.0.0.33121802ciAOVy)
- Breadboard and leads
Assembly
Arduino | INA226 |
---|---|
5V | VCC |
GND | GND |
A4 | SDA |
A5 | SCL |
D7 | ALE |
Pin Connections
Then put the INA226 in series with the device being measured. Commonly this would involve connecting IN+ to the device's positive supply voltage and IN- to the device VCC. Then connecting VBS to IN-
Firmware
https://github.com/richcarni/UNO-power-logger
Requirements:
- Install Arduino INA226 library (https://github.com/RobTillaart/INA226)
To hit a sampling rate of 1.5kHz required a few optimisations:
- I use PIND&(1<<7) instead of digitalRead(7). I haven't measured the performance gain here but I have for digitalRead, and direct port manipulation takes 1 clock cycle (63 ns) vs 4.4 us for digitalRead.
- I set I2C speed to 400kHz
- I use Serial.write() to push the minimum amount of binary data. 2 bytes for the shunt voltage and 2 byte for the bus voltage. Potentially this could be reduced to 2 bytes total if we just pushed out the INA226's onboard power (or current) measurement. The trade off is you can't have both and you either give up some resolution or range
- A serial speed of 115200 baud is sufficient
Logging software
https://github.com/richcarni/simple-power-logger
I imagine it would be easiest to write something in Python. Since I've been writing a lot of C++ recently, I used that.
The gist of it involves unpacking the binary data that is being transmitted over the serial port. Each packet of 4 bytes contains the shunt voltage (first 2 bytes), following by the bus voltage. Note that the shunt voltage is a signed 16 bit integer
[byte0, byte1, byte2, byte3], [byte4, byte5, byte6, byte7], byte8...
Data starts immediately after a specific start sequence ("#!#!#!\n")
I convert the raw readings to voltages, calculate current and power, and write out to a CSV file that I can open later in Excel or Python
