Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Multiple I2C buses on T4.1

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    10

    Multiple I2C buses on T4.1

    I am trying to connect two MPU6050s to my T4.1 but can't get the i2c scanner to recognize both of them. I'm using the standard bus Wire(SCL 19 and SDA 18) and the second Wire1(SCL 16 SDA 17). With my current sketch, it doesn't recognize Wire only Wire1 (when I disconnect Wire1 it says no i2c). If I go back to the original it recognizes Wire, but not Wire1. I've done a lot of searching but couldn't find a solution to my problem.

    Here is the code I am using with some comments where I think I've gone wrong. Any help is greatly appreciated.

    Code:
    #include <Wire.h>
    
    void setup()
    {
      Wire.setSCL(19);
      Wire.setSDA(18);
      Wire1.setSCL(16);
      Wire1.setSDA(17);
      
      Wire.begin();
      Wire1.begin();
    
      Serial.begin(9600);
      while (!Serial);           
      Serial.println("\nI2C Scanner");
    }
    
    
    void loop()
    {
      byte error, address;
      int nDevices;
    
      Serial.println("Scanning...");
    
      nDevices = 0;
      for(address = 1; address < 127; address++ ) //I'm guessing I haven't updated this correctly
      {
        // The i2c_scanner uses the return value of
        // the Write.endTransmisstion to see if
        // a device did acknowledge to the address.
        Wire.beginTransmission(address);
        Wire1.beginTransmission(address); // same thing here
        error = Wire.endTransmission();    
        error = Wire1.endTransmission(); // and here
    
        if (error == 0)
        {
          Serial.print("I2C device found at address 0x");
          if (address<16) 
            Serial.print("0");
          Serial.print(address,HEX);
          Serial.println("  !");
    
          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\n");
      else
        Serial.println("done\n");
    
      delay(5000);   
    }

  2. #2
    Senior Member
    Join Date
    Nov 2015
    Location
    Cold hollow VT
    Posts
    169
    I may be wrong here, but I don't recall Wire.h supporting the other I2C busses. I do believe it can be edited and renamed as wire1.h to support each additional I2C bus.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    23,027
    I believe these lines are likely the problem.

    Code:
        error = Wire.endTransmission();    
        error = Wire1.endTransmission(); // and here
    Results from Wire1 activity are immediately overwriting the result from Wire before your program is able to show you that info.

  4. #4
    Junior Member
    Join Date
    Oct 2020
    Posts
    10
    Quote Originally Posted by PaulStoffregen View Post
    I believe these lines are likely the problem.

    Code:
        error = Wire.endTransmission();    
        error = Wire1.endTransmission(); // and here
    Results from Wire1 activity are immediately overwriting the result from Wire before your program is able to show you that info.
    Thanks Paul! That took care of it. In case anyone else is looking for a solution to this problem here's the sketch I used including all three i2c buses on the T4.1. I've also added a total count of i2c devices so even if you overwrite the address you can still see the total count which should help you understand if the problem is in the wiring or in the sketch.

    Code:
    // Version T4.1
    //    This program (or code that looks like it)
    //    can be found in many places.
    //    For example on the Arduino.cc forum.
    //    The original author is not know.
    
    #include <Wire.h>
    
    void setup()
    {
      Wire.setSCL(19);  // SCL on first i2c bus on T4.1
      Wire.setSDA(18);  // SDA on first i2c bus on T4.1
      Wire1.setSCL(16); // SCL1 on second i2c bus on T4.1
      Wire1.setSDA(17); // SDA1 on second i2c bus on T4.1
      Wire2.setSCL(24); // SCL2 on second i2c bus on T4.1
      Wire2.setSDA(25); // SDA2 on second i2c bus on T4.1
    
      Wire.begin();
      Wire1.begin();
      Wire2.begin();
    
      Serial.begin(9600);
      while (!Serial);             // Leonardo: wait for serial monitor
      Serial.println("\nI2C Scanner");
    }
    
    
    void loop()
    {
      byte error, address;
      int nDevices;
    
      Serial.println("Scanning...");
    
      nDevices = 0;
      for (address = 1; address < 127; address++ ) //I'm guessing I haven't updated this correctly
      {
        // The i2c_scanner uses the return value of
        // the Write.endTransmisstion to see if
        // a device did acknowledge to the address.
    
        //---------------------------------------------------------------------------------
        Wire.beginTransmission(address); // i2c on first bus starts here
        error = Wire.endTransmission();
    
        if (error == 0)
        {
          Serial.print("I2C device found at Wire 0x");
          if (address < 16)
            Serial.print("0");
          Serial.print(address, HEX);
          Serial.println("  !");
    
          nDevices++;
        }
        else if (error == 4)
        {
          Serial.print("Unknown error at Wire 0x");
          if (address < 16)
            Serial.print("0");
          Serial.println(address, HEX);
        } // i2c on first bus ends here
    
        //---------------------------------------------------------------------------------
        Wire1.beginTransmission(address); // i2c on second bus starts here
        error = Wire1.endTransmission();
    
        if (error == 0)
        {
          Serial.print("I2C device found at Wire1 0x");
          if (address < 16)
            Serial.print("0");
          Serial.print(address, HEX);
          Serial.println("  !");
    
          nDevices++;
        }
        else if (error == 4)
        {
          Serial.print("Unknown error at Wire1 0x");
          if (address < 16)
            Serial.print("0");
          Serial.println(address, HEX);
        }
                                       // i2c on second bus ends here
    
      //---------------------------------------------------------------------------------
    
      Wire2.beginTransmission(address); // i2c on third bus starts here
        error = Wire2.endTransmission();
    
        if (error == 0)
        {
          Serial.print("I2C device found at Wire2 0x");
          if (address < 16)
            Serial.print("0");
          Serial.print(address, HEX);
          Serial.println("  !");
    
          nDevices++;
        }
        else if (error == 4)
        {
          Serial.print("Unknown error at Wire2 0x");
          if (address < 16)
            Serial.print("0");
          Serial.println(address, HEX);
        }
      }                                 // i2c on third bus ends here
    
     //---------------------------------------------------------------------------------
    
      if (nDevices == 0)
        Serial.println("No I2C devices found\n");
      else
        Serial.print("number of devices found = ");
      Serial.println(nDevices);  // this prints the total number of i2c devices found
      Serial.println("done\n");
    
      delay(2000);           // wait 2 seconds for next scan
    }

  5. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    10
    Hi Grease,

    Apparently Wire does support more than one without creating a new library. See the example I put on my previous comment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •