DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ina219.h File Reference

TI INA219 high-side current / power monitor codec (DETWS_ENABLE_INA219). More...

#include <stdint.h>

Go to the source code of this file.

Macros

#define INA219_REG_CONFIG   0x00
 configuration
 
#define INA219_REG_SHUNT   0x01
 shunt voltage
 
#define INA219_REG_BUS   0x02
 bus voltage
 
#define INA219_REG_POWER   0x03
 power
 
#define INA219_REG_CURRENT   0x04
 current
 
#define INA219_REG_CALIBRATION   0x05
 calibration
 

Functions

int32_t ina219_bus_mv (uint16_t raw)
 Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV).
 
int32_t ina219_shunt_uv (int16_t raw)
 Decode the shunt-voltage register to microvolts (signed, LSB 10 uV).
 
uint16_t ina219_calibration (uint32_t current_lsb_ua, uint32_t shunt_mohm)
 Compute the calibration register from the current LSB (microamps per bit) and the shunt resistance (milliohms): trunc(0.04096 / (current_LSB[A] * R[ohm])) = 40960000 / (lsb_ua * shunt_mohm), clamped to 16 bits. (100 uA + 100 mohm -> 4096.) 0 on a zero denominator.
 
int32_t ina219_current_ua (int16_t raw, uint32_t current_lsb_ua)
 Scale the raw current register to microamps (raw * current_lsb_ua).
 
int32_t ina219_power_uw (int16_t raw, uint32_t current_lsb_ua)
 Scale the raw power register to microwatts (power LSB is 20 * current LSB).
 
bool ina219_begin (uint8_t addr, uint32_t current_lsb_ua, uint32_t shunt_mohm)
 Program the INA219 at addr: write the calibration for current_lsb_ua (uA/bit) and shunt_mohm (milliohms), then the default 32 V / 320 mV continuous config.
 
bool ina219_read_bus_mv (int32_t *millivolts)
 Read the bus voltage into millivolts.
 
bool ina219_read_shunt_uv (int32_t *microvolts)
 Read the shunt voltage into microvolts.
 
bool ina219_read_current_ua (int32_t *microamps)
 Read the current into microamps (needs the calibration set by ina219_begin).
 
bool ina219_read_power_uw (int32_t *microwatts)
 Read the power into microwatts (needs the calibration set by ina219_begin).
 

Detailed Description

TI INA219 high-side current / power monitor codec (DETWS_ENABLE_INA219).

The INA219 measures the voltage across a shunt resistor (LSB 10 uV) and the bus voltage (LSB 4 mV, in the upper 13 bits of its register), and - once a calibration value derived from the shunt resistance and a chosen current LSB is programmed - reports current and power directly. From those you get how much current and power a circuit draws.

This codec is pure and host-tested: ina219_bus_mv / ina219_shunt_uv decode the voltage registers, ina219_calibration computes the calibration register, and ina219_current_ua / ina219_power_uw scale the raw current / power registers by the current LSB. On an ESP32 the binding programs the calibration + config and reads the registers over I2C (Wire); only that touches hardware.

A cheap solder-and-bench-test breakout: put it in series with a load and watch the current.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file ina219.h.

Macro Definition Documentation

◆ INA219_REG_CONFIG

#define INA219_REG_CONFIG   0x00

configuration

Definition at line 30 of file ina219.h.

◆ INA219_REG_SHUNT

#define INA219_REG_SHUNT   0x01

shunt voltage

Definition at line 31 of file ina219.h.

◆ INA219_REG_BUS

#define INA219_REG_BUS   0x02

bus voltage

Definition at line 32 of file ina219.h.

◆ INA219_REG_POWER

#define INA219_REG_POWER   0x03

power

Definition at line 33 of file ina219.h.

◆ INA219_REG_CURRENT

#define INA219_REG_CURRENT   0x04

current

Definition at line 34 of file ina219.h.

◆ INA219_REG_CALIBRATION

#define INA219_REG_CALIBRATION   0x05

calibration

Definition at line 35 of file ina219.h.

Function Documentation

◆ ina219_bus_mv()

int32_t ina219_bus_mv ( uint16_t  raw)

Decode the bus-voltage register to millivolts (value is bits [15:3], LSB 4 mV).

◆ ina219_shunt_uv()

int32_t ina219_shunt_uv ( int16_t  raw)

Decode the shunt-voltage register to microvolts (signed, LSB 10 uV).

◆ ina219_calibration()

uint16_t ina219_calibration ( uint32_t  current_lsb_ua,
uint32_t  shunt_mohm 
)

Compute the calibration register from the current LSB (microamps per bit) and the shunt resistance (milliohms): trunc(0.04096 / (current_LSB[A] * R[ohm])) = 40960000 / (lsb_ua * shunt_mohm), clamped to 16 bits. (100 uA + 100 mohm -> 4096.) 0 on a zero denominator.

◆ ina219_current_ua()

int32_t ina219_current_ua ( int16_t  raw,
uint32_t  current_lsb_ua 
)

Scale the raw current register to microamps (raw * current_lsb_ua).

◆ ina219_power_uw()

int32_t ina219_power_uw ( int16_t  raw,
uint32_t  current_lsb_ua 
)

Scale the raw power register to microwatts (power LSB is 20 * current LSB).

◆ ina219_begin()

bool ina219_begin ( uint8_t  addr,
uint32_t  current_lsb_ua,
uint32_t  shunt_mohm 
)

Program the INA219 at addr: write the calibration for current_lsb_ua (uA/bit) and shunt_mohm (milliohms), then the default 32 V / 320 mV continuous config.

Returns
true on ack.

◆ ina219_read_bus_mv()

bool ina219_read_bus_mv ( int32_t *  millivolts)

Read the bus voltage into millivolts.

Returns
false on I2C error.

◆ ina219_read_shunt_uv()

bool ina219_read_shunt_uv ( int32_t *  microvolts)

Read the shunt voltage into microvolts.

Returns
false on I2C error.

◆ ina219_read_current_ua()

bool ina219_read_current_ua ( int32_t *  microamps)

Read the current into microamps (needs the calibration set by ina219_begin).

◆ ina219_read_power_uw()

bool ina219_read_power_uw ( int32_t *  microwatts)

Read the power into microwatts (needs the calibration set by ina219_begin).