two I2Cs simultaneously,

Post here first, or if you can't find a relevant section!
Post Reply
madhavan
Posts: 29
Joined: Fri May 21, 2021 1:22 am

two I2Cs simultaneously,

Post by madhavan »

hi, i am trying to use two i2c device simultaneously . Individually single port is function is ok like wise PB7 -SDA , PB6 -SCL or PB11-SDA & PB10 -SCL

when i am scanning first i2c port with just

Wire,begin(); it's giving first device address details ....

When i am scanning second i2c port with

Wire.setSDA(PB11);
Wire.setSCL(PB10);
Wire.begin(); it's giving asecond device address details ....

When i try to use simultaneously - only first device address is coming ..no second device address

Code: Select all

#include <Wire.h>

void setup() {

  Serial.begin(9600);
  Wire.begin();
  
  TwoWire Wire2(PB11,PB10);
  Wire2.begin();
  Serial.println("\nI2C Scanner");
}


void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.

    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) 
        Serial.print("0");
      Serial.println(address, HEX);

      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) 
        Serial.print("0");
      Serial.println(address, HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found");
  else
    Serial.println("done");

  delay(5000);           // wait 5 seconds for next scan
}
Board: blue pill . Core : Stm32 Duino
by GonzoG » Thu Sep 23, 2021 10:16 pm
madhavan wrote: Tue Sep 21, 2021 1:31 pm

Code: Select all

#include <Wire.h>

void setup() {

  Serial.begin(9600);
  Wire.begin();
  
  TwoWire Wire2(PB11,PB10);
  Wire2.begin();
  Serial.println("\nI2C Scanner");
}
It won't work.
You define Wire2 only in setup, and it's not accessible outside setup() function.
You need to define it as a global object.

Code: Select all

#include <Wire.h>
TwoWire Wire2(PB11,PB1);

void setup(){
...
}
Go to full post
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: two I2Cs simultaneously,

Post by mrburnette »

I2C is generally used as a "bus" topology when 2 or more devices are needed:
https://electronics.stackexchange.com/q ... sda-and-a5

If 2 separate I2C embedded controllers are used, two I2C instances are required, but this is really wasteful on SRAM resources as 2 object variables must be in RAM. The consideration is that both devices have different addresses ... some devices can be jumpered to avoid address conflicts.

I2C.png
I2C.png (6.84 KiB) Viewed 2086 times
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: two I2Cs simultaneously,

Post by GonzoG »

madhavan wrote: Tue Sep 21, 2021 1:31 pm

Code: Select all

#include <Wire.h>

void setup() {

  Serial.begin(9600);
  Wire.begin();
  
  TwoWire Wire2(PB11,PB10);
  Wire2.begin();
  Serial.println("\nI2C Scanner");
}
It won't work.
You define Wire2 only in setup, and it's not accessible outside setup() function.
You need to define it as a global object.

Code: Select all

#include <Wire.h>
TwoWire Wire2(PB11,PB1);

void setup(){
...
}
Post Reply

Return to “General discussion”