Skip to content

DataLogger.ino

Arnd edited this page Oct 13, 2018 · 1 revision

Arduino sketch to demonstrate making current and voltage measurements with the INA226 chip in the background and storing readings in as few bytes as possible.

The program uses the Atmel interrupt mechanism and is therefore limited to those processors. Two different interrupts are used:

  1. A Pin-Change-Interrupt is used that is triggered when the INA226 open-drain alert pin pulls down when a measurement is ready. This is set in the program to trigger approximately 15 times a second. The raw voltage and bus values are stored and averaged in this interrupt routine.
  2. An Atmel clock timer is used to trigger an interrupt every second. The interrupt handler is where all the work of the program is done. The accumulated raw readings are averaged and then stored in memory.

Since the precision of the INA226 is 2 bytes for both the bus voltage and the shunt voltage the storage requirement for measurements every second would be 120 bytes per minute. Since the delta values between measurements will usually have a lot less bits of precision this program tries to make the most of the available memory by utilizing a variation on Huffmann encoding to use variable-length storage. The number of nibbles used to store data ranges from 1 to 5. The minimum size used is a "nibble" which is 4 bits.

Compatibility

This program works on Atmel processors

General

Pin Change Interrupt Pins

Each Atmel processor has specific pin allocations for interrupts. The program uses a pin change interrupt, of which each processor has only a limited number of vectors to handle. A description of which pins can be used on which processor can be found at Arduino PinChangeInterrupt(). The constant INA226_ALERT_PIN defines which pin is used in this program. As the pin is pulled low on alarm, the program uses the internal pullup to ensure that the pin is high when not triggered.

Function