ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ina260.cpp
Go to the documentation of this file.
1#include "ina260.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::ina260 {
6
7static const char *const TAG = "ina260";
8
9// | A0 | A1 | Address |
10// | GND | GND | 0x40 |
11// | GND | V_S+ | 0x41 |
12// | GND | SDA | 0x42 |
13// | GND | SCL | 0x43 |
14// | V_S+ | GND | 0x44 |
15// | V_S+ | V_S+ | 0x45 |
16// | V_S+ | SDA | 0x46 |
17// | V_S+ | SCL | 0x47 |
18// | SDA | GND | 0x48 |
19// | SDA | V_S+ | 0x49 |
20// | SDA | SDA | 0x4A |
21// | SDA | SCL | 0x4B |
22// | SCL | GND | 0x4C |
23// | SCL | V_S+ | 0x4D |
24// | SCL | SDA | 0x4E |
25// | SCL | SCL | 0x4F |
26
27static const uint8_t INA260_REGISTER_CONFIG = 0x00;
28static const uint8_t INA260_REGISTER_CURRENT = 0x01;
29static const uint8_t INA260_REGISTER_BUS_VOLTAGE = 0x02;
30static const uint8_t INA260_REGISTER_POWER = 0x03;
31static const uint8_t INA260_REGISTER_MASK_ENABLE = 0x06;
32static const uint8_t INA260_REGISTER_ALERT_LIMIT = 0x07;
33static const uint8_t INA260_REGISTER_MANUFACTURE_ID = 0xFE;
34static const uint8_t INA260_REGISTER_DEVICE_ID = 0xFF;
35
37 // Reset device on setup
38 if (!this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) {
39 this->error_code_ = DEVICE_RESET_FAILED;
40 this->mark_failed();
41 return;
42 }
43
44 delay(2);
45
46 this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
47 this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
48
49 if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
50 this->error_code_ = COMMUNICATION_FAILED;
51 this->mark_failed();
52 return;
53 }
54
55 if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) {
56 this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
57 this->mark_failed();
58 return;
59 }
60}
61
63 ESP_LOGCONFIG(TAG,
64 "INA260:\n"
65 " Manufacture ID: 0x%x\n"
66 " Device ID: 0x%x",
67 this->manufacture_id_, this->device_id_);
68 LOG_I2C_DEVICE(this);
69 LOG_UPDATE_INTERVAL(this);
70 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
71 LOG_SENSOR(" ", "Current", this->current_sensor_);
72 LOG_SENSOR(" ", "Power", this->power_sensor_);
73
74 switch (this->error_code_) {
76 ESP_LOGE(TAG, "Connected device does not match a known INA260 sensor");
77 break;
79 ESP_LOGE(TAG, "Device reset failed - Is the device connected?");
80 break;
82 ESP_LOGE(TAG, "Failed to update device configuration");
83 break;
84 case NONE:
85 default:
86 break;
87 }
88}
89
91 if (this->bus_voltage_sensor_ != nullptr) {
92 uint16_t raw_bus_voltage;
93 if (!this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
94 this->status_set_warning();
95 return;
96 }
97 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
98 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
99 }
100
101 if (this->current_sensor_ != nullptr) {
102 uint16_t raw_current;
103 if (!this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)) {
104 this->status_set_warning();
105 return;
106 }
107 float current_a = int16_t(raw_current) * 0.00125f;
108 this->current_sensor_->publish_state(current_a);
109 }
110
111 if (this->power_sensor_ != nullptr) {
112 uint16_t raw_power;
113 if (!this->read_byte_16(INA260_REGISTER_POWER, &raw_power)) {
114 this->status_set_warning();
115 return;
116 }
117 float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
118 this->power_sensor_->publish_state(power_w);
119 }
120
121 this->status_clear_warning();
122}
123
124} // namespace esphome::ina260
void mark_failed()
Mark this component as failed.
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
enum esphome::ina260::INA260Component::ErrorCode NONE
sensor::Sensor * bus_voltage_sensor_
Definition ina260.h:23
sensor::Sensor * current_sensor_
Definition ina260.h:24
sensor::Sensor * power_sensor_
Definition ina260.h:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85