Back to Wled

Usermod INA226

usermods/INA226_v2/README.md

16.0.04.6 KB
Original Source

Usermod INA226

This Usermod is designed to read values from an INA226 sensor and output the following:

  • Current
  • Voltage
  • Power
  • Shunt Voltage
  • Overflow status

Configuration

The following settings can be configured in the Usermod Menu:

  • Enabled: Enable or disable the usermod.
  • I2CAddress: The I2C address in decimal. Default is 64 (0x40).
  • CheckInterval: Number of seconds between readings. This should be higher than the time it takes to make a reading, determined by the two next options.
  • INASamples: The number of samples to configure the INA226 to use for a measurement. Higher counts provide more accuracy. See the 'Understanding Samples and Conversion Times' section for more details.
  • INAConversionTime: The time to use on converting and preparing readings on the INA226. Higher times provide more precision. See the 'Understanding Samples and Conversion Times' section for more details.
  • Decimals: Number of decimals in the output.
  • ShuntResistor: Shunt resistor value in milliohms. An R100 shunt resistor should be written as "100", while R010 should be "10".
  • CurrentRange: Expected maximum current in milliamps (e.g., 5 A = 5000 mA).
  • MqttPublish: Enable or disable MQTT publishing.
  • MqttPublishAlways: Publish always, regardless if there is a change.
  • MqttHomeAssistantDiscovery: Enable Home Assistant discovery.

Understanding Samples and Conversion Times

The INA226 uses a programmable ADC with configurable conversion times and averaging to optimize the measurement accuracy and speed. The conversion time and number of samples are determined based on the INASamples and INAConversionTime settings. The following table outlines the possible combinations:

Conversion Time (μs)1 Sample4 Samples16 Samples64 Samples128 Samples256 Samples512 Samples1024 Samples
1400.28 ms1.12 ms4.48 ms17.92 ms35.84 ms71.68 ms143.36 ms286.72 ms
2040.408 ms1.632 ms6.528 ms26.112 ms52.224 ms104.448 ms208.896 ms417.792 ms
3320.664 ms2.656 ms10.624 ms42.496 ms84.992 ms169.984 ms339.968 ms679.936 ms
5881.176 ms4.704 ms18.816 ms75.264 ms150.528 ms301.056 ms602.112 ms1204.224 ms
11002.2 ms8.8 ms35.2 ms140.8 ms281.6 ms563.2 ms1126.4 ms2252.8 ms
21164.232 ms16.928 ms67.712 ms270.848 ms541.696 ms1083.392 ms2166.784 ms4333.568 ms
41568.312 ms33.248 ms132.992 ms531.968 ms1063.936 ms2127.872 ms4255.744 ms8511.488 ms
824416.488 ms65.952 ms263.808 ms1055.232 ms2110.464 ms4220.928 ms8441.856 ms16883.712 ms

It is important to pick a combination that provides the needed balance between accuracy and precision while ensuring new readings within the CheckInterval setting. When USERMOD_INA226_DEBUG is defined, the info pane contains the expected time to make a reading, which can be seen in the table above.

As an example, if you want a new reading every 5 seconds (CheckInterval), a valid combination is 256 samples and 4156 μs which would provide new values every 2.1 seconds.

The picked values also slightly affect power usage. If the CheckInterval is set to more than 20 seconds, the INA226 is configured in triggered reading mode, where it only uses power as long as it's working. Then the conversion time and average samples counts determine how long the chip stays turned on every CheckInterval time.

Calculating Current and Power

The INA226 calculates current by measuring the differential voltage across a shunt resistor and using the calibration register value to convert this measurement into current. Power is calculated by multiplying the current by the bus voltage.

For detailed programming information and register configurations, refer to the INA226 datasheet.

Author

@LordMike

Compiling

To enable, compile with INA226 in custom_usermods (e.g. in platformio_override.ini).

ini
[env:ina226_example]
extends = env:esp32dev
custom_usermods = ${env:esp32dev.custom_usermods} INA226
build_flags = ${env:esp32dev.build_flags}
  ; -D USERMOD_INA226_DEBUG ; -- add a debug status to the info modal