MULTIPLE I2C connections not working

Hi all,

I am struggling to connect 2 devices to the I2C. I got the followings:

ARDUINO MEGA2560
DS1307
GY-521

I have successfully managed to get them working separately with the example code I found online but the problem starts when I connect both of the devices at the same time. Initially I thought I have made a mistake in merging the codes, therefore I have used the code only for the DS1307 (working one) and it works fine until the GY-521 is connected to teh I2C bus. When the GY-521 is connected the time is not displayed correctly. The same happens viceversa, using the code for the GY-521 works fine until the DS1307 in connected to I2C. I have removed the pull up resistor from the DY-521 so there is only 1 pull up for the I2C line but still nothing worked properly >:( . Is there anyone who knows how to solve this issue?

Thanks in advance :slight_smile:

What do you see in serial monitor if you run i2c scanner sketch with both devices connected?

I can see both of them, 0x50 and 0x68

Good, sounds like a problem in your sketch then. If you feel like posting it, read the forum guide in the sticky post first, so you don't break the rules.

I don't have a mega, but I do have nano, mpu6050 (=gy-521) and ds3231, so I can test your sketch.

Hi,

I have attached the code.

Thanks for your help :slight_smile:

both.ino (1.33 KB)

PaulRB:
read the forum guide in the sticky post first, so you don't break the rules.

manpreet1335:
I have attached the code.

:o

Here's his code :wink:

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

//installare la RTCLib
#include "RTClib.h"

RTC_DS1307 rtc;

void setup() {
  
  Serial.begin(2000000);
  
  if (!rtc.begin()) {
    Serial.println("Errore! Verifica le connesioni!");
    return;
  }
  
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
   return;
  }
  

  if (!rtc.isrunning()) {
    Serial.println("Configuro l'ora");
    //Uso l'ora di compilazione dello sketch
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // per farlo a mano...
    // January 21, 2014 at 3am you would call
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

    if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    }

    mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
    mpu.setGyroRange(MPU6050_RANGE_2000_DEG);
    mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
    
}


void loop() {
  //chiedo l'ora

  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  Serial.print(a.acceleration.z);
  Serial.print("-------");

  
  DateTime now = rtc.now();
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  

  delay(1000);
}

How do you do that? there is only one option to attach files. :o

Thanks though

manpreet1335:
How do you do that?

PaulRB:
read the forum guide in the sticky post

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

//installare la RTCLib
#include "RTClib.h"

RTC_DS1307 rtc;

void setup() {
  
  Serial.begin(2000000);
  
  if (!rtc.begin()) {
    Serial.println("Errore! Verifica le connesioni!");
    return;
  }
  
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
   return;
  }
  

  if (!rtc.isrunning()) {
    Serial.println("Configuro l'ora");
    //Uso l'ora di compilazione dello sketch
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // per farlo a mano...
    // January 21, 2014 at 3am you would call
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

    if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    }

    mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
    mpu.setGyroRange(MPU6050_RANGE_2000_DEG);
    mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
    
}


void loop() {
  //chiedo l'ora

  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  Serial.print(a.acceleration.z);
  Serial.print("-------");

  
  DateTime now = rtc.now();
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  

  delay(1000);
}

@manpreet1335 your code calls "mpu.begin()" twice in setup(). Try removing one of those.

Still not working correctly. Something I just noticed is that when both of the sensors are connected prior to powering arduino I see the "Failed to find MPU6050 chip" message in the console. While if I connect only one of them it works fine displaying the correct values until the second module is connected after which the data is not correct anymore.

Example 1 if only the RTC is connected, the time is displayed correctly until the MPU6050 is connected. After which the only few numbers are displayed correct (if the time is 11:30:45 it is displayed as 1:30:40 or 11:26:6 is displayed as 1:2:4) . While the MPU6050 outputs a different range of values which vary but not as expected.

Example 2 if only the MPU6050 is connected then it displayed the correct acceleration but as soon as the RTC is conneted the value range changes and it doesn't seem to be correct. while the time is behaving like before (wrong values).

Here's the slightly modified code where there is only 1 mpu.begin and functions are moved around.

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

//installare la RTCLib
#include "RTClib.h"

RTC_DS1307 rtc;

void setup() {
  
  Serial.begin(2000000);

  
  if (!rtc.begin()) {
    Serial.println("Errore! Verifica le connesioni!");
    return;
  }
  
  if (!rtc.isrunning()) {
    Serial.println("Configuro l'ora");
    //Uso l'ora di compilazione dello sketch
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // per farlo a mano...
    // January 21, 2014 at 3am you would call
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }



    if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
   return;
  }

    mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
    mpu.setGyroRange(MPU6050_RANGE_2000_DEG);
    mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
    
}


void loop() {
 
  DateTime now = rtc.now();
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);

  Serial.print("-------");

  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  Serial.println(a.acceleration.z);

  delay(1000);
}

It would be helpful for us, and for you, to spot errors in your code if you perform Auto-format (Tools menu) before you post it.

  Serial.begin(2000000);

This is not a standard baud rate, and very fast, so could maybe cause errors on the serial monitor. Try 115200 or 57600.

Just tried that doesn't make any difference. I have also tried 9600. Can you think of any hardware issue? Because when I connect only one it works perfectly.

Thanks

I don't think the serial hardware on mega can achieve 2000000 baud, so I expect it will be defaulting to the highest available speed. I was thinking that the highest available speed would not be very reliable, and adding one more component to the circuit could perhaps cause it to stop working. It was worth trying.

I think I have found the problem. I used the I2C scanner and here's what I found.

Connecting both of the sensors the addresses found are:
0x68
0x50

Connecting only the MPU6050:
0x68

Connecting only the DS1307:
0x50
0x68

How is it possible the RTC has 2 addresses?! is there any way of changing this?

How is it possible the RTC has 2 addresses?

The DS1307 has some eeprom, hence the second address.

Thanks guys!!!!

I will try doing some research on how to change the i2c address or any of you guys knows already? could save me some time :slight_smile:

Some modules have address pins, and you ground one or more of them to change the address. But afaik, that has to be a feature of the actual chip itself.

Not sure if those chips have that ability?

found it!! :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Having a quick read through the datasheet of the MPU6050 I found that "AD0" being equal to 1 or 0 can change the address. It turns out by putting vcc to AD0 can change the i2c address of the board from 0x68 to 0x69.

Thanks for your help guys. appreciate it!!!1 :slight_smile: