ESPHome 2025.9.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 {
6namespace ina260 {
7
8static const char *const TAG = "ina260";
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
28static const uint8_t INA260_REGISTER_CONFIG = 0x00;
29static const uint8_t INA260_REGISTER_CURRENT = 0x01;
30static const uint8_t INA260_REGISTER_BUS_VOLTAGE = 0x02;
31static const uint8_t INA260_REGISTER_POWER = 0x03;
32static const uint8_t INA260_REGISTER_MASK_ENABLE = 0x06;
33static const uint8_t INA260_REGISTER_ALERT_LIMIT = 0x07;
34static const uint8_t INA260_REGISTER_MANUFACTURE_ID = 0xFE;
35static const uint8_t INA260_REGISTER_DEVICE_ID = 0xFF;
36
38 // Reset device on setup
39 if (!this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) {
40 this->error_code_ = DEVICE_RESET_FAILED;
41 this->mark_failed();
42 return;
43 }
44
45 delay(2);
46
47 this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
48 this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
49
50 if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
51 this->error_code_ = COMMUNICATION_FAILED;
52 this->mark_failed();
53 return;
54 }
55
56 if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) {
57 this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
58 this->mark_failed();
59 return;
60 }
61}
62
64 ESP_LOGCONFIG(TAG, "INA260:");
65 LOG_I2C_DEVICE(this);
66 LOG_UPDATE_INTERVAL(this);
67
68 ESP_LOGCONFIG(TAG, " Manufacture ID: 0x%x", this->manufacture_id_);
69 ESP_LOGCONFIG(TAG, " Device ID: 0x%x", this->device_id_);
70
71 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
72 LOG_SENSOR(" ", "Current", this->current_sensor_);
73 LOG_SENSOR(" ", "Power", this->power_sensor_);
74
75 switch (this->error_code_) {
77 ESP_LOGE(TAG, "Connected device does not match a known INA260 sensor");
78 break;
80 ESP_LOGE(TAG, "Device reset failed - Is the device connected?");
81 break;
83 ESP_LOGE(TAG, "Failed to update device configuration");
84 break;
85 case NONE:
86 default:
87 break;
88 }
89}
90
92 if (this->bus_voltage_sensor_ != nullptr) {
93 uint16_t raw_bus_voltage;
94 if (!this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
95 this->status_set_warning();
96 return;
97 }
98 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
99 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
100 }
101
102 if (this->current_sensor_ != nullptr) {
103 uint16_t raw_current;
104 if (!this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)) {
105 this->status_set_warning();
106 return;
107 }
108 float current_a = int16_t(raw_current) * 0.00125f;
109 this->current_sensor_->publish_state(current_a);
110 }
111
112 if (this->power_sensor_ != nullptr) {
113 uint16_t raw_power;
114 if (!this->read_byte_16(INA260_REGISTER_POWER, &raw_power)) {
115 this->status_set_warning();
116 return;
117 }
118 float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
119 this->power_sensor_->publish_state(power_w);
120 }
121
122 this->status_clear_warning();
123}
124
125} // namespace ina260
126} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
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
enum esphome::ina260::INA260Component::ErrorCode NONE
sensor::Sensor * bus_voltage_sensor_
Definition ina260.h:24
sensor::Sensor * current_sensor_
Definition ina260.h:25
sensor::Sensor * power_sensor_
Definition ina260.h:26
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
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