ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ina226.cpp
Go to the documentation of this file.
1#include "ina226.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4#include <cinttypes>
5
6namespace esphome::ina226 {
7
8static const char *const TAG = "ina226";
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 INA226_REGISTER_CONFIG = 0x00;
29static const uint8_t INA226_REGISTER_SHUNT_VOLTAGE = 0x01;
30static const uint8_t INA226_REGISTER_BUS_VOLTAGE = 0x02;
31static const uint8_t INA226_REGISTER_POWER = 0x03;
32static const uint8_t INA226_REGISTER_CURRENT = 0x04;
33static const uint8_t INA226_REGISTER_CALIBRATION = 0x05;
34
35static const uint16_t INA226_ADC_TIMES[] = {140, 204, 332, 588, 1100, 2116, 4156, 8244};
36static const uint16_t INA226_ADC_AVG_SAMPLES[] = {1, 4, 16, 64, 128, 256, 512, 1024};
37
40
41 config.reset = 1;
42 if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
43 this->mark_failed();
44 return;
45 }
46 delay(1);
47
48 config.raw = 0;
49 config.reserved = 0b100; // as per datasheet
50
51 // Averaging Mode AVG Bit Settings[11:9] (000 -> 1 sample, 001 -> 4 sample, 111 -> 1024 samples)
52 config.avg_samples = this->adc_avg_samples_;
53
54 // Bus Voltage Conversion Time VBUSCT Bit Settings [8:6] (100 -> 1.1ms, 111 -> 8.244 ms)
56
57 // Shunt Voltage Conversion Time VSHCT Bit Settings [5:3] (100 -> 1.1ms, 111 -> 8.244 ms)
59
60 // Mode Settings [2:0] Combinations (111 -> Shunt and Bus, Continuous)
61 config.mode = 0b111;
62
63 if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
64 this->mark_failed();
65 return;
66 }
67
68 // lsb is multiplied by 1000000 to store it as an integer value
69 uint32_t lsb = static_cast<uint32_t>(ceilf(this->max_current_a_ * 1000000.0f / 32768));
70
71 this->calibration_lsb_ = lsb;
72
73 auto calibration = uint32_t(0.00512 / (lsb * this->shunt_resistance_ohm_ / 1000000.0f));
74
75 ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration);
76
77 if (!this->write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
78 this->mark_failed();
79 return;
80 }
81}
82
84 ESP_LOGCONFIG(TAG, "INA226:");
85 LOG_I2C_DEVICE(this);
86
87 if (this->is_failed()) {
88 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
89 return;
90 }
91 LOG_UPDATE_INTERVAL(this);
92
93 ESP_LOGCONFIG(TAG,
94 " ADC Conversion Time Bus Voltage: %d\n"
95 " ADC Conversion Time Shunt Voltage: %d\n"
96 " ADC Averaging Samples: %d",
97 INA226_ADC_TIMES[this->adc_time_voltage_ & 0b111], INA226_ADC_TIMES[this->adc_time_current_ & 0b111],
98 INA226_ADC_AVG_SAMPLES[this->adc_avg_samples_ & 0b111]);
99
100 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
101 LOG_SENSOR(" ", "Shunt Voltage", this->shunt_voltage_sensor_);
102 LOG_SENSOR(" ", "Current", this->current_sensor_);
103 LOG_SENSOR(" ", "Power", this->power_sensor_);
104}
105
107 if (this->bus_voltage_sensor_ != nullptr) {
108 uint16_t raw_bus_voltage;
109 if (!this->read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
110 this->status_set_warning();
111 return;
112 }
113 // Convert for 2's compliment and signed value (though always positive)
114 float bus_voltage_v = this->twos_complement_(raw_bus_voltage, 16);
115 bus_voltage_v *= 0.00125f;
116 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
117 }
118
119 if (this->shunt_voltage_sensor_ != nullptr) {
120 uint16_t raw_shunt_voltage;
121 if (!this->read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
122 this->status_set_warning();
123 return;
124 }
125 // Convert for 2's compliment and signed value
126 float shunt_voltage_v = this->twos_complement_(raw_shunt_voltage, 16);
127 shunt_voltage_v *= 0.0000025f;
128 this->shunt_voltage_sensor_->publish_state(shunt_voltage_v);
129 }
130
131 if (this->current_sensor_ != nullptr) {
132 uint16_t raw_current;
133 if (!this->read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
134 this->status_set_warning();
135 return;
136 }
137 // Convert for 2's compliment and signed value
138 float current_ma = this->twos_complement_(raw_current, 16);
139 current_ma *= (this->calibration_lsb_ / 1000.0f);
140 this->current_sensor_->publish_state(current_ma / 1000.0f);
141 }
142
143 if (this->power_sensor_ != nullptr) {
144 uint16_t raw_power;
145 if (!this->read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
146 this->status_set_warning();
147 return;
148 }
149 float power_mw = int16_t(raw_power) * (this->calibration_lsb_ * 25.0f / 1000.0f);
150 this->power_sensor_->publish_state(power_mw / 1000.0f);
151 }
152
153 this->status_clear_warning();
154}
155
156int32_t INA226Component::twos_complement_(int32_t val, uint8_t bits) {
157 if (val & ((uint32_t) 1 << (bits - 1))) {
158 val -= (uint32_t) 1 << bits;
159 }
160 return val;
161}
162
163} // namespace esphome::ina226
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 * power_sensor_
Definition ina226.h:70
sensor::Sensor * bus_voltage_sensor_
Definition ina226.h:67
sensor::Sensor * current_sensor_
Definition ina226.h:69
sensor::Sensor * shunt_voltage_sensor_
Definition ina226.h:68
AdcAvgSamples adc_avg_samples_
Definition ina226.h:65
int32_t twos_complement_(int32_t val, uint8_t bits)
Definition ina226.cpp:156
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
mopeka_std_values val[3]
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t