I2C conflicts on WiFi 32Kits when integrating INA219 sensor

I connected an INA219 sensor via I2C pins SDA (GPIO#21)& SCL(GPIO#22). The I2C address of the INA219 is 0x40. The following code works great. As soon as I add the SSD1306 OLED display code (I2C address 0x3C), the INA219 reading is no longer valid. The OLED display still works fine. Here is the code:


#include <Wire.h>
#include <Adafruit_INA219.h>
#include “heltec.h”

Adafruit_INA219 ina219;

void setup(void)
{
Serial.begin(115200);
while (!Serial) {
delay(1);
}

// Initialize the INA219.
if (! ina219.begin()) {
Serial.println(“Failed to find INA219 chip”);
while (1) { delay(10); }
}

// Initialize OLED display SSD1306
Heltec.begin(true /DisplayEnable Enable/, false /LoRa Disable/, true /Serial Enable/);
Heltec.display->init();
Heltec.display->flipScreenVertically();
Heltec.display->setFont(ArialMT_Plain_16);
Heltec.display->clear();
Heltec.display->drawString(8, 0, “INA219 integration”);
Heltec.display->display();
}

void loop(void)
{
float current_mA = 0;
float loadvoltage = 0;

current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);

Serial.print(“Load Voltage: “); Serial.print(loadvoltage); Serial.println(” V”);
Serial.print(“Current: “); Serial.print(current_mA); Serial.println(” mA”);

delay(5000);
}


I came across a similar problem when trying to integrate the SCD41 CO2 sensor via I2C interface. I am not sure how to setup the I2C correctly to interface both the SSD1306 and INA219 simultaneously. Can you suggest a solution?

PeterHsi75

ESP32 have two I2C bus, you can use another(wire1) to connect your sensor.

Thank you for your quick response.

  1. Yes, I used wire() to scan I2C0 and found OLED display @0x3C and used wire1() to scan the I2C1 bus and found the INA219 device at 0x40. However, I don’t know how to change wire() to wire1() inside the Adafruit_INA219 ina219 library. These standard Arduino libraries all seem to use wire() function.

  2. The other question is why does the INA219 code work before I added Heltec.display() function?

PeterHsi75

I resolve the problem. Here is thee code to successfully initialize both INA219 & SSD1306 on the same I2C0 bus in the setup().

Setup()
{

// Initialize OLED display SSD1306
Wire.begin(SDA_OLED, SCL_OLED); //Scan OLED’s I2C address via I2C0
Heltec.display->init();
……

// Without the following codes, the ina219.begin() cannot detect INA219 for some reasons(?)

// check INA219 on the I2C0 bus
address=0x40; // address of INA219
Wire.beginTransmission(address);
error = Wire.endTransmission();
…….
// Initialize the INA219.
if (! ina219.begin()) {
Serial.println(“Failed to find INA219 chip”);
while (1) { delay(10); }
……
}