TinyCircuits Forum

General Category => General Discussion => Topic started by: JKaechler42 on July 16, 2021, 10:31:59 PM

Title: Sequential filenames on the SD Card
Post by: JKaechler42 on July 16, 2021, 10:31:59 PM
I am working up a sketch to log data for model rocket flights. I have a TinyZero with the accelerometer. I have the accelerometer working, plus temperature and milliseconds.  I have an SD card module on order, and I need to work out the code to create a .CSV filename with sequential numbers.  I dont much care about the numbers, so much as I care about not overwriting the data from the previous flight the next time the unit is powered on.  And I want a separate CSV file for each flight.

Can anyone point me at a guide for how to program such a thing?  Or better yet a sample of code from someones project where the accomplish the same thing?


Code: [Select]
//Model Rocket Data Logger - Built on TinyCircuits TinyZero with Accelerometer
//Accelerometer data in values of 'G', Temperature in 'C', Pressure, and time stored on SD card as a .CSV file
#include <Wire.h>         // For I2C communication with sensor
#include "BMA250.h"       // For interfacing with the accel. sensor
#include <SPI.h>
#include <SD.h>

// Accelerometer sensor variables for the sensor and its values
BMA250 accel_sensor;
int x, y, z, runtime;
int AccelX, AccelY, AccelZ;
double temp;
String dataString = "";
char filename[16];
const int chipSelect = 10;

//////////////////////////////////////Setup Section////////////////////////////////////////////////////////////////////////////
void setup() {
digitalWrite(LED_BUILTIN, HIGH);

dataString += " Milliseconds , Accel Z , Accel X , Accel Y , Temp , Pressure";
// Set up the BMA250 acccelerometer sensor
accel_sensor.begin(BMA250_range_16g, BMA250_update_time_64ms);

////////////////////////////////////Data Wrangling//////////////////////////////////////////////////////////////////////////////
void loop() {
accel_sensor.read();//This function gets new data from the acccelerometer
float  x = accel_sensor.X;
float  y = accel_sensor.Y;
float  z = accel_sensor.Z;
temp = ((accel_sensor.rawTemp * 0.5) + 24.0);
float AccelX = (x/32);
float AccelY = (y/32);
float AccelZ = (z/32);
float runtime = millis();

//////////////////////////////////Output Section///////////////////////////////////////////////////////////////////////////////
SerialUSB.print("  Temperature(C) = "); SerialUSB.print(temp); SerialUSB.print("     Seconds: "); SerialUSB.println(runtime/100);
SerialUSB.print (" X Accel: "); SerialUSB.print(AccelX); SerialUSB.print (" Y Accel: "); SerialUSB.print(AccelY); SerialUSB.print (" Z Accel: "); SerialUSB.println(AccelZ);   
Title: Re: Sequential filenames on the SD Card
Post by: lennevia on July 19, 2021, 03:02:34 PM

This tutorial that includes some code on writing to a file on an SD Card should start you off in the right direction: https://www.circuitbasics.com/writing-data-to-files-on-an-sd-card-on-arduino/

I found it by googling/searching "arduino write csv to sd card"

Best of luck,
Title: Re: Sequential filenames on the SD Card
Post by: WillemH on July 21, 2021, 04:10:55 AM

A clear example for indexed filenames can be found in the example project “Rocket Altimeter Logger”.
See https://tinycircuits.com/blogs/learn/130916679-rocket-altimeter-logger
under “Step two”, Altimeter_Datalogger.ino.

I have extended this principle to three digits in the following code part (only the first part of the .ino file) for a GPS application.
In this case it relates to a .txt file, but the indexing for .csv is the same.
See below.


Code: [Select]
// Setup, Monitoring and Logging of ZOE GNSS:
//  - U-blox ZOE-M8B GPS through HW UART (Serial)
//  - Monitoring on SerialMonitorInterface of TinyZero
//  - Logging on microSD card
//  version 01: 29-11-2020
//  version GPS_Monitor_ZOE_a
//  Essential for ZOE GPS board application:
//  - Secure ZOE GPS: SAFEBOOT_N and RESET_N to high to avoid problems
//  version with NMEA GGA,GSA (2x), GSV (2 sets)and VTG at 1 Hz, GPS and GLONASS
//  HWUART (Serial, 9600 baud -> 57600 baud)
//  assumption: default GPS configuration at startup, so no settings in a battery buffered memory
//  assumption: no I2C communication with GPS
//  Monitoring and/or Logging
//  - GNSSxxx.txt for text logging during setup (both UBX and NMEA) and NMEA strings afterwards. NMEA strings also readible with VisualGPSView
//  - File is written in blocks of 512 bytes (after setup) from TinyZero to minimize micro SD waiting time
//  - File numbers are automatically increased
//  - File is opened in setup, but not closed, so last part of logging can be missed

#include <SD.h>

#define SerialMonitorInterface SerialUSB

const int chipSelect = 10;
int cardPresent,wG;
File GPSFile;

unsigned int i_skip1 = 0,b1_length;
int sd1_room=0;

// string to buffer output datafile
String buffer1 = "";

char Dat2File[]="GNSS999.txt";
char hold[]="999";

// The GPS connection is realized with a hardware serial port

#define Gps_serial Serial

void setup()

// Secure ZOE GPS: SAFEBOOT_N and RESET_N to high to avoid problems
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH); 

  int incomingByte = 0;// for incoming serial hex data
  char incomingChar;// for incoming serial char data
  int ubx;
  Gps_serial.begin(9600);//will be set to higher baudrate lateron

  // reserve 2kByte for buffer to cope with long waiting time of microSD card
  SerialMonitorInterface.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (SD.begin(chipSelect)) {
    SerialMonitorInterface.println("card initialized.");
    cardPresent = true;
  } else {
    SerialMonitorInterface.println("Card failed, or not present");
    cardPresent = false;

    //So you can find different trials easier//
  if (cardPresent) { 


  if (cardPresent) {
    GPSFile = SD.open((const char*)Dat2File, FILE_WRITE);

  while (!SerialMonitorInterface && millis() < 5000); //This will wait until the Serial Monitor is opened or until 5 seconds has passed

// changing a number of u-blox M8 GPS configuration settings

..... etc ......