GY-521(MPU6050) and DS3231 can not be used together on arduino UNO

Nice to meet you here. This is my first post here. I am building a prototype on Arduino UNO R3 with a DS3231 clock module and a GY-521(MPU6050) accelerator module. Here is the overall process and the current problem I have.

Here is the code

#include "I2Cdev.h"
#include "MPU6050.h"
#include <DS3231.h>
#include "Wire.h"
MPU6050 accelgyro;
DS3231 Clock;

int16_t ax, ay, az;
int16_t gx, gy, gz;
bool c = false;

void setup()
{
Wire.begin();
Serial.begin(115200);
accelgyro.initialize();
Clock.setSecond(50);
Clock.setMinute(59);
Clock.setHour(11);
Clock.setDoW(5);
Clock.setDate(31);
Clock.setMonth(5);
Clock.setYear(13);
}
void loop()
{
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print(ax); Serial.print('-');
Serial.print(ay); Serial.print('-');
Serial.print(az); Serial.print('-');
Serial.print(gx); Serial.print('-');
Serial.print(gy); Serial.print('-');
Serial.print(gz); Serial.print('\n');
Serial.print("20"); Serial.print(Clock.getYear(), DEC);
Serial.print('-'); Serial.print(Clock.getMonth(c), DEC);
Serial.print('-'); Serial.print(Clock.getDate(), DEC);
Serial.print(' '); Serial.print(Clock.getHour(c, c), DEC);
Serial.print(':'); Serial.print(Clock.getMinute(), DEC);
Serial.print(':'); Serial.println(Clock.getSecond());
delay(1000);
}


This is my first attempt, after the wires are connected, I found that accelerator shows all zero, while the clock shows totally wrong time like year 20149 (I am not sure human exist in 20149)

I found some post about this problem. It seems that the default I2C address for the MPU6050 is 0x68 and the DS3231 happened to be exactly the same address. This is really rare because there are about 120 address available. And I just have 2 of them with same address.

Then someone suggest that the MPU6050 default address can be changed to 0x69 by setting the AD0 to high. So I connect the AD0 to the VCC and I checked with I2C scanner. It worked because now the DS3231 is 0x68 and the MPU6050 is 0x69

But just as I thought that problem can be solved, I found that with the AD0 connected to VCC, the MPU6050 shows all zero on 6 axis.

I removed the DS3231 and stick to the MPU6050, trying to make sense of it. I found that if I attach AD0 to GND(or I just disconnect AD0 because it is set to GND as default), the 6 axis number are correct, and the address is 0x68. If I attach AD0 to VCC, the address will be 0x69 but all 6 axis show zero.

Here somes my question: If I want to use both these 2 module I have to set the MPU6050 to 0x69 but it will be all zero. Is there anything I did wrong? How can I solve this problem?

Thank you very much!

All the examples of the IDE on MPU6050 work when the AD0-pin of the sensor is connected to GND. That means that the MPU6050.h file has keyed 0x68 as the device address.

Because you have the DS3231 in the project with the same address, you may try to do the following to change the address of MPU6050 to 0x69. I have tried this and it works.

File Explorer ---> C:\ ----> Users ---> GM (your computer name) ---> Documets ---> Arduino ---> libraries ---> Arduino-MPU6050-master ---> MPU6050.h and click to open

Down somewhere you will find this line :
#define MPU6050_ADDRESS (0x68) // 0x69 when AD0 pin to Vcc

Just write 0x69 in palce of 0x68, save the file and exit.

Don't forget to connect AD0-pin of MPU6050 with 5V.

This is the test program with device address 0x69 to monitor room temperature and show on Serail Monitor..

/*
    MPU6050 Triple Axis Gyroscope & Accelerometer. Temperature Example.
    Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-zyroskop-i-akcelerometr-mpu6050.html
    GIT: https://github.com/jarzebski/Arduino-MPU6050
    Web: http://www.jarzebski.pl
    (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup() 
{
  Serial.begin(9600); 
  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(5000);
  }
}

void loop()
{
  float temp = mpu.readTemperature();

  Serial.print(" Temp = ");
  Serial.print(temp);
  Serial.println(" *C");
  
  delay(2000);
}

Oh my god this works!
I just changed my head file according to your suggestion.
After that my code just work for both DS3231 and MPU6050
Thank you so much!

Cheers!

@bigdeer
Can you please post, in this thread, the zip version of I2dev.h file?

Got it.
please check the attachment.

This file locates in C:\Program Files (x86)\Arduino IDE for Microduino\libraries\I2Cdev

I2Cdev.h (11.9 KB)

Guys,
even though this might work, it is NOT a correct solution. You don't want to modify provided libraries! Never ever. MPU6050 provides by default an MPU constructor that accepts registry address. So the correct answer is to use proper constructor.

So instead of MPU6050 mpu; use MPU6050 mpu(0x69).

GolamMostafa:
All the examples of the IDE on MPU6050 work when the AD0-pin of the sensor is connected to GND. That means that the MPU6050.h file has keyed 0x68 as the device address.

Because you have the DS3231 in the project with the same address, you may try to do the following to change the address of MPU6050 to 0x69. I have tried this and it works.

File Explorer ---> C:\ ----> Users ---> GM (your computer name) ---> Documets ---> Arduino ---> libraries ---> Arduino-MPU6050-master ---> MPU6050.h and click to open

Down somewhere you will find this line :
#define MPU6050_ADDRESS (0x68) // 0x69 when AD0 pin to Vcc

Just write 0x69 in palce of 0x68, save the file and exit.

Don't forget to connect AD0-pin of MPU6050 with 5V.

This is the test program with device address 0x69 to monitor room temperature and show on Serail Monitor..

/*

MPU6050 Triple Axis Gyroscope & Accelerometer. Temperature Example.
   Read more: /dev/jarzebski: 3-osiowy żyroskop i akcelerometr MPU6050
   GIT: GitHub - jarzebski/Arduino-MPU6050: MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library
   Web: http://www.jarzebski.pl
   (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup()
{
 Serial.begin(9600);
 Serial.println("Initialize MPU6050");

while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
 {
   Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
   delay(5000);
 }
}

void loop()
{
 float temp = mpu.readTemperature();

Serial.print(" Temp = ");
 Serial.print(temp);
 Serial.println(" *C");
 
 delay(2000);
}

talapoin:
Guys,
even though this might work, it is NOT a correct solution. You don't want to modify provided libraries! Never ever. MPU6050 provides by default an MPU constructor that accepts registry address. So the correct answer is to use proper constructor.

So instead of MPU6050 mpu; use MPU6050 mpu(0x69).

Thank you with + for the great information I was longing!

Hi, I am doing the same project. May I know how jumpers connected?

Try This = [SOLVED] Cannot Connect DS3231 and MPU6050 - Networking, Protocols, and Devices - Arduino Forum