0

Can I connect a DS3231 and an MPU6050 by an Arduino Mega 2560? I have connect it:

MPU 6050               Arduino Mega 2560
SDA           =        SDA
SCL           =        SCL
GND           =        GND
VCC           =        5V

And I have connected DS3231 to SDA and SCL beside AREF pin:

DS3231               Arduino Mega 2560
SDA           =        SDA
SCL           =        SCL
GND           =        GND
VCC           =        5V

The problem is when I connect both of them MPU6050 prints nan, after opening the DS3231 pin from the arduino, MPU6050 prints the correct angles. Can someone please help me?

Code:

#include "MPU6050.h"
#include "Wire.h"
#include <DS3231.h>
MPU6050 accelgyro;
int16_t ax, ay, az, gx, gy, gz;

DS3231 rtc(SDA, SCL);


double timeStep, time, timePrev;
double arx, ary, arz, grx, gry, grz, gsx, gsy, gsz, rx, ry, rz;

int i;
double gyroScale = 131;

void setup() {
  Wire.begin();
  accelgyro.initialize();
  Serial.begin(9600);
  rtc.begin();
}

void loop() {
  timePrev = time;
  time = millis();
  timeStep = (time - timePrev) / 1000; 
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  gsx = gx/gyroScale;   gsy = gy/gyroScale;   gsz = gz/gyroScale;
  arx = (180/3.141592) * atan(ax / sqrt(square(ay) + square(az))); 
  ary = (180/3.141592) * atan(ay / sqrt(square(ax) + square(az)));
  arz = (180/3.141592) * atan(sqrt(square(ay) + square(ax)) / az);
  if (i == 1) {
    grx = arx;
    gry = ary;
    grz = arz;
  }
  else{
    grx = grx + (timeStep * gsx);
    gry = gry + (timeStep * gsy);
    grz = grz + (timeStep * gsz);
  }  
  rx = (0.1 * arx) + (0.9 * grx);
  ry = (0.1 * ary) + (0.9 * gry);
  rz = (0.1 * arz) + (0.9 * grz);
  Serial.print("X axis == ");
  Serial.println(arx);
  Serial.print("Y axis == ");
  Serial.println(ary);
  Serial.print("Z axis == ");
  Serial.println(arz);
  Serial.println(rtc.getTimeStr());
  i = i + 1;
  delay(1000);
}
4
  • run I2C scanner sketch
    – Juraj
    Mar 3 '19 at 10:42
  • What DS3231.h library are you using, and why are you specifying the pins to use? That probably puts it into software I2C mode and breaks hardware I2C.
    – Majenko
    Mar 3 '19 at 10:44
  • I am using this DS3231, Link = rinkydinkelectronics.com/library.php?id=73... Mar 3 '19 at 10:52
  • And how to run I2C scanner, what is it? Mar 3 '19 at 10:52
1

The DS3231 library you are using works (for some bizarre reason) exclusively with its own internal bit-bang implementation of I2C. This cannot be used on the same pins as the hardware Wire library which the MPU6050 uses.

Either move the DS3231 to a different pair of pins, or find a better DS3231 library that uses the Wire library for communication.

0

Yes. Using i2c, you need to change the address of the MPU6050 because the default is (0x68) which is also the default and only address the DS3231 can use.

Example of changing the address from a personal sketch:

MPU6050 mpu(0x69);

So try:

MPU6050 accelgyro(0x69);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.