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?
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.
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);
}
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.
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.
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.
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.