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