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

Thread: How to assign sensors to I2C buses T4.1? :confused:

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

    Sensors not found through Multiplexer on T4.1?

    Hi All, and thanks for the help so far!

    I'm trying to connect two MPU6050s to a T4.1 via i2c and the Adafruit TCA9548A. I am able to use a modified i2c scanner and see the sensors, but when I use the following sketch it fails to connect to the sensors. I've meticulously checked the wiring so I don't think that's it (but I've included a fritzing just in case). I'm also including the i2c scanner I used that was able to detect the sensors.

    Thank you for any help!!!

    Sketch that is not working:
    Code:
    #include "MPU6050.h"
    #include <Wire.h>
    
    #define TCAADDR 0x70
    
    MPU6050 mpu1;
    MPU6050 mpu2;
    
    int16_t ax, ay, az;
    int16_t gx, gy, gz;
    
    void tcaselect(uint8_t i) {
      if (i > 7) return;
    
      Wire.beginTransmission(TCAADDR);
      Wire.write(1 << i);
      Wire.endTransmission();
    }
    
    void setup(void)
    {
      Serial.begin(38400);
    
      Serial.println("2 MPU 6050s on multiplexer Test"); Serial.println("");
      Serial.println("Initializing I2C devices...");
      /* Initialise the sensors */
      tcaselect(2);
      mpu1.initialize();
      Serial.println(mpu1.testConnection() ? "MPU1 connection successful" : "MPU1 connection failed"); // returns connection failed
    
      tcaselect(6);
      mpu2.initialize();
      Serial.println(mpu2.testConnection() ? "MPU2 connection successful" : "MPU2 connection failed"); // returns connection failed
    
      Serial.println("");
      delay(3000);
    }
    
    void loop(void)
    {
    
      tcaselect(2);
      mpu1.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
      Serial.print("Sensor 1 accel:\t");
      Serial.print(ax); Serial.print("\t");
      Serial.print(ay); Serial.print("\t");
      Serial.print(az); Serial.print("\t");
      Serial.print(gx); Serial.print("\t");
      Serial.print(gy); Serial.print("\t");
      Serial.println(gz);
    
      tcaselect(6);
      mpu2.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
      Serial.print("Sensor 2 accel:\t");
      Serial.print(ax); Serial.print("\t");
      Serial.print(ay); Serial.print("\t");
      Serial.print(az); Serial.print("\t");
      Serial.print(gx); Serial.print("\t");
      Serial.print(gy); Serial.print("\t");
      Serial.println(gz);
    
      delay(500);
    
    }
    Modified i2c scanner:
    Code:
    /**
     * TCA9548 I2CScanner.pde -- I2C bus scanner for Arduino
     *
     * Based on code c. 2009, Tod E. Kurt, http://todbot.com/blog/
     *
     */
    
    #include "Wire.h"
    extern "C"
    uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
    {
            if (!wait) return 4;
            Wire.beginTransmission(address);
            while (length) {
                    Wire.write(*data++);
                    length--;
            }
            return Wire.endTransmission(sendStop);
    }
    
    #define TCAADDR 0x70
    
    void tcaselect(uint8_t i) {
      if (i > 7) return;
     
      Wire.beginTransmission(TCAADDR);
      Wire.write(1 << i);
      Wire.endTransmission();  
    }
    
    
    // standard Arduino setup()
    void setup()
    {
        while (!Serial);
        delay(1000);
    
        Wire.begin();
        
        Serial.begin(115200);
        Serial.println("\nTCAScanner ready!");
        
        for (uint8_t t=0; t<8; t++) {
          tcaselect(t);
          Serial.print("TCA Port #"); Serial.println(t);
    
          for (uint8_t addr = 0; addr<=127; addr++) {
            if (addr == TCAADDR) continue;
          
            uint8_t data;
            if (! twi_writeTo(addr, &data, 0, 1, 1)) {
               Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
            }
          }
        }
        Serial.println("\ndone");
    }
    
    void loop() 
    {
    }
    Click image for larger version. 

Name:	2MPU6050_T41_Multi_bb.png 
Views:	5 
Size:	188.5 KB 
ID:	22220
    Last edited by humanpowered; 10-27-2020 at 08:40 PM. Reason: wrong title

Posting Permissions

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