Go Down

Topic: Storing data from MPU5060 sensor into SD card (Read 204 times) previous topic - next topic

Mojtaba256

Hello

I have a code to store acceleration data from ADXL345 sensor to the SD card, but I want to do the same thing for MPU5060 sensor. I send you the ADXL345 code which works well and can store a specified number of data into an SD card and the MPU5060 code which works with serial monitor and I want to modify it based on my ADXL345 code given to you below. I kindly appreciate it if you can help me.

ADXL345 Code for storing data into an SD card:

/*******************************************************
   Name: ADXL345
   Description: Print the X, Y, and Z values on Serial Monitor
   Website: www.sunfoundder.coom
   Email: serice@sunfounder.com
   Date: 14/12/2016
   Author: Daisy
 ********************************************************/

#include <Wire.h>  //Call the I2C library built in Arduino
//Set the address of the register
#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library

#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
//
#define redLEDpin 3
#define greenLEDpin 4
//
int chipSelect = 4; //chipSelect pin for the SD card Reader
File myFile; //Data object you will write your sesnor data to
//
int ADXAddress = 0x53;  //I2C address
int reading = 0;
int val = 0;
int X0, X1, X_out;
int Y0, Y1, Y_out;
int Z1, Z0, Z_out;
double Xg, Yg, Zg;
float samples = 100000;//Number of samples

void setup()
{
  Serial.begin(9600);//Set the baud rate of serial monitor as 9600bps
  delay(100);
  Wire.begin();  //Initialize I2C
  delay(100);
  Wire.beginTransmission(ADXAddress);
  Wire.write(Register_2D);
  Wire.write(8);
  Wire.endTransmission();
  Serial.println("\n");
  Serial.println("Accelerometer Test ");

  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  // Create/Open file
  myFile = SD.open("test.txt", FILE_WRITE);
  while (samples != 0) {
    Wire.beginTransmission(ADXAddress);
    Wire.write(Register_X0);
    Wire.write(Register_X1);
    Wire.endTransmission();
    Wire.requestFrom(ADXAddress, 2);
    if (Wire.available() <= 2);
    {
      X0 = Wire.read();
      X1 = Wire.read();
      X1 = X1 << 8;
      X_out = X0 + X1;
    }

    Wire.beginTransmission(ADXAddress);
    Wire.write(Register_Y0);
    Wire.write(Register_Y1);
    Wire.endTransmission();
    Wire.requestFrom(ADXAddress, 2);
    if (Wire.available() <= 2);
    {
      Y0 = Wire.read();
      Y1 = Wire.read();
      Y1 = Y1 << 8;
      Y_out = Y0 + Y1;
    }

    Wire.beginTransmission(ADXAddress);
    Wire.write(Register_Z0);
    Wire.write(Register_Z1);
    Wire.endTransmission();
    Wire.requestFrom(ADXAddress, 2);
    if (Wire.available() <= 2);
    {
      Z0 = Wire.read();
      Z1 = Wire.read();
      Z1 = Z1 << 8;
      Z_out = Z0 + Z1;
    }
    Xg = X_out;// / 256.00; //Convert the output result into the acceleration g, accurate to 2 decimal points.
    Yg = Y_out;// / 256.00;
    Zg = Z_out;// / 256.00;

    if (myFile) {
      //Serial.println("Writing to file...");
      myFile.print(Xg);
      myFile.print("\t");
      myFile.print(Yg);
      myFile.print("\t");
      myFile.println(Zg);
    }
    else {
      Serial.println("error opening test.txt");
    }
    samples = samples - 1;
  }
  myFile.close();
  Serial.println("Done!!");
  pinMode(LED_BUILTIN, OUTPUT);
  //delay(100); //Optional
}

// the loop function runs over and over again forever
void loop() {
}


MPU5060 sensor code which works with IDE serial monitor:


#include<Wire.h>
const int MPU6050_addr=0x68;
int16_t AccX,AccY,AccZ,Temp,GyroX,GyroY,GyroZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU6050_addr,14,true);
  AccX=Wire.read()<<8|Wire.read();
  AccY=Wire.read()<<8|Wire.read();
  AccZ=Wire.read()<<8|Wire.read();
  Temp=Wire.read()<<8|Wire.read();
  GyroX=Wire.read()<<8|Wire.read();
  GyroY=Wire.read()<<8|Wire.read();
  GyroZ=Wire.read()<<8|Wire.read();
  Serial.print("AccX = "); Serial.print(AccX);
  Serial.print(" || AccY = "); Serial.print(AccY);
  Serial.print(" || AccZ = "); Serial.print(AccZ);
  Serial.print(" || Temp = "); Serial.print(Temp/340.00+36.53);
  Serial.print(" || GyroX = "); Serial.print(GyroX);
  Serial.print(" || GyroY = "); Serial.print(GyroY);
  Serial.print(" || GyroZ = "); Serial.println(GyroZ);
  delay(500);
}

Thanks

PaulS

Quote
MPU5060 sensor code which works with IDE serial monitor:
So, where is YOUR attempt to add writing to a file on the SD card?
The art of getting good answers lies in asking good questions.

Go Up