27static const uint8_t INA219_READ = 0x01;
28static const uint8_t INA219_REGISTER_CONFIG = 0x00;
29static const uint8_t INA219_REGISTER_SHUNT_VOLTAGE = 0x01;
30static const uint8_t INA219_REGISTER_BUS_VOLTAGE = 0x02;
31static const uint8_t INA219_REGISTER_POWER = 0x03;
32static const uint8_t INA219_REGISTER_CURRENT = 0x04;
33static const uint8_t INA219_REGISTER_CALIBRATION = 0x05;
64 uint16_t config = 0x0000;
66 config |= 0b0000000000000111;
68 config |= 0b0000011110000000;
69 config |= 0b0000000001111000;
73 bool bus_32v_range = this->
max_voltage_v_ > 16.0f || shunt_max_voltage > 0.16f;
76 config |= 0b0010000000000000;
79 config |= 0b0000000000000000;
85 if (shunt_max_voltage * multiplier <= 0.02f) {
87 }
else if (shunt_max_voltage * multiplier <= 0.04f) {
89 }
else if (shunt_max_voltage * multiplier <= 0.08f) {
92 if (
int(shunt_max_voltage * multiplier * 100) > 16) {
94 " Max voltage across shunt resistor (resistance*current) exceeds %dmV. "
95 "This could damage the sensor!",
96 int(160 / multiplier));
101 config |= shunt_gain << 11;
102 ESP_LOGCONFIG(TAG,
" Using %dV-Range Shunt Gain=%dmV", bus_32v_range ? 32 : 16, 40 << shunt_gain);
108 auto min_lsb =
uint32_t(ceilf(this->max_current_a_ * 1000000.0f / 0x8000));
109 auto max_lsb =
uint32_t(floorf(this->max_current_a_ * 1000000.0f / 0x1000));
111 for (; lsb <= max_lsb; lsb++) {
112 float max_current_before_overflow = lsb * 0x7FFF / 1000000.0f;
113 if (this->max_current_a_ <= max_current_before_overflow)
118 ESP_LOGW(TAG,
" The requested current (%0.02fA) cannot be achieved without an overflow", this->max_current_a_);
123 ESP_LOGV(TAG,
" Using LSB=%" PRIu32
" calibration=%" PRIu32, lsb, calibration);
124 if (!this->
write_byte_16(INA219_REGISTER_CALIBRATION, calibration)) {
130void INA219Component::on_powerdown() {
133 ESP_LOGE(TAG,
"powerdown error");
137void INA219Component::dump_config() {
138 ESP_LOGCONFIG(TAG,
"INA219:");
139 LOG_I2C_DEVICE(
this);
143 LOG_UPDATE_INTERVAL(
this);
145 LOG_SENSOR(
" ",
"Bus Voltage", this->bus_voltage_sensor_);
146 LOG_SENSOR(
" ",
"Shunt Voltage", this->shunt_voltage_sensor_);
147 LOG_SENSOR(
" ",
"Current", this->current_sensor_);
148 LOG_SENSOR(
" ",
"Power", this->power_sensor_);
151void INA219Component::update() {
152 if (this->bus_voltage_sensor_ !=
nullptr) {
153 uint16_t raw_bus_voltage;
154 if (!this->
read_byte_16(INA219_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
158 raw_bus_voltage >>= 3;
159 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.004f;
160 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
163 if (this->shunt_voltage_sensor_ !=
nullptr) {
164 uint16_t raw_shunt_voltage;
165 if (!this->
read_byte_16(INA219_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
169 float shunt_voltage_mv = int16_t(raw_shunt_voltage) * 0.01f;
170 this->shunt_voltage_sensor_->publish_state(shunt_voltage_mv / 1000.0f);
173 if (this->current_sensor_ !=
nullptr) {
174 uint16_t raw_current;
175 if (!this->
read_byte_16(INA219_REGISTER_CURRENT, &raw_current)) {
179 float current_ma = int16_t(raw_current) * (this->
calibration_lsb_ / 1000.0f);
180 this->current_sensor_->publish_state(current_ma / 1000.0f);
183 if (this->power_sensor_ !=
nullptr) {
185 if (!this->
read_byte_16(INA219_REGISTER_POWER, &raw_power)) {
189 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 20.0f / 1000.0f);
190 this->power_sensor_->publish_state(power_mw / 1000.0f);