ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ina219.cpp
Go to the documentation of this file.
1#include "ina219.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::ina219 {
6
7static const char *const TAG = "ina219";
8
9// | A0 | A1 | Address |
10// | GND | GND | 0x40 |
11// | GND | V_S+ | 0x41 |
12// | GND | SDA | 0x42 |
13// | GND | SCL | 0x43 |
14// | V_S+ | GND | 0x44 |
15// | V_S+ | V_S+ | 0x45 |
16// | V_S+ | SDA | 0x46 |
17// | V_S+ | SCL | 0x47 |
18// | SDA | GND | 0x48 |
19// | SDA | V_S+ | 0x49 |
20// | SDA | SDA | 0x4A |
21// | SDA | SCL | 0x4B |
22// | SCL | GND | 0x4C |
23// | SCL | V_S+ | 0x4D |
24// | SCL | SDA | 0x4E |
25// | SCL | SCL | 0x4F |
26
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;
34
36 // Config Register
37 // 0bx000000000000000 << 15 RESET Bit (1 -> trigger reset)
38 if (!this->write_byte_16(INA219_REGISTER_CONFIG, 0x8000)) {
39 this->mark_failed();
40 return;
41 }
42
43 delay(1);
44
45 // 0b00000xxxx0000000 << 7 Bus ADC Resolution/Averaging
46 // 0b000000000xxxx000 << 3 Shunt ADC Resolution/Averaging
47
48 // Value Resolution, Averaging, Conversion
49 // 0b0X00 -> 9 bit, 1 sample, 84 µs
50 // 0b0X01 -> 10 bit, 1 sample, 148 µs
51 // 0b0X10 -> 11 bit, 1 sample, 276 µs
52 // 0b0X11 -> 12 bit, 1 sample, 532 µs
53 // 0b1001 -> 12 bit, 2 samples, 1.06 ms
54 // 0b1010 -> 12 bit, 4 samples, 2.13 ms
55 // 0b1011 -> 12 bit, 8 samples, 4.26 ms
56 // 0b1100 -> 12 bit, 16 samples, 8.51 ms
57 // 0b1101 -> 12 bit, 32 samples, 17.02 ms
58 // 0b1110 -> 12 bit, 64 samples, 34.05 ms
59 // 0b1111 -> 12 bit, 128 samples, 68.10 ms <--
60
61 // 0b0000000000000xxx << 0 Mode (Bus and Shunt continuous -> 0b111)
62
63 // Bus ADC and Shunt ADC 12 bit+128 samples
64 uint16_t config = 0x0000;
65 // Continuous operation of Bus and Shunt ADCs
66 config |= 0b0000000000000111;
67 // Bus ADC and Shunt ADC 12 bit+128 samples -> 68.10 ms
68 config |= 0b0000011110000000;
69 config |= 0b0000000001111000;
70 const float shunt_max_voltage = this->shunt_resistance_ohm_ * this->max_current_a_;
71
72 // 0b00x0000000000000 << 13 Bus Voltage Range (0 -> 16V, 1 -> 32V)
73 bool bus_32v_range = this->max_voltage_v_ > 16.0f || shunt_max_voltage > 0.16f;
74 float multiplier;
75 if (bus_32v_range) {
76 config |= 0b0010000000000000;
77 multiplier = 0.5f;
78 } else {
79 config |= 0b0000000000000000;
80 multiplier = 1.0f;
81 }
82
83 // 0b000xx00000000000 << 11 Shunt Voltage Gain (0b00 -> 40mV, 0b01 -> 80mV, 0b10 -> 160mV, 0b11 -> 320mV)
84 uint16_t shunt_gain;
85 if (shunt_max_voltage * multiplier <= 0.02f) {
86 shunt_gain = 0b00; // 40mV
87 } else if (shunt_max_voltage * multiplier <= 0.04f) {
88 shunt_gain = 0b01; // 80mV
89 } else if (shunt_max_voltage * multiplier <= 0.08f) {
90 shunt_gain = 0b10; // 160mV
91 } else {
92 if (int(shunt_max_voltage * multiplier * 100) > 16) {
93 ESP_LOGW(TAG,
94 " Max voltage across shunt resistor (resistance*current) exceeds %dmV. "
95 "This could damage the sensor!",
96 int(160 / multiplier));
97 }
98 shunt_gain = 0b11; // 320mV
99 }
100
101 config |= shunt_gain << 11;
102 ESP_LOGCONFIG(TAG, " Using %dV-Range Shunt Gain=%dmV", bus_32v_range ? 32 : 16, 40 << shunt_gain);
103 if (!this->write_byte_16(INA219_REGISTER_CONFIG, config)) {
104 this->mark_failed();
105 return;
106 }
107
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));
110 uint32_t lsb = min_lsb;
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)
114 break;
115 }
116 if (lsb > max_lsb) {
117 lsb = max_lsb;
118 ESP_LOGW(TAG, " The requested current (%0.02fA) cannot be achieved without an overflow", this->max_current_a_);
119 }
120
121 this->calibration_lsb_ = lsb;
122 auto calibration = uint32_t(0.04096f / (0.000001 * lsb * this->shunt_resistance_ohm_));
123 ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration);
124 if (!this->write_byte_16(INA219_REGISTER_CALIBRATION, calibration)) {
125 this->mark_failed();
126 return;
127 }
128}
129
131 // Mode = 0 -> power down
132 if (!this->write_byte_16(INA219_REGISTER_CONFIG, 0)) {
133 ESP_LOGE(TAG, "powerdown error");
134 }
135}
136
138 ESP_LOGCONFIG(TAG, "INA219:");
139 LOG_I2C_DEVICE(this);
140
141 if (this->is_failed()) {
142 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
143 return;
144 }
145 LOG_UPDATE_INTERVAL(this);
146
147 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
148 LOG_SENSOR(" ", "Shunt Voltage", this->shunt_voltage_sensor_);
149 LOG_SENSOR(" ", "Current", this->current_sensor_);
150 LOG_SENSOR(" ", "Power", this->power_sensor_);
151}
152
154 if (this->bus_voltage_sensor_ != nullptr) {
155 uint16_t raw_bus_voltage;
156 if (!this->read_byte_16(INA219_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
157 this->status_set_warning();
158 return;
159 }
160 raw_bus_voltage >>= 3;
161 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.004f;
162 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
163 }
164
165 if (this->shunt_voltage_sensor_ != nullptr) {
166 uint16_t raw_shunt_voltage;
167 if (!this->read_byte_16(INA219_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
168 this->status_set_warning();
169 return;
170 }
171 float shunt_voltage_mv = int16_t(raw_shunt_voltage) * 0.01f;
172 this->shunt_voltage_sensor_->publish_state(shunt_voltage_mv / 1000.0f);
173 }
174
175 if (this->current_sensor_ != nullptr) {
176 uint16_t raw_current;
177 if (!this->read_byte_16(INA219_REGISTER_CURRENT, &raw_current)) {
178 this->status_set_warning();
179 return;
180 }
181 float current_ma = int16_t(raw_current) * (this->calibration_lsb_ / 1000.0f);
182 this->current_sensor_->publish_state(current_ma / 1000.0f);
183 }
184
185 if (this->power_sensor_ != nullptr) {
186 uint16_t raw_power;
187 if (!this->read_byte_16(INA219_REGISTER_POWER, &raw_power)) {
188 this->status_set_warning();
189 return;
190 }
191 float power_mw = int16_t(raw_power) * (this->calibration_lsb_ * 20.0f / 1000.0f);
192 this->power_sensor_->publish_state(power_mw / 1000.0f);
193 }
194
195 this->status_clear_warning();
196}
197
198} // namespace esphome::ina219
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
sensor::Sensor * current_sensor_
Definition ina219.h:33
sensor::Sensor * shunt_voltage_sensor_
Definition ina219.h:32
sensor::Sensor * bus_voltage_sensor_
Definition ina219.h:31
sensor::Sensor * power_sensor_
Definition ina219.h:34
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t