Hi, I have seen that about everyone can’t connect the DS3231 and MPU6050 modules in an Arduino Board. I am also faced with this problems. But I have finally fixed the problem, so I am sharing with the community.
You just need the following Arduino Libraries ::
- DS3231 Library, Download it from here :: Link = DS3231 - Rinky-Dink Electronics
Note :: Download any other DS3231 Library… - MPU6050 Library, Download it from here :: Link = MPU6050
Connect The DS3231 and MPU6050 Modules to ::
DS3231 Arduino
VCC 5V
GND GND
SDA 8
SCL 9
MPU6050 Arduino
VCC 5V
GND GND
SDA SDA
SCL SCL
Code ::
#include "MPU6050.h"
#include "Wire.h"
#include <DS3231.h>
MPU6050 accelgyro;
int16_t ax, ay, az, gx, gy, gz;
DS3231 rtc(8, 9);
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.print("Time == ");
Serial.println(rtc.getTimeStr());
i = i + 1;
delay(1000);
}