ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ina3221.cpp
Go to the documentation of this file.
1#include "ina3221.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::ina3221 {
6
7static const char *const TAG = "ina3221";
8
9static const uint8_t INA3221_REGISTER_CONFIG = 0x00;
10static const uint8_t INA3221_REGISTER_CHANNEL1_SHUNT_VOLTAGE = 0x01;
11static const uint8_t INA3221_REGISTER_CHANNEL1_BUS_VOLTAGE = 0x02;
12static const uint8_t INA3221_REGISTER_CHANNEL2_SHUNT_VOLTAGE = 0x03;
13static const uint8_t INA3221_REGISTER_CHANNEL2_BUS_VOLTAGE = 0x04;
14static const uint8_t INA3221_REGISTER_CHANNEL3_SHUNT_VOLTAGE = 0x05;
15static const uint8_t INA3221_REGISTER_CHANNEL3_BUS_VOLTAGE = 0x06;
16
17// Addresses:
18// A0 = GND -> 0x40
19// A0 = VS -> 0x41
20// A0 = SDA -> 0x42
21// A0 = SCL -> 0x43
22
24 // Config Register
25 // 0bx000000000000000 << 15 RESET Bit (1 -> trigger reset)
26 if (!this->write_byte_16(INA3221_REGISTER_CONFIG, 0x8000)) {
27 this->mark_failed();
28 return;
29 }
30 delay(1);
31
32 uint16_t config = 0;
33 // 0b0xxx000000000000 << 12 Channel Enables (1 -> ON)
34 if (this->channels_[0].exists()) {
35 config |= 0b0100000000000000;
36 }
37 if (this->channels_[1].exists()) {
38 config |= 0b0010000000000000;
39 }
40 if (this->channels_[2].exists()) {
41 config |= 0b0001000000000000;
42 }
43 // 0b0000xxx000000000 << 9 Averaging Mode (0 -> 1 sample, 111 -> 1024 samples)
44 config |= 0b0000000000000000;
45 // 0b0000000xxx000000 << 6 Bus Voltage Conversion time (100 -> 1.1ms, 111 -> 8.244 ms)
46 config |= 0b0000000111000000;
47 // 0b0000000000xxx000 << 3 Shunt Voltage Conversion time (same as above)
48 config |= 0b0000000000111000;
49 // 0b0000000000000xxx << 0 Operating mode (111 -> Shunt and bus, continuous)
50 config |= 0b0000000000000111;
51 if (!this->write_byte_16(INA3221_REGISTER_CONFIG, config)) {
52 this->mark_failed();
53 return;
54 }
55}
56
58 ESP_LOGCONFIG(TAG, "INA3221:");
59 LOG_I2C_DEVICE(this);
60 if (this->is_failed()) {
61 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
62 }
63 LOG_UPDATE_INTERVAL(this);
64
65 LOG_SENSOR(" ", "Bus Voltage #1", this->channels_[0].bus_voltage_sensor_);
66 LOG_SENSOR(" ", "Shunt Voltage #1", this->channels_[0].shunt_voltage_sensor_);
67 LOG_SENSOR(" ", "Current #1", this->channels_[0].current_sensor_);
68 LOG_SENSOR(" ", "Power #1", this->channels_[0].power_sensor_);
69 LOG_SENSOR(" ", "Bus Voltage #2", this->channels_[1].bus_voltage_sensor_);
70 LOG_SENSOR(" ", "Shunt Voltage #2", this->channels_[1].shunt_voltage_sensor_);
71 LOG_SENSOR(" ", "Current #2", this->channels_[1].current_sensor_);
72 LOG_SENSOR(" ", "Power #2", this->channels_[1].power_sensor_);
73 LOG_SENSOR(" ", "Bus Voltage #3", this->channels_[2].bus_voltage_sensor_);
74 LOG_SENSOR(" ", "Shunt Voltage #3", this->channels_[2].shunt_voltage_sensor_);
75 LOG_SENSOR(" ", "Current #3", this->channels_[2].current_sensor_);
76 LOG_SENSOR(" ", "Power #3", this->channels_[2].power_sensor_);
77}
78
79inline uint8_t ina3221_bus_voltage_register(int channel) { return 0x02 + channel * 2; }
80
81inline uint8_t ina3221_shunt_voltage_register(int channel) { return 0x01 + channel * 2; }
82
84 for (int i = 0; i < 3; i++) {
85 INA3221Channel &channel = this->channels_[i];
86 float bus_voltage_v = NAN, current_a = NAN;
87 uint16_t raw;
88 if (channel.should_measure_bus_voltage()) {
90 this->status_set_warning();
91 return;
92 }
93 bus_voltage_v = int16_t(raw) / 1000.0f;
94 if (channel.bus_voltage_sensor_ != nullptr)
95 channel.bus_voltage_sensor_->publish_state(bus_voltage_v);
96 }
97 if (channel.should_measure_shunt_voltage()) {
99 this->status_set_warning();
100 return;
101 }
102 const float shunt_voltage_v = int16_t(raw) * 40.0f / 8.0f / 1000000.0f;
103 if (channel.shunt_voltage_sensor_ != nullptr)
104 channel.shunt_voltage_sensor_->publish_state(shunt_voltage_v);
105 current_a = shunt_voltage_v / channel.shunt_resistance_;
106 if (channel.current_sensor_ != nullptr)
107 channel.current_sensor_->publish_state(current_a);
108 }
109 if (channel.power_sensor_ != nullptr) {
110 channel.power_sensor_->publish_state(bus_voltage_v * current_a);
111 }
112 }
113}
114
115void INA3221Component::set_shunt_resistance(int channel, float resistance_ohm) {
116 this->channels_[channel].shunt_resistance_ = resistance_ohm;
117}
118
120 return this->bus_voltage_sensor_ != nullptr || this->shunt_voltage_sensor_ != nullptr ||
121 this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
122}
124 return this->shunt_voltage_sensor_ != nullptr || this->current_sensor_ != nullptr || this->power_sensor_ != nullptr;
125}
127 return this->bus_voltage_sensor_ != nullptr || this->power_sensor_ != nullptr;
128}
129
130} // namespace esphome::ina3221
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
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
struct esphome::ina3221::INA3221Component::INA3221Channel channels_[3]
void set_shunt_resistance(int channel, float resistance_ohm)
Definition ina3221.cpp:115
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint8_t ina3221_shunt_voltage_register(int channel)
Definition ina3221.cpp:81
uint8_t ina3221_bus_voltage_register(int channel)
Definition ina3221.cpp:79
void HOT delay(uint32_t ms)
Definition hal.cpp:85