29static const uint8_t INA226_REGISTER_CONFIG = 0x00;
30static const uint8_t INA226_REGISTER_SHUNT_VOLTAGE = 0x01;
31static const uint8_t INA226_REGISTER_BUS_VOLTAGE = 0x02;
32static const uint8_t INA226_REGISTER_POWER = 0x03;
33static const uint8_t INA226_REGISTER_CURRENT = 0x04;
34static const uint8_t INA226_REGISTER_CALIBRATION = 0x05;
36static const uint16_t INA226_ADC_TIMES[] = {140, 204, 332, 588, 1100, 2116, 4156, 8244};
37static const uint16_t INA226_ADC_AVG_SAMPLES[] = {1, 4, 16, 64, 128, 256, 512, 1024};
40 ConfigurationRegister config;
43 if (!this->
write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
50 config.reserved = 0b100;
64 if (!this->
write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
76 ESP_LOGV(TAG,
" Using LSB=%" PRIu32
" calibration=%" PRIu32, lsb, calibration);
78 if (!this->
write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
84void INA226Component::dump_config() {
85 ESP_LOGCONFIG(TAG,
"INA226:");
90 LOG_UPDATE_INTERVAL(
this);
93 " ADC Conversion Time Bus Voltage: %d\n"
94 " ADC Conversion Time Shunt Voltage: %d\n"
95 " ADC Averaging Samples: %d",
99 LOG_SENSOR(
" ",
"Bus Voltage", this->bus_voltage_sensor_);
100 LOG_SENSOR(
" ",
"Shunt Voltage", this->shunt_voltage_sensor_);
101 LOG_SENSOR(
" ",
"Current", this->current_sensor_);
102 LOG_SENSOR(
" ",
"Power", this->power_sensor_);
105void INA226Component::update() {
106 if (this->bus_voltage_sensor_ !=
nullptr) {
107 uint16_t raw_bus_voltage;
108 if (!this->
read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
114 bus_voltage_v *= 0.00125f;
115 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
118 if (this->shunt_voltage_sensor_ !=
nullptr) {
119 uint16_t raw_shunt_voltage;
120 if (!this->
read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
126 shunt_voltage_v *= 0.0000025f;
127 this->shunt_voltage_sensor_->publish_state(shunt_voltage_v);
130 if (this->current_sensor_ !=
nullptr) {
131 uint16_t raw_current;
132 if (!this->
read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
139 this->current_sensor_->publish_state(current_ma / 1000.0f);
142 if (this->power_sensor_ !=
nullptr) {
144 if (!this->
read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
148 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 25.0f / 1000.0f);
149 this->power_sensor_->publish_state(power_mw / 1000.0f);