Write MPU6050 data to SD Card

Hey, everybody! Looking to modify the below code to write the values collected by the MPU6050 to a text file on the SD card.

As of right now, everything words beautifully, but the SD card is just written with that sample text, and I don't know how to change that. Any advice would be appreciated.

And if, in the course of looking through it, you figured out a way to start and stop data-writing to the card with a button-press (if such a thing is possible), that'd be cool, too. :stuck_out_tongue:

I appreciate you all in advance!

#include <SPI.h>
#include <SD.h>
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/* 
 Library and code have been taken from:
 https://github.com/tockn/MPU6050_tockn
 
Thanks to RoJax for base code. Code modified by Jordan Gaither (2021).
 
 1-MPU6050 Introduction video and code: https://youtu.be/uhh7ik02aDc
 2-LCD1602 with I2C module video and code https://youtu.be/q9YC_GVHy5A
 */

// Set the LCD address to 0x27 for a 16 chars and 2 line display
MPU6050 mpu6050(Wire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
File myFile;
int pinCS = 10;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(false);
  pinMode(pinCS, OUTPUT);
  
  // 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);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("Writing to file...");
    // Write to file
    myFile.println("Testing text 1, 2 ,3...");
    myFile.close(); // close the file
    Serial.println("Done.");
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
  // Reading the file
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("Read:");
    // Reading the whole file
    while (myFile.available()) {
      Serial.write(myFile.read());
   }
    myFile.close();
  }
  else {
    Serial.println("error opening test.txt");
  }
  
  // initialize the LCD,
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.clear();
  lcd.setCursor (0, 0); //
  lcd.setCursor (0, 0); //

  lcd.setCursor (0, 0); //
  lcd.print("HELLO, SIR.");
  delay(1000);
  
  lcd.setCursor (0, 0); //
  lcd.print("STAND BY - 3");
  lcd.setCursor (0, 1); //
  lcd.write(255);
  lcd.setCursor (1, 1); //
  lcd.write(255);
  lcd.setCursor (2, 1); //
  lcd.write(255);
  lcd.setCursor (3, 1); //
  lcd.write(255);
  lcd.setCursor (4, 1); //
  lcd.write(255);     
  delay(500);
  
  lcd.setCursor (0, 0); //
  lcd.print("STAND BY - 2");
  lcd.setCursor (0, 1); //
  lcd.write(255);
  lcd.setCursor (1, 1); //
  lcd.write(255);
  lcd.setCursor (2, 1); //
  lcd.write(255);
  lcd.setCursor (3, 1); //
  lcd.write(255);
  lcd.setCursor (4, 1); //
  lcd.write(255);
  lcd.setCursor (5, 1); //
  lcd.write(255);
  lcd.setCursor (6, 1); //
  lcd.write(255);
  lcd.setCursor (7, 1); //
  lcd.write(255);
  lcd.setCursor (8, 1); //
  lcd.write(255);
  lcd.setCursor (9, 1); //
  lcd.write(255);    
  delay(500);
  
  lcd.setCursor (0, 0); //
  lcd.print("STAND BY - 1");
  lcd.setCursor (0, 1); //
  lcd.write(255);
  lcd.setCursor (1, 1); //
  lcd.write(255);
  lcd.setCursor (2, 1); //
  lcd.write(255);
  lcd.setCursor (3, 1); //
  lcd.write(255);
  lcd.setCursor (4, 1); //
  lcd.write(255);
  lcd.setCursor (5, 1); //
  lcd.write(255);
  lcd.setCursor (6, 1); //
  lcd.write(255);
  lcd.setCursor (7, 1); //
  lcd.write(255);
  lcd.setCursor (8, 1); //
  lcd.write(255);
  lcd.setCursor (9, 1); //
  lcd.write(255);
  lcd.setCursor (10, 1); //
  lcd.write(255);  
  lcd.setCursor (11, 1); //
  lcd.write(255);  
  lcd.setCursor (12, 1); //
  lcd.write(255);  
  lcd.setCursor (13, 1); //
  lcd.write(255);  
  lcd.setCursor (14, 1); //
  lcd.write(255);      
  delay(500);
}

void loop() {

  mpu6050.update();
  Serial.print("angleX : ");
  Serial.print(mpu6050.getAngleX());
  Serial.print("  angleY : ");
  Serial.print(mpu6050.getAngleY());
  Serial.print("  angleZ : ");
  Serial.println(mpu6050.getAngleZ());

  lcd.clear();// clean previous values from screen
  lcdDisplay(
  mpu6050.getAngleX(), // send angle X
  mpu6050.getAngleY(), // send angle Y
  mpu6050.getAngleZ()  // send angle Z
    );
  delay(100);

}// loop end

/*
   Written by Ahmad Shamshiri for Robojax.com
 lcdDisplay(float x, float y, float z)
 displays value and title on LCD2004-I2C display
 How to use:
 just pass the three values and it will display it
 lcdDisplay(mpu6050.getAngleX() , mpu6050.getAngleY() , mpu6050.getAngleZ() )
 */
void lcdDisplay(float x, float y, float z)
{
  // MPU6050 Demo with LCD2004-I2C Display
  lcd.setCursor (0, 0); //
  lcd.print("X:");
  lcd.setCursor (2, 0); //
  lcd.print(x);

  lcd.setCursor (9, 0); //
  lcd.print("Y:");
  lcd.setCursor (11, 0); //
  lcd.print(y);

  lcd.setCursor (0, 1); //
  lcd.print("Z:");
  lcd.setCursor (2, 1); //
  lcd.print(z);

}


myFile.print() will send any data you want to the SD card, just like Serial.print() sends any data you want to the serial monitor.

Could it be that you jumped into the deep end of the pool a bit early, and need to go over some of the basic Arduino examples first?

A) Thanks for the "myFile.print()" tip; I'll check that out.

B) No thanks whatsoever for your unnecessary and condescending second sentence.

For what it's worth, I'm a hands-on, "deep-end" learner, who assimilates new knowledge best from the bottom-up.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.