INA219 Incorrect Voltage Readings

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.

New contributor

a a is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật