ESPHome 2026.11.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->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) {
39 this->mark_failed(LOG_STR("Device reset failed - Is the device connected?"));
40 return;
41 }
42
43 delay(2);
44
45 this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
46 this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
47
48 if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
49 this->mark_failed(LOG_STR("Connected device does not match a known INA260 sensor"));
50 return;
51 }
52
53 if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) {
54 this->mark_failed(LOG_STR("Failed to update device configuration"));
55 return;
56 }
57}
58
60 ESP_LOGCONFIG(TAG,
61 "INA260:\n"
62 " Manufacture ID: 0x%x\n"
63 " Device ID: 0x%x",
64 this->manufacture_id_, this->device_id_);
65 LOG_I2C_DEVICE(this);
66 LOG_UPDATE_INTERVAL(this);
67 LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
68 LOG_SENSOR(" ", "Current", this->current_sensor_);
69 LOG_SENSOR(" ", "Power", this->power_sensor_);
70}
71
73 if (this->bus_voltage_sensor_ != nullptr) {
74 uint16_t raw_bus_voltage;
75 if (!this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
76 this->status_set_warning();
77 return;
78 }
79 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
80 this->bus_voltage_sensor_->publish_state(bus_voltage_v);
81 }
82
83 if (this->current_sensor_ != nullptr) {
84 uint16_t raw_current;
85 if (!this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)) {
86 this->status_set_warning();
87 return;
88 }
89 float current_a = int16_t(raw_current) * 0.00125f;
90 this->current_sensor_->publish_state(current_a);
91 }
92
93 if (this->power_sensor_ != nullptr) {
94 uint16_t raw_power;
95 if (!this->read_byte_16(INA260_REGISTER_POWER, &raw_power)) {
96 this->status_set_warning();
97 return;
98 }
99 float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
100 this->power_sensor_->publish_state(power_w);
101 }
102
103 this->status_clear_warning();
104}
105
106} // namespace esphome::ina260
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
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:101
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:65
ESPHOME_LOG_TAG(TAG, "ina260")
void HOT delay(uint32_t ms)
Definition hal.cpp:85