Using SCL2 and SDA2 on Teensy 4.1

Hi, I have five Adafruit INA219 current sensors that are connected to my Teensy's SCL2 and SDA2 lines. I ran a I2C scanner on those lines and I am able to see that the Teensy is seeing all of the sensors. However, I can't seem to figure out how to communicate with them.

I am trying to edit the sample sketch just to test them, the address for one of them is 0x44 which I have set on line 4.

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219(0x44);


void setup(void) 
{
  Serial.begin(115200);
  while (!Serial) {
      // will pause Zero, Leonardo, etc until serial console opens
      delay(1);
  }

  uint32_t currentFrequency;
    
  Serial.println("Hello!");
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  //ina219.setCalibration_16V_400mA();

  Serial.println("Measuring voltage and current with INA219 ...");
}

void loop(void) 
{
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("");

  delay(2000);
}

Anybody have any idea how to read from the third I2C line and read from multiple sensors?

Yes, each sensor must have its unique address, the I2C protocol works like this.

If it is not possible to use a different address, it may be possible to control the activation of the slave device, as is done in SPI communication.

I found a case where the user 'switches devices' using analog switches:

Multiplexing with the TCA9548A

Up to 4 boards may be connected. Addressing is as follows:
Board 0: Address = 0x40 Offset = binary 00000 (no jumpers required)
Board 1: Address = 0x41 Offset = binary 00001 (bridge A0 as in the photo above)
Board 2: Address = 0x44 Offset = binary 00100 (bridge A1)
Board 3: Address = 0x45 Offset = binary 00101 (bridge A0 & A1)

Knowing this, it may be necessary 'to switch' only 2 devices with the same address. Being 2 switched with the same address, plus 3 with unique addresses.

Another option would be to use two more microcontroller pins to create a new I2C bus:

P.S.: It will probably be necessary to disable interrupts to operate with I2C, if something strange happens in the communication, check this.

Each have their own address and I have already identified them. When I run the I2C scanner I am able to see each of them and their addresses. I am just wondering what I need to put in my code to use SCL2 and SDA2. When I hook them up to SCL and SDA they work, so there is something I need to change in my sketch to switch it to Wire2. I can't seem to figure out what it is though.

Did you look at the source code for the Adafruit library? From Adafruit_INA219.h:

public:
  Adafruit_INA219(uint8_t addr = INA219_ADDRESS);
  ~Adafruit_INA219();
  bool begin(TwoWire *theWire = &Wire);

So, it looks like you should supply the begin() function with a pointer to the Wire2 object.

1 Like

&Wire

Yeah, I looked in the header file earlier and saw that, that's how I knew it was possible to use the sensor with SCL2 and SDA2. It seems that some sensors aren't cable of using Wire1 & Wire2 out of the box.

I'm not too familiar with how the pointer works, could you explain or show me how to incorporate it into my code?

The sensor has no idea which I2C interface it's connected to.

Instead of

ina219.begin()

Try

ina219.begin(&Wire2)

That worked, thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.