Setting up SD data logger for an accelerometer

Hello I am currently making a weather balloon controller that mainly records acceleration once it hits the Jet stream. I have the accelerometer working for the most part but am having trouble actually having data log to the SD card. To preface this, this is a school project and I have VERY little experience in programming in general so I was kinda just fusing 2 scripts together hoping it would work. It did to some extent but now I'd like to seek outside help. Any help is greatly appreciated!

#include <Adafruit_LSM303_Accel.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);

const int chipSelect = 10;  

void displaySensorDetails(void) {
  sensor_t sensor;
  accel.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print("Sensor:       ");
  Serial.println(sensor.name);
  Serial.print("Driver Ver:   ");
  Serial.println(sensor.version);
  Serial.print("Unique ID:    ");
  Serial.println(sensor.sensor_id);
  Serial.print("Max Value:    ");
  Serial.print(sensor.max_value);
  Serial.println(" m/s^2");
  Serial.print("Min Value:    ");
  Serial.print(sensor.min_value);
  Serial.println(" m/s^2");
  Serial.print("Resolution:   ");
  Serial.print(sensor.resolution);
  Serial.println(" m/s^2");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void setup(void) {
#ifndef ESP8266
  while (!Serial)
    ; // will pause Zero, Leonardo, etc until serial console opens
#endif
  Serial.begin(9600);
  Serial.println("Accelerometer Test");
  Serial.println("");

  /* Initialise the sensor */
  if (!accel.begin()) {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while (1)
      ;
  }

 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }

  /* Display some basic information on this sensor */
  displaySensorDetails();

  accel.setRange(LSM303_RANGE_4G);
  Serial.print("Range set to: ");
  lsm303_accel_range_t new_range = accel.getRange();
  switch (new_range) {
  case LSM303_RANGE_2G:
    Serial.println("+- 2G");
    break;
  case LSM303_RANGE_4G:
    Serial.println("+- 4G");
    break;
  case LSM303_RANGE_8G:
    Serial.println("+- 8G");
    break;
  case LSM303_RANGE_16G:
    Serial.println("+- 16G");
    break;
  }

  accel.setMode(LSM303_MODE_NORMAL);
  Serial.print("Mode set to: ");
  lsm303_accel_mode_t new_mode = accel.getMode();
  switch (new_mode) {
  case LSM303_MODE_NORMAL:
    Serial.println("Normal");
    break;
  case LSM303_MODE_LOW_POWER:
    Serial.println("Low Power");
    break;
  case LSM303_MODE_HIGH_RESOLUTION:
    Serial.println("High Resolution");
    break;
  }
}

void loop(void) {
  /* Get a new sensor event */
  sensors_event_t event;
  accel.getEvent(&event);
  String dataString = "";
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
                                        // print to the serial port too:
    Serial.println(dataString);
  }
                                        // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: ");
  Serial.print(event.acceleration.x);
  Serial.print("  ");
  Serial.print("Y: ");
  Serial.print(event.acceleration.y);
  Serial.print("  ");
  Serial.print("Z: ");
  Serial.print(event.acceleration.z);
  Serial.print("  ");
  Serial.println("m/s^2");

  dataFile.print("X: ");
  dataFile.print(event.acceleration.x);
  dataFile.print("  ");
  dataFile.print("Y: ");
  dataFile.print(event.acceleration.y);
  dataFile.print("  ");
  dataFile.print("Z: ");
  dataFile.print(event.acceleration.z);
  dataFile.print("  ");
  dataFile.println("m/s^2");

  dataFile.flush();

  /* Delay before the next sample */
  delay(500);
}

Ok, so to what extend did it work?

What isn't working as you expected/wanted?

  • why is the file repeatedly opened in loop() instead of opened once in setup?

  • shouldn't the data be as compact as possible to store as much data as possible (i.e. data structure? should it be stored as ASCII strings or as binary bytes?

  • should each data be timestamped?

  • should data be collected periodically (once a second), not each iteration of loop()?

  • if this is an SD card, wouldn't it be removed from the arduino (or whatever) and the data read on a PC/laptop where it is processed?

When I run the code, the SD card initializes, the sensor logs the data to the serial port, but it doesn't output anything to the SD card file. When I open it after running, it looks like it's just outputting spaces. The file gets longer but nothing is there. Thank you!

I do not know much about programming and most of this code is made from examples.
Overall yes that would be ideal, I didn't have a whole lot of time to plan this or work on it.
Preferably, yes.
Teacher wants it to log every second.
And yes, overall once the balloon comes in we'll be reading the data with the SD card.

I also would like to add a button switch on it to turn it on and off. I figured that would be something I could do once the code for the most part is wrapped up but I guess not.

suggest you start with a simpler program, that opens a file, writes sample data (i.e. dummy data) to it periodically (i.e. once a second) and probably stops after some amount of time and displays the data from the file

then modify it to capture accelleromter and timestamps

Alright, I wasn't given much time and have til monday to finish it so we'll see how it goes!

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