0

I am working on a school project, where I have to log GPS and acceleration data to an sd card at the same time, but I cannot get it to work. Logging GPS and acceleration data separately work fine. The mpu6050 used to measure acceleration uses i2c communication, and the neo-GPS 6m uses softwareSerial.

The code doesn't even write the csv titles in the setup()

  #include <Wire.h>
  #include <SPI.h>
  #include <SD.h>
  #include "SoftwareSerial.h"
  #include <MPU6050_tockn.h>
  #include "TinyGPS++.h"

  int RXPin = 3;
  int TXPin = 2;
  String fileName = "data";
  int count = 0;
  bool file = false;


  MPU6050 mpu6050(Wire);
  File dataFile;
  TinyGPSPlus gps;
  SoftwareSerial serial_connection(RXPin, TXPin);

  void setup(){
  //Serial begin
    serial_connection.begin(9600);
    Serial.begin(9600);


  //SD card begin
    if (!SD.begin(4)) {
      Serial.println("initialization failed!");
      while (1);
    }

  //Checks if file already exists
    while(!file){
      if(SD.exists(fileName + ".csv")){
        count++;
        fileName = "data" + String(count);
      }else{
        file = true;
      }
    }


    dataFile = SD.open((fileName + ".csv"), FILE_WRITE);
    if(dataFile){

      dataFile.print("x");
      dataFile.print(",");
      dataFile.print("y");
      dataFile.print(",");
      dataFile.print("z");
      dataFile.print(",");
      dataFile.print("GPS_time");
      dataFile.print(",");
      dataFile.print("Latitude");
      dataFile.print(",");
      dataFile.print("Longitude");
      dataFile.print(",");
      dataFile.print("m/s");
      dataFile.print(",");
      dataFile.print("km/h");
      dataFile.print(",");
      dataFile.print("altitude (m)");
      dataFile.print(",");
      dataFile.println("satellites");
      dataFile.close();

    }else{
      Serial.print("error opening sd card");
    }

  //MPU6050

    Wire.begin();
    mpu6050.begin();

    Serial.println("initialization done.");

    delay(100);
    deltaTime = millis();
  }




  void loop(){

    while(serial_connection.available()){
      gps.encode(serial_connection.read());   
     } 

    mpu6050.update();


    dataFile = SD.open((fileName + ".csv"), FILE_WRITE);
    if(dataFile){

      dataFile.print(mpu6050.getAccX());
      dataFile.print(",");
      dataFile.print(mpu6050.getAccY());
      dataFile.print(",");
      dataFile.println(mpu6050.getAccZ());
      dataFile.print(",");  
      dataFile.print(gps.time.value());
      dataFile.print(",");
      dataFile.print(gps.location.lat(), 6);
      dataFile.print(",");
      dataFile.print(gps.location.lng(), 6);
      dataFile.print(",");
      dataFile.print(gps.speed.mps());
      dataFile.print(",");
      dataFile.print(gps.speed.kmph());
      dataFile.print(",");
      dataFile.print(gps.altitude.meters());
      dataFile.print(",");
      dataFile.println(gps.satellites.value());
      dataFile.close();
      }else{
      Serial.print("Does not log data!");
    }



    Serial.print(" X: ");Serial.print(mpu6050.getAccX());
    Serial.print(" Y: ");Serial.print(mpu6050.getAccY());
    Serial.print(" Z: ");Serial.println(mpu6050.getAccZ());
    Serial.print(","); 
    Serial.print(gps.time.value());
    Serial.print(",");
    Serial.print(gps.location.lat(), 6);
    Serial.print(",");
    Serial.print(gps.location.lng(), 6);
    Serial.print(",");
    Serial.print(gps.speed.mps());
    Serial.print(",");
    Serial.print(gps.speed.kmph());
    Serial.print(",");
    Serial.print(gps.altitude.meters());
    Serial.print(",");
    Serial.println(gps.satellites.value()); 

    }
  • 2
    And what exactly does "does not work" mean? What is actally happening? What did you expect to happen? – chrisl Dec 3 '19 at 20:04
  • Are you getting any error messages over Serial? Can you confirm, that the calls for writing the titles into the csv file are actually executed (for example through an additional Serial.println() call there)? – chrisl Dec 3 '19 at 21:47

Your Answer

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

Browse other questions tagged or ask your own question.