ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ina226.cpp
Go to the documentation of this file.
1#include "ina226.h"
3#include "esphome/core/log.h"
4#include "esphome/core/hal.h"
5#include <cinttypes>
6
7namespace esphome::ina226 {
8
9ESPHOME_LOG_TAG(TAG, "ina226");
10
11// | A0 | A1 | Address |
12// | GND | GND | 0x40 |
13// | GND | V_S+ | 0x41 |
14// | GND | SDA | 0x42 |
15// | GND | SCL | 0x43 |
16// | V_S+ | GND | 0x44 |
17// | V_S+ | V_S+ | 0x45 |
18// | V_S+ | SDA | 0x46 |
19// | V_S+ | SCL | 0x47 |
20// | SDA | GND | 0x48 |
21// | SDA | V_S+ | 0x49 |
22// | SDA | SDA | 0x4A |
23// | SDA | SCL | 0x4B |
24// | SCL | GND | 0x4C |
25// | SCL | V_S+ | 0x4D |
26// | SCL | SDA | 0x4E |
27// | SCL | SCL | 0x4F |
28
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;
35
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};
38
41
42 config.reset = 1;
43 if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
44 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
45 return;
46 }
47 delay(1);
48
49 config.raw = 0;
50 config.reserved = 0b100; // as per datasheet
51
52 // Averaging Mode AVG Bit Settings[11:9] (000 -> 1 sample, 001 -> 4 sample, 111 -> 1024 samples)
53 config.avg_samples = this->adc_avg_samples_;
54
55 // Bus Voltage Conversion Time VBUSCT Bit Settings [8:6] (100 -> 1.1ms, 111 -> 8.244 ms)
57
58 // Shunt Voltage Conversion Time VSHCT Bit Settings [5:3] (100 -> 1.1ms, 111 -> 8.244 ms)
60
61 // Mode Settings [2:0] Combinations (111 -> Shunt and Bus, Continuous)
62 config.mode = 0b111;
63
64 if (!this->write_byte_16(INA226_REGISTER_CONFIG, config.raw)) {
65 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
66 return;
67 }
68
69 // lsb is multiplied by 1000000 to store it as an integer value
70 uint32_t lsb = static_cast<uint32_t>(ceilf(this->max_current_a_ * 1000000.0f / 32768));
71
72 this->calibration_lsb_ = lsb;
73
74 auto calibration = uint32_t(0.00512f / (lsb * this->shunt_resistance_ohm_ / 1000000.0f));
75
76 ESP_LOGV(TAG, " Using LSB=%" PRIu32 " calibration=%" PRIu32, lsb, calibration);
77
78 if (!this->write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
79 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
80 return;
81 }
82}
83
85 ESP_LOGCONFIG(TAG, "INA226:");
86 LOG_I2C_DEVICE(this);
87
88 if (this->is_failed())
89 return;
90 LOG_UPDATE_INTERVAL(this);
91
92 ESP_LOGCONFIG(TAG,
93 " ADC Conversion Time Bus Voltage: %d\n"
94 " ADC Conversion Time Shunt Voltage: %d\n"
95 " ADC Averaging Samples: %d",
96 INA226_ADC_TIMES[this->adc_time_voltage_ & 0b111], INA226_ADC_TIMES[this->adc_time_current_ & 0b111],
97 INA226_ADC_AVG_SAMPLES[this->adc_avg_samples_ & 0b111]);
98
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_);
103}
104
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)) {
109 this->status_set_warning();
110 return;
111 }
112 // Convert for 2's compliment and signed value (though always positive)
113 float bus_voltage_v = sign_extend<16>(raw_bus_voltage);
114 bus_voltage_v *= 0.00125f;
115 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
116 }
117
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)) {
121 this->status_set_warning();
122 return;
123 }
124 // Convert for 2's compliment and signed value
125 float shunt_voltage_v = sign_extend<16>(raw_shunt_voltage);
126 shunt_voltage_v *= 0.0000025f;
127 this->shunt_voltage_sensor_->publish_state(shunt_voltage_v);
128 }
129
130 if (this->current_sensor_ != nullptr) {
131 uint16_t raw_current;
132 if (!this->read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
133 this->status_set_warning();
134 return;
135 }
136 // Convert for 2's compliment and signed value
137 float current_ma = sign_extend<16>(raw_current);
138 current_ma *= (this->calibration_lsb_ / 1000.0f);
139 this->current_sensor_->publish_state(current_ma / 1000.0f);
140 }
141
142 if (this->power_sensor_ != nullptr) {
143 uint16_t raw_power;
144 if (!this->read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
145 this->status_set_warning();
146 return;
147 }
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);
150 }
151
152 this->status_clear_warning();
153}
154
155} // namespace esphome::ina226
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
void status_clear_warning()
Definition component.h:296
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
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
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ESPHOME_LOG_TAG(TAG, "ina226")
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
Definition helpers.h:1012
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t