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