ESPHome  2022.6.3
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 
5 namespace esphome {
6 namespace ina226 {
7 
8 static 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 
28 static const uint8_t INA226_REGISTER_CONFIG = 0x00;
29 static const uint8_t INA226_REGISTER_SHUNT_VOLTAGE = 0x01;
30 static const uint8_t INA226_REGISTER_BUS_VOLTAGE = 0x02;
31 static const uint8_t INA226_REGISTER_POWER = 0x03;
32 static const uint8_t INA226_REGISTER_CURRENT = 0x04;
33 static const uint8_t INA226_REGISTER_CALIBRATION = 0x05;
34 
36  ESP_LOGCONFIG(TAG, "Setting up INA226...");
37  // Config Register
38  // 0bx000000000000000 << 15 RESET Bit (1 -> trigger reset)
39  if (!this->write_byte_16(INA226_REGISTER_CONFIG, 0x8000)) {
40  this->mark_failed();
41  return;
42  }
43  delay(1);
44 
45  uint16_t config = 0x0000;
46 
47  // Averaging Mode AVG Bit Settings[11:9] (000 -> 1 sample, 001 -> 4 sample, 111 -> 1024 samples)
48  config |= 0b0000001000000000;
49 
50  // Bus Voltage Conversion Time VBUSCT Bit Settings [8:6] (100 -> 1.1ms, 111 -> 8.244 ms)
51  config |= 0b0000000100000000;
52 
53  // Shunt Voltage Conversion Time VSHCT Bit Settings [5:3] (100 -> 1.1ms, 111 -> 8.244 ms)
54  config |= 0b0000000000100000;
55 
56  // Mode Settings [2:0] Combinations (111 -> Shunt and Bus, Continuous)
57  config |= 0b0000000000000111;
58 
59  if (!this->write_byte_16(INA226_REGISTER_CONFIG, config)) {
60  this->mark_failed();
61  return;
62  }
63 
64  // lsb is multiplied by 1000000 to store it as an integer value
65  uint32_t lsb = static_cast<uint32_t>(ceilf(this->max_current_a_ * 1000000.0f / 32768));
66 
67  this->calibration_lsb_ = lsb;
68 
69  auto calibration = uint32_t(0.00512 / (lsb * this->shunt_resistance_ohm_ / 1000000.0f));
70 
71  ESP_LOGV(TAG, " Using LSB=%u calibration=%u", lsb, calibration);
72 
73  if (!this->write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
74  this->mark_failed();
75  return;
76  }
77 }
78 
80  ESP_LOGCONFIG(TAG, "INA226:");
81  LOG_I2C_DEVICE(this);
82 
83  if (this->is_failed()) {
84  ESP_LOGE(TAG, "Communication with INA226 failed!");
85  return;
86  }
87  LOG_UPDATE_INTERVAL(this);
88 
89  LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
90  LOG_SENSOR(" ", "Shunt Voltage", this->shunt_voltage_sensor_);
91  LOG_SENSOR(" ", "Current", this->current_sensor_);
92  LOG_SENSOR(" ", "Power", this->power_sensor_);
93 }
94 
96 
98  if (this->bus_voltage_sensor_ != nullptr) {
99  uint16_t raw_bus_voltage;
100  if (!this->read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
101  this->status_set_warning();
102  return;
103  }
104  float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
105  this->bus_voltage_sensor_->publish_state(bus_voltage_v);
106  }
107 
108  if (this->shunt_voltage_sensor_ != nullptr) {
109  uint16_t raw_shunt_voltage;
110  if (!this->read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
111  this->status_set_warning();
112  return;
113  }
114  float shunt_voltage_v = int16_t(raw_shunt_voltage) * 0.0000025f;
115  this->shunt_voltage_sensor_->publish_state(shunt_voltage_v);
116  }
117 
118  if (this->current_sensor_ != nullptr) {
119  uint16_t raw_current;
120  if (!this->read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
121  this->status_set_warning();
122  return;
123  }
124  float current_ma = int16_t(raw_current) * (this->calibration_lsb_ / 1000.0f);
125  this->current_sensor_->publish_state(current_ma / 1000.0f);
126  }
127 
128  if (this->power_sensor_ != nullptr) {
129  uint16_t raw_power;
130  if (!this->read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
131  this->status_set_warning();
132  return;
133  }
134  float power_mw = int16_t(raw_power) * (this->calibration_lsb_ * 25.0f / 1000.0f);
135  this->power_sensor_->publish_state(power_mw / 1000.0f);
136  }
137 
138  this->status_clear_warning();
139 }
140 
141 } // namespace ina226
142 } // namespace esphome
sensor::Sensor * power_sensor_
Definition: ina226.h:31
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:107
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
sensor::Sensor * bus_voltage_sensor_
Definition: ina226.h:28
sensor::Sensor * current_sensor_
Definition: ina226.h:30
float get_setup_priority() const override
Definition: ina226.cpp:95
void status_clear_warning()
Definition: component.cpp:148
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:70
void status_set_warning()
Definition: component.cpp:140
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
Definition: a4988.cpp:4
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:127
void dump_config() override
Definition: ina226.cpp:79
sensor::Sensor * shunt_voltage_sensor_
Definition: ina226.h:29
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27