Hi!
I am using SD Card Shield with Arduino UNO for logging the data from MPU6050 - Triple Axis Gyro Accelerometer Module. I used SDA and SCL (A5, A4) for the MPU6050. My complete code is given below. I am not able to read the MPU chip when using with SD card inside the shield. However, when I remove the SD card it starts to read the data from MPU6050. Please help me to resolve this issue.
#include <SPI.h>
#include <SD.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
File myFile; //for SD Card Reading
// change this to match your SD shield or module;
const int chipSelect = 10;
Adafruit_MPU6050 mpu;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("MPU6050.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("Ax, Ay, Az, Gx, Gy, Gz");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// set accelerometer range to +-8G
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
// set gyro range to +- 500 deg/s
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
// set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(100);
}
void loop()
{
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
/* Print out the values */
Serial.print(a.acceleration.x);
Serial.print(",");
Serial.print(a.acceleration.y);
Serial.print(",");
Serial.print(a.acceleration.z);
Serial.print(", ");
Serial.print(g.gyro.x);
Serial.print(",");
Serial.print(g.gyro.y);
Serial.print(",");
Serial.print(g.gyro.z);
Serial.println("");
delay(10);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("MPU6050.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(a.acceleration.x);
myFile.println(",");
myFile.println(a.acceleration.y);
myFile.println(",");
myFile.println(a.acceleration.z);
myFile.println(",");
myFile.println(g.gyro.x);
myFile.println(",");
myFile.println(g.gyro.y);
myFile.println(",");
myFile.println(g.gyro.z);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}