ESPHome 2026.12.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
7ESPHOME_LOG_TAG(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->setup_check(this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000),
39 LOG_STR("Device reset failed - Is the device connected?")))
40 return;
41
42 delay(2);
43
44 this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
45 this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
46
47 if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
48 this->mark_failed(LOG_STR("Connected device does not match a known INA260 sensor"));
49 return;
50 }
51
52 if (!this->setup_check(this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111),
53 LOG_STR("Failed to update device configuration")))
54 return;
55}
56
57void INA260Component::dump_config() {
58 ESP_LOGCONFIG(TAG,
59 "INA260:\n"
60 " Manufacture ID: 0x%x\n"
61 " Device ID: 0x%x",
62 this->manufacture_id_, this->device_id_);
63 LOG_I2C_DEVICE(this);
64 LOG_UPDATE_INTERVAL(this);
65 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
66 LOG_SENSOR(" ", "Current", this->current_sensor_);
67 LOG_SENSOR(" ", "Power", this->power_sensor_);
68}
69
70void INA260Component::update() {
71 if (this->bus_voltage_sensor_ != nullptr) {
72 uint16_t raw_bus_voltage;
73 if (!this->status_comm_check(this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)))
74 return;
75 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
76 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
77 }
78
79 if (this->current_sensor_ != nullptr) {
80 uint16_t raw_current;
81 if (!this->status_comm_check(this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)))
82 return;
83 float current_a = int16_t(raw_current) * 0.00125f;
84 this->current_sensor_->publish_state(current_a);
85 }
86
87 if (this->power_sensor_ != nullptr) {
88 uint16_t raw_power;
89 if (!this->status_comm_check(this->read_byte_16(INA260_REGISTER_POWER, &raw_power)))
90 return;
91 float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
92 this->power_sensor_->publish_state(power_w);
93 }
94
96}
97
98} // namespace esphome::ina260
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:233
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
Definition component.h:306
void status_clear_warning()
Definition component.h:315
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:116
ESPHOME_LOG_TAG(TAG, "ina260")
void HOT delay(uint32_t ms)
Definition hal.cpp:85