ESP32-WROOM-32D
Adafruit INA219 breakout board w/ R020 (0.020) Shunt
- MAX Current = 10A
- MAX Voltage = 32VDC
- Shunt Resistor = 0.020
I’m measuring a +24VDC Load, however, INA219 is only reading roughly 5VDC.
Connections are Source to +Vin and Load to -Vin. SCL, SDA, VS, GND on module are all connected correctly.
#include <Wire.h>
#include <Adafruit_INA219.h>
// Custom definition for 12-bit resolution with 8 samples (4.156 ms conversion time)
#define INA219_CONFIG_BADCRES_12BIT_8S_4MS 0x0078 // Manually defined
// Shunt resistor value
const float shuntResistorValue = 0.02; // 20 milli-ohms
const float maxCurrent = 10.0; // Max current 10A
// Addresses for INA219 sensors
const uint8_t ina219Addresses[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F};
const int NUM_SENSORS = sizeof(ina219Addresses) / sizeof(ina219Addresses[0]);
Adafruit_INA219 ina219[NUM_SENSORS];
bool sensorInitialized[NUM_SENSORS];
// Custom function to write to INA219 register via I2C
void writeINA219Register(uint8_t address, uint8_t reg, uint16_t value) {
Wire.beginTransmission(address);
Wire.write(reg); // Register to write to
Wire.write((value >> 8) & 0xFF); // MSB
Wire.write(value & 0xFF); // LSB
Wire.endTransmission();
}
// Custom calibration function for 32V, 320mV gain, 10A max, and 0.020 mOhm shunt
void setCustomCalibration(uint8_t address) {
// Calculate the current LSB (Least Significant Bit)
// Current LSB = MaxExpected_I / 32767 (Max 15-bit value)
float currentLSB = maxCurrent / 32767.0; // In amperes per bit
currentLSB = 0.000305; // 305 µA per bit for easier calibration
// Calculate the calibration value
// Calibration = trunc(0.04096 / (currentLSB * shuntResistorValue))
uint16_t calValue = (uint16_t)(0.04096 / (currentLSB * shuntResistorValue));
// Write the calibration register (0x05)
writeINA219Register(address, 0x05, calValue);
// Configuration register setup: 32V range, 320mV gain, continuous mode
uint16_t configValue = INA219_CONFIG_BVOLTAGERANGE_32V |
INA219_CONFIG_GAIN_8_320MV |
INA219_CONFIG_BADCRES_12BIT |
INA219_CONFIG_SADCRES_12BIT_16S_8510US |
//INA219_CONFIG_BADCRES_12BIT_8S_4MS |
INA219_CONFIG_BADCRES_12BIT_64S_34MS |
// Custom averaging 12-bit, 8 samples, 4ms
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
// Write the configuration register (0x00)
writeINA219Register(address, 0x00, configValue);
Serial.print("Custom calibration applied for INA219 at 0x");
Serial.println(address, HEX);
}
void setup() {
Serial.begin(115200);
Wire.begin();
for (int i = 0; i < NUM_SENSORS; i++) {
Serial.print("Initializing INA219 at 0x");
Serial.print(ina219Addresses[i], HEX);
ina219[i] = Adafruit_INA219(ina219Addresses[i]);
if (ina219[i].begin()) {
sensorInitialized[i] = true;
Serial.println(" - Initialized");
setCustomCalibration(ina219Addresses[i]); // Apply custom calibration
} else {
sensorInitialized[i] = false;
Serial.println(" - Failed to initialize");
}
}
delay(2000); // Allow time for initialization
}
void readINA219Sensors() {
for (int i = 0; i < NUM_SENSORS; i++) {
if (sensorInitialized[i]) {
float shuntVoltage_mV = ina219[i].getShuntVoltage_mV(); // Shunt voltage in mV
float busVoltage_V = ina219[i].getBusVoltage_V(); // Bus voltage in V
float current_mA = ina219[i].getCurrent_mA(); // Current in mA
float power_mW = ina219[i].getPower_mW(); // Power in mW
float loadvoltage = busVoltage_V + (shuntVoltage_mV / 1000.0);
// Output the readings to the Serial console
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(" at 0x");
Serial.print(ina219Addresses[i], HEX);
Serial.print(": Bus Voltage = ");
Serial.print(busVoltage_V);
Serial.print(" V, Shunt Voltage = ");
Serial.print(shuntVoltage_mV);
Serial.print(" mV, Load Voltage = ");
Serial.print(loadvoltage);
Serial.print(" V, Current = ");
Serial.print(current_mA);
Serial.print(" mA, Power = ");
Serial.print(power_mW);
Serial.println(" mW");
}
}
delay(1000); // Delay between readings
}
void loop() {
readINA219Sensors();
}
This is just a unit test for the module. The code base integrates many modules and has worked as expected before. However, this is a unit test created for INA219 module validation. At this point I’ve completely isolated the INA219 module from the rest of the system. So only the ESP32-WROOM-32D and INA219 module are connected.
a a is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.