ESPHome 2025.9.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 {
7namespace ina226 {
8
9static const char *const 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();
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();
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.00512 / (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();
80 return;
81 }
82}
83
85 ESP_LOGCONFIG(TAG, "INA226:");
86 LOG_I2C_DEVICE(this);
87
88 if (this->is_failed()) {
89 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
90 return;
91 }
92 LOG_UPDATE_INTERVAL(this);
93
94 ESP_LOGCONFIG(TAG,
95 " ADC Conversion Time Bus Voltage: %d\n"
96 " ADC Conversion Time Shunt Voltage: %d\n"
97 " ADC Averaging Samples: %d",
98 INA226_ADC_TIMES[this->adc_time_voltage_ & 0b111], INA226_ADC_TIMES[this->adc_time_current_ & 0b111],
99 INA226_ADC_AVG_SAMPLES[this->adc_avg_samples_ & 0b111]);
100
101 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
102 LOG_SENSOR(" ", "Shunt Voltage", this->shunt_voltage_sensor_);
103 LOG_SENSOR(" ", "Current", this->current_sensor_);
104 LOG_SENSOR(" ", "Power", this->power_sensor_);
105}
106
108
110 if (this->bus_voltage_sensor_ != nullptr) {
111 uint16_t raw_bus_voltage;
112 if (!this->read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
113 this->status_set_warning();
114 return;
115 }
116 // Convert for 2's compliment and signed value (though always positive)
117 float bus_voltage_v = this->twos_complement_(raw_bus_voltage, 16);
118 bus_voltage_v *= 0.00125f;
119 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
120 }
121
122 if (this->shunt_voltage_sensor_ != nullptr) {
123 uint16_t raw_shunt_voltage;
124 if (!this->read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
125 this->status_set_warning();
126 return;
127 }
128 // Convert for 2's compliment and signed value
129 float shunt_voltage_v = this->twos_complement_(raw_shunt_voltage, 16);
130 shunt_voltage_v *= 0.0000025f;
131 this->shunt_voltage_sensor_->publish_state(shunt_voltage_v);
132 }
133
134 if (this->current_sensor_ != nullptr) {
135 uint16_t raw_current;
136 if (!this->read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
137 this->status_set_warning();
138 return;
139 }
140 // Convert for 2's compliment and signed value
141 float current_ma = this->twos_complement_(raw_current, 16);
142 current_ma *= (this->calibration_lsb_ / 1000.0f);
143 this->current_sensor_->publish_state(current_ma / 1000.0f);
144 }
145
146 if (this->power_sensor_ != nullptr) {
147 uint16_t raw_power;
148 if (!this->read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
149 this->status_set_warning();
150 return;
151 }
152 float power_mw = int16_t(raw_power) * (this->calibration_lsb_ * 25.0f / 1000.0f);
153 this->power_sensor_->publish_state(power_mw / 1000.0f);
154 }
155
156 this->status_clear_warning();
157}
158
159int32_t INA226Component::twos_complement_(int32_t val, uint8_t bits) {
160 if (val & ((uint32_t) 1 << (bits - 1))) {
161 val -= (uint32_t) 1 << bits;
162 }
163 return val;
164}
165
166} // namespace ina226
167} // 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 ina226.cpp:107
sensor::Sensor * power_sensor_
Definition ina226.h:72
sensor::Sensor * bus_voltage_sensor_
Definition ina226.h:69
sensor::Sensor * current_sensor_
Definition ina226.h:71
sensor::Sensor * shunt_voltage_sensor_
Definition ina226.h:70
AdcAvgSamples adc_avg_samples_
Definition ina226.h:67
int32_t twos_complement_(int32_t val, uint8_t bits)
Definition ina226.cpp:159
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
mopeka_std_values val[4]
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