My Datalogger SCD30 code wont print values on Sd Card - SOLVED

Hello everybody, I have a project where i got to build and code a Datalogger with the SCD30 sensor for Temperture, Humidity and CO2. I also created an Offsets.txt file for my sensor to read the values from the file and then print the values +- the offset valuu anyway, it seems to work on the serial monitor, but it wont print on my Sd Card!! it opens the file to print the values but it doesnt actually print! I would really appreciate any sugestion from you guys.

#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include "SparkFun_SCD30_Arduino_Library.h"



const int SDchipSelect = 10; 
File sdfile;
SCD30 airSensor;
RTC_PCF8523 DataLoggerRTC;

const byte pinRDY = 8;
byte pin_LED = 13;

unsigned long timerInterval = 0;
unsigned long timerLast = 0; 
float CO2 = 0.0;
float Temperature = 0.0;
float Humidity = 0.0;
float CO2ppm = 0.0;
float TempC = 0.0;
float HumidityPct = 0.0;
float Offsets[4] = {0.0, 0.0, 0.0, 0.0};
int x = 0;   
int notpossible = 0;
char buf1[20];
char filename[14];

///////////////////////////////////////////////////////////////

void setup() {  
  Serial.begin(9600);
  while (!Serial) {
    delay(1);
  }

  // pinMode(pin_LED, OUTPUT);
//////////////////////////////////////////////////////////////////////////////
  // Sensirion SCD30 CO2 sensor
  Wire.begin();
  if (airSensor.begin() == false) {
    Serial.println("ERROR - CO2 sensor not detected.  Check wiring");
    //while (1) { blinkERR(pin_LED);}
  }
  pinMode(pinRDY, INPUT);

  //////////////////////////////////////////////////////////////////////////////
  // Initialize the DataLogger RTC
  if (!DataLoggerRTC.begin()) {
    Serial.println("ERROR - Couldn't find DataLogger RTC");
    while (1) { } //blinkERR(pin_LED);}
  }

////////////////////////////////////////////////////////////////////////
// see if the SD card is present and can be initialized: 
  if (!SD.begin(SDchipSelect)) {
    Serial.println("SD card failed, or not present");
    while (1) { } //blinkERR(pin_LED);}
  }
   Serial.println("SD card initialized."); 
  // Create a filename reference to a file that doesn't exist 'ANALOG00.TXT'..'ANALOG99.TXT'
  //char filename[14];
  strcpy(filename, "/ANALOG00.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[7] = '0' + i/10;
    filename[8] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (SD.exists(filename)){
      Serial.print("File '");
      Serial.print(filename);
      Serial.println("' already exists");
    } else {
      Serial.print("New file will be '");
      Serial.print(filename);
      Serial.println("'");
      break;
    }
  }

  // Open file on SD Card for writing
  sdfile = SD.open(filename, FILE_WRITE);
  if (! sdfile) {
    Serial.print("ERROR - unable to create '");
    Serial.print(filename); Serial.println("'");
    while (1) { } //blinkERR(pin_LED);}
  }
  //////////////////////////////////////////////////////////////////////////////
  //readMicroSDcard();
  Serial.println("Setup complete\n");
  sdfile.println("Setup complete\n");
  Serial.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.println("Date/Time; CO2ppm; TempC; Humidity%");
  readMicroSDcard();
  sdfile.flush();
} // setup()


void readMicroSDcard() {              //read values from SD card, and parse into myArray array
  const byte numChars = 5;            //max number of characters that comprise each of the comma separated values
  const int chipSelect = 10;          //connected to pin 10 Arduino Uno/Metro to detect card presence
  char receivedChars[numChars];       //array to receive characters for each comma separated value
  char tempChars[numChars];           //array used to store temporary characters during parsing function


  sdfile = SD.open("Offsets.txt");
  if (sdfile) {
    static byte index = 0;
    char incomingChar;
    while (sdfile.available() > 0) {
      incomingChar = sdfile.read();
      if (incomingChar != ',') {                        //keep receiving characters until next comma
        receivedChars[index] = incomingChar;
        index++;
        //sdfile.close();
      }
      else {
        receivedChars[index] = '\0';                    // terminate the string
        index = 0;                                      //reset receivedChars array index
        strcpy(tempChars, receivedChars);                // this temporary copy is necessary to protect the original data
                                                          // because strtok() used in parseData() replaces the commas with \0
        char * strtokIndex;                               // this is used by strtok() as an index
        strtokIndex = strtok(tempChars, ",");              // this continues where the previous call left off at the comma
        Offsets[x] = atof(strtokIndex);                    //convert characters to a float value, add it to current index myArray
        x++;                                                //move to next element in myArray 
      }
    }
  }
}

void loop() {

    sdfile = SD.open(filename);
    timerInterval = 1000 * Offsets[0];
   
    if (Offsets[0] < 5) {
      Serial.println("Time to short, please choose longer than 5sec");
      while (1) {blinkERR(pin_LED);}
      //while (1) {  }
    }
   

    if (timerLast > millis())  timerLast = millis();
    if ((millis() - timerLast) > timerInterval) {

      digitalWrite(pin_LED, HIGH);
      DateTime now = DataLoggerRTC.now();

      CO2 = airSensor.getCO2();
      Temperature = airSensor.getTemperature();
      Humidity = airSensor.getHumidity();
      sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",
          now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());

    
      CO2ppm = CO2 - Offsets[1];
      TempC = Temperature - Offsets[2];
      HumidityPct = Humidity - Offsets[3];

      

      Serial.print(buf1);
      Serial.print(";");
      Serial.print(CO2ppm);
      Serial.print(";");
      Serial.print(TempC);
      Serial.print(";");
      Serial.println(HumidityPct);
      
      // Write to the SD card..
      sdfile.print(buf1);  // 20200216T150644Z
      sdfile.print(";");
      sdfile.print(CO2ppm);
      sdfile.print(";");
      sdfile.print(TempC);
      sdfile.print(";");
      sdfile.println(HumidityPct);
    // Execute a flush() to insure it is written since no sdfile.close() will be issued.
      sdfile.flush();
      digitalWrite(pin_LED, LOW);
      timerLast = millis();
  }
  
  yield();
  
} // loop()
  

void blinkLED(byte ledPIN){
  //  consumes 300 ms.
  for(int i = 5; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(30);
    digitalWrite(ledPIN, LOW);
    delay(30);
  }    
} // blinkLED()

void blinkERR(byte ledPIN){
  // S-O-S
  const int S = 150, O = 300;
  for(int i = 3; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(S);
    digitalWrite(ledPIN, LOW);
    delay(S);
  }    
  delay(200);
  for(int i = 3; i>0; i--){
    digitalWrite(ledPIN, HIGH);
    delay(O);
    digitalWrite(ledPIN, LOW);
    delay(O);
  }
}  

How much troubleshooting have you done? For example, have you tried just writing dummy values to the file (whether or not sensor readings are available, or anything else that might block it)?

Also you said it opens a file. How do you know that?

well I'm new into programming but i tried around and unfortunately cant find the problem. The Sd Card its ok, if i leave the void readMicroSDcard function out than it works fine!
I know that it opens the file, because I take the SD Card out and see that a new file was created but nothing was printed on it, meanwhile on the serial monitor i get the values of the sensor printedout without a problem.

Did you close the file?

void readMicroSDcard() {              //read values from SD card, and parse into myArray array
  const byte numChars = 5;            //max number of characters that comprise each of the comma separated values
  const int chipSelect = 10;          //connected to pin 10 Arduino Uno/Metro to detect card presence
  char receivedChars[numChars];       //array to receive characters for each comma separated value
  char tempChars[numChars];           //array used to store temporary characters during parsing function


  sdfile = SD.open("Offsets.txt");
  if (sdfile) {
    static byte index = 0;
    char incomingChar;
    while (sdfile.available() > 0) {
      incomingChar = sdfile.read();
      if (incomingChar != ',') {                        //keep receiving characters until next comma
        receivedChars[index] = incomingChar;
        index++;
        sdfile.close();
      }
      else {
        receivedChars[index] = '\0';                    // terminate the string
        index = 0;                                      //reset receivedChars array index
        strcpy(tempChars, receivedChars);                // this temporary copy is necessary to protect the original data
                                                          // because strtok() used in parseData() replaces the commas with \0
        char * strtokIndex;                               // this is used by strtok() as an index
        strtokIndex = strtok(tempChars, ",");              // this continues where the previous call left off at the comma
        Offsets[x] = atof(strtokIndex);                    //convert characters to a float value, add it to current index myArray
        x++;                                                //move to next element in myArray 
        sdfile.close();
      }
    }
  }
}

Yes i tried, it still doesnt work, it opens the file but it doesnt print nthg in it, not even the part from the setup!
if i add the " sdfile.close(); " just like i did here, than it doesnt read the values from the Offsets file!

this part seems to be the problem, since now its printing "this is a problem"

void readMicroSDcard() {              //read values from SD card, and parse into Offsets array
  const byte numChars = 5;            //max number of characters that comprise each of the comma separated values
  const int chipSelect = 10;          //connected to pin 10 Arduino Uno to detect card presence
  char receivedChars[numChars];       //array to receive characters for each comma separated value
  char tempChars[numChars];           //array used to store temporary characters during parsing function


  sdfile = SD.open("Offsets.txt");
  if (sdfile) {
    static byte index = 0;
    char incomingChar;
    while (sdfile.available() > 0) {
      incomingChar = sdfile.read();
      if (incomingChar != ',') {                    //keep receiving characters until next comma
        receivedChars[index] = incomingChar;
        index++;
        //sdfile.close();
      }
      else {
        Serial.print("this is a problem");
        while(1) { }
        //receivedChars[index] = '\0';                // terminate the string
        //index = 0;                                  //reset receivedChars array index
        //strcpy(tempChars, receivedChars);           // this temporary copy is necessary to protect the original data
                                                    // because strtok() used in parseData() replaces the commas with \0
        //char * strtokIndex;                         // this is used by strtok() as an index
        //strtokIndex = strtok(tempChars, ",");       // this continues where the previous call left off at the comma
        //Offsets[x] = atof(strtokIndex);             //convert characters to a float value, add it to current index myArray
        //x++;                                        //move to next element in myArray 
        
      }
    }
  }
      sdfile.close();
}

HI!

I think the error is on setup because you are not closing the file before call the function readMicroSDcard.

Best regards.

Hi Fernando,

I tried that aswell but unfortunately it doesnt work!

void setup() {  
  Serial.begin(9600);
  while (!Serial) {
    delay(1);
  }

  // pinMode(pin_LED, OUTPUT);
//////////////////////////////////////////////////////////////////////////////
  // Sensirion SCD30 CO2 sensor
  Wire.begin();
  if (airSensor.begin() == false) {
    Serial.println("ERROR - CO2 sensor not detected.  Check wiring");
    while(1) { }
  }
  pinMode(pinRDY, INPUT);

  //////////////////////////////////////////////////////////////////////////////
  // Initialize the DataLogger RTC
  if (!DataLoggerRTC.begin()) {
    Serial.println("ERROR - Couldn't find DataLogger RTC");
    while(1) { } 
  }

////////////////////////////////////////////////////////////////////////
// see if the SD card is present and can be initialized: 
  if (!SD.begin(SDchipSelect)) {
    Serial.println("SD card failed, or not present");
    while(1) { } 
  }
   Serial.println("SD card initialized."); 
  // Create a filename reference to a file that doesn't exist 'ANALOG00.TXT'..'ANALOG99.TXT'
  //char filename[14];
  strcpy(filename, "/ANALOG00.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[7] = '0' + i/10;
    filename[8] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (SD.exists(filename)){
      Serial.print("File '");
      Serial.print(filename);
      Serial.println("' already exists");
    } else {
      Serial.print("New file will be '");
      Serial.print(filename);
      Serial.println("'");
      break;
    }
  }

  // Open file on SD Card for writing
  sdfile = SD.open(filename, FILE_WRITE);
  if (! sdfile) {
    Serial.print("ERROR - unable to create '");
    Serial.print(filename); Serial.println("'");
    while(1) { } 
  }
  //////////////////////////////////////////////////////////////////////////////
  
  sdfile = SD.open(filename);
  Serial.println("Setup complete\n");
  sdfile.println("Setup complete\n");
  Serial.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.close();
 
} // setup()

It worked to me.

#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include "SparkFun_SCD30_Arduino_Library.h"

const int SDchipSelect = 10;
File sdfile;
SCD30 airSensor;
RTC_PCF8523 DataLoggerRTC;

const byte pinRDY = 8;
byte pin_LED = 13;

unsigned long timerInterval = 0;
unsigned long timerLast = 0;
float CO2 = 0.0;
float Temperature = 0.0;
float Humidity = 0.0;
float CO2ppm = 0.0;
float TempC = 0.0;
float HumidityPct = 0.0;
float Offsets[4] = {0.0, 0.0, 0.0, 0.0};
int x = 0;
int notpossible = 0;
char buf1[20];
char filename[14];


void readMicroSDcard()                // read values from SD card, and parse into myArray array
{
  const byte numChars = 5;            // max number of characters that comprise each of the comma separated values
  char receivedChars[numChars];       // array to receive characters for each comma separated value
  char tempChars[numChars];           // array used to store temporary characters during parsing function

  sdfile = SD.open("Offsets.txt");
  if (sdfile)
  {
    static byte index = 0;
    char incomingChar;
    while (sdfile.available() > 0)
    {
      incomingChar = sdfile.read();
      if (incomingChar != ',')                          // keep receiving characters until next comma
      {
        receivedChars[index] = incomingChar;
        index++;
        // sdfile.close();
      }
      else
      {
        receivedChars[index] = '\0';                    // terminate the string
        index = 0;                                      // reset receivedChars array index
        strcpy(tempChars, receivedChars);               // this temporary copy is necessary to protect the original data
                                                        // because strtok() used in parseData() replaces the commas with \0
        char * strtokIndex;                             // this is used by strtok() as an index
        strtokIndex = strtok(tempChars, ",");           // this continues where the previous call left off at the comma
        Offsets[x] = atof(strtokIndex);                 // convert characters to a float value, add it to current index myArray
        x++;                                            // move to next element in myArray
      }
    }
  }
}


void blinkLED(byte ledPIN)
{
  //  consumes 300 ms.
  for(int i = 5; i > 0; i--)
  {
    digitalWrite(ledPIN, HIGH);
    delay(30);
    digitalWrite(ledPIN, LOW);
    delay(30);
  }
} // blinkLED()

void blinkERR(byte ledPIN)
{
  // S-O-S
  const int S = 150, O = 300;
  for(int i = 3; i > 0; i--)
  {
    digitalWrite(ledPIN, HIGH);
    delay(S);
    digitalWrite(ledPIN, LOW);
    delay(S);
  }
  delay(200);
  for(int i = 3; i > 0; i--)
  {
    digitalWrite(ledPIN, HIGH);
    delay(O);
    digitalWrite(ledPIN, LOW);
    delay(O);
  }
}

void setup()
{
  Serial.begin(9600);
  while (!Serial)
  {
    delay(1);
  }

  // pinMode(pin_LED, OUTPUT);
//////////////////////////////////////////////////////////////////////////////
  // Sensirion SCD30 CO2 sensor
  Wire.begin();
  if (airSensor.begin() == false)
  {
    Serial.println("ERROR - CO2 sensor not detected.  Check wiring");
    // while (1) { blinkERR(pin_LED);}
  }
  pinMode(pinRDY, INPUT);

  //////////////////////////////////////////////////////////////////////////////
  // Initialize the DataLogger RTC
  if (!DataLoggerRTC.begin())
  {
    Serial.println("ERROR - Couldn't find DataLogger RTC");
    while (1)
    {
    }             // blinkERR(pin_LED);}
  }

////////////////////////////////////////////////////////////////////////
// see if the SD card is present and can be initialized:
  if (!SD.begin(SDchipSelect))
  {
    Serial.println("SD card failed, or not present");
    while (1)
    {
    }             // blinkERR(pin_LED);}
  }
  Serial.println("SD card initialized.");
  // Create a filename reference to a file that doesn't exist 'ANALOG00.TXT'..'ANALOG99.TXT'
  // char filename[14];
  strcpy(filename, "/ANALOG00.TXT");
  for (uint8_t i = 0; i < 100; i++)
  {
    filename[7] = '0' + i / 10;
    filename[8] = '0' + i % 10;
    // create if does not exist, do not open existing, write, sync after write
    if (SD.exists(filename))
    {
      Serial.print("File '");
      Serial.print(filename);
      Serial.println("' already exists");
    }
    else
    {
      Serial.print("New file will be '");
      Serial.print(filename);
      Serial.println("'");
      break;
    }
  }

  // Open file on SD Card for writing
  sdfile = SD.open(filename, FILE_WRITE);
  if (!sdfile)
  {
    Serial.print("ERROR - unable to create '");
    Serial.print(filename); Serial.println("'");
    while (1)
    {
    }             // blinkERR(pin_LED);}
  }
  //////////////////////////////////////////////////////////////////////////////
  // readMicroSDcard();
  Serial.println("Setup complete\n");
  sdfile.println("Setup complete\n");
  Serial.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.close();
  readMicroSDcard();
} // setup()

void loop()
{

  sdfile = SD.open(filename);
  timerInterval = 1000 * Offsets[0];

  if (Offsets[0] < 5)
  {
    Serial.println("Time to short, please choose longer than 5sec");
    while (1)
    {
      blinkERR(pin_LED);
    }
    // while (1) {  }
  }


  if (timerLast > millis())
  {
    timerLast = millis();
  }
  if ((millis() - timerLast) > timerInterval)
  {

    digitalWrite(pin_LED, HIGH);
    DateTime now = DataLoggerRTC.now();

    CO2 = airSensor.getCO2();
    Temperature = airSensor.getTemperature();
    Humidity = airSensor.getHumidity();
    sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",
            now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());


    CO2ppm = CO2 - Offsets[1];
    TempC = Temperature - Offsets[2];
    HumidityPct = Humidity - Offsets[3];



    Serial.print(buf1);
    Serial.print(";");
    Serial.print(CO2ppm);
    Serial.print(";");
    Serial.print(TempC);
    Serial.print(";");
    Serial.println(HumidityPct);

    // Write to the SD card..
    sdfile.print(buf1);    // 20200216T150644Z
    sdfile.print(";");
    sdfile.print(CO2ppm);
    sdfile.print(";");
    sdfile.print(TempC);
    sdfile.print(";");
    sdfile.println(HumidityPct);
    // Execute a flush() to insure it is written since no sdfile.close() will be issued.
    sdfile.flush();
    digitalWrite(pin_LED, LOW);
    timerLast = millis();
  }

  yield();

} // loop()

WhatsApp Image 2022-06-25 at 12.02.51

Yes, after i wrote that it didnt work, I tried differently and it works for me aswell, thank you. Anyway It only prints the message from the setup and still doesnt print the loop and the values form the sensor!!

I have fixed only the setup.

You have to add sdfile.close() after finishing the writing process in the rest of your code.

I did, still doesnt do much!!

void setup() {  
  Serial.begin(9600);
  while (!Serial) {
    delay(1);
  }

  // pinMode(pin_LED, OUTPUT);
//////////////////////////////////////////////////////////////////////////////
  // Sensirion SCD30 CO2 sensor
  Wire.begin();
  if (airSensor.begin() == false) {
    Serial.println("ERROR - CO2 sensor not detected.  Check wiring");
    while(1) { }
  }
  pinMode(pinRDY, INPUT);

  //////////////////////////////////////////////////////////////////////////////
  // Initialize the DataLogger RTC
  if (!DataLoggerRTC.begin()) {
    Serial.println("ERROR - Couldn't find DataLogger RTC");
    while(1) { } 
  }

////////////////////////////////////////////////////////////////////////
// see if the SD card is present and can be initialized: 
  if (!SD.begin(SDchipSelect)) {
    Serial.println("SD card failed, or not present");
    while(1) { } 
  }
   Serial.println("SD card initialized."); 
  // Create a filename reference to a file that doesn't exist 'ANALOG00.TXT'..'ANALOG99.TXT'
  //char filename[14];
  strcpy(filename, "/ANALOG00.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[7] = '0' + i/10;
    filename[8] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (SD.exists(filename)){
      Serial.print("File '");
      Serial.print(filename);
      Serial.println("' already exists");
    } else {
      Serial.print("New file will be '");
      Serial.print(filename);
      Serial.println("'");
      break;
    }
  }

  // Open file on SD Card for writing
  sdfile = SD.open(filename, FILE_WRITE);
  if (! sdfile) {
    Serial.print("ERROR - unable to create '");
    Serial.print(filename); Serial.println("'");
    while(1) { } 
  }
  //////////////////////////////////////////////////////////////////////////////
  
  //sdfile = SD.open(filename);
  Serial.println("Setup complete\n");
  Serial.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.close();
  readMicroSDcard();
 
} // setup()
      

void loop() {
  
    readMicroSDcard();
    sdfile = SD.open(filename);
    timerInterval = 1000 * Offsets[0];
   
    if (Offsets[0] < 5) {
      Serial.println("Time to short, please choose longer than 5sec");
      while(1) {  }
    }
   

    if (timerLast > millis())  timerLast = millis();
    if ((millis() - timerLast) > timerInterval) {

      
      DateTime now = DataLoggerRTC.now();

      CO2 = airSensor.getCO2();
      Temperature = airSensor.getTemperature();
      Humidity = airSensor.getHumidity();
      sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",
          now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());

    
      CO2ppm = CO2 - Offsets[1];
      TempC = Temperature - Offsets[2];
      HumidityPct = Humidity - Offsets[3];

      

      Serial.print(buf1);
      Serial.print(";");
      Serial.print(CO2ppm);
      Serial.print(";");
      Serial.print(TempC);
      Serial.print(";");
      Serial.println(HumidityPct);
      
      // Write to the SD card..
      sdfile.print(buf1);  // 20200216T150644Z
      sdfile.print(";");
      sdfile.print(CO2ppm);
      sdfile.print(";");
      sdfile.print(TempC);
      sdfile.print(";");
      sdfile.println(HumidityPct);
    // Execute a flush() to insure it is written since no sdfile.close() will be issued.
      sdfile.flush();
      timerLast = millis();
      sdfile.close();
  }
  
  yield();
  
} // loop()

I think this sdfile.flush() is not needed.

unfortunately it changes nothing on the result!!

On loop change sdfile = SD.open(filename) to sdfile = SD.open(filename, FILE_WRITE).

nope, still no difference!! Its been 2 days that I'm hanging here on this problem its driving me crazy!

HALLELLUJAH, it worked. I moved the sdfile.open out from the begginng of the loop and wrote in the end of the loop before it print the values to the sd card and it workeed even though it shouldnt make any difference in my opinion, but anyway I'm very happy that it finally works.
It seems that now it doesnt read the values from the offsets file but thats smth I think I'll figure it out. Thank you very much man, I really appreciate your help.

For me it worked.

  sdfile = SD.open(filename, FILE_WRITE);

  Serial.print("The current filename is: ");
  Serial.println(filename);

  if(!sdfile)
  {
    Serial.println("Cant open the file");
  }

Maybe you are looking for the wrong file. The filename is not changed inside of the loop.

Captura de tela de 2022-06-25 13.33.57

void setup() {  
  Serial.begin(9600);
  while (!Serial) {
    delay(1);
  }

  // pinMode(pin_LED, OUTPUT);
//////////////////////////////////////////////////////////////////////////////
  // Sensirion SCD30 CO2 sensor
  Wire.begin();
  if (airSensor.begin() == false) {
    Serial.println("ERROR - CO2 sensor not detected.  Check wiring");
    while(1) { }
  }
  pinMode(pinRDY, INPUT);

  //////////////////////////////////////////////////////////////////////////////
  // Initialize the DataLogger RTC
  if (!DataLoggerRTC.begin()) {
    Serial.println("ERROR - Couldn't find DataLogger RTC");
    while(1) { } 
  }

////////////////////////////////////////////////////////////////////////
// see if the SD card is present and can be initialized: 
  if (!SD.begin(SDchipSelect)) {
    Serial.println("SD card failed, or not present");
    while(1) { } 
  }
   Serial.println("SD card initialized."); 
  // Create a filename reference to a file that doesn't exist 'ANALOG00.TXT'..'ANALOG99.TXT'
  //char filename[14];
  strcpy(filename, "/ANALOG00.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[7] = '0' + i/10;
    filename[8] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (SD.exists(filename)){
      Serial.print("File '");
      Serial.print(filename);
      Serial.println("' already exists");
    } else {
      Serial.print("New file will be '");
      Serial.print(filename);
      Serial.println("'");
      break;
    }
  }

  // Open file on SD Card for writing
  sdfile = SD.open(filename, FILE_WRITE);
  if (! sdfile) {
    Serial.print("ERROR - unable to create '");
    Serial.print(filename); Serial.println("'");
    while(1) { } 
  }
  //////////////////////////////////////////////////////////////////////////////
  
  //sdfile = SD.open(filename);
  Serial.println("Setup complete\n");
  Serial.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.println("Date/Time; CO2ppm; TempC; Humidity%");
  sdfile.close();
  readMicroSDcard();
 
} // setup()
      

void loop() {
  
    readMicroSDcard();
    //sdfile = SD.open(filename, FILE_WRITE);
    timerInterval = 1000 * Offsets[0];
   
    if (Offsets[0] < 5) {
      Serial.println("Time to short, please choose longer than 5sec");
      while(1) {  }
    }
   

    if (timerLast > millis())  timerLast = millis();
    if ((millis() - timerLast) > timerInterval) {

      
      DateTime now = DataLoggerRTC.now();

      CO2 = airSensor.getCO2();
      Temperature = airSensor.getTemperature();
      Humidity = airSensor.getHumidity();
      sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",
          now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());

    
      CO2ppm = CO2 - Offsets[1];
      TempC = Temperature - Offsets[2];
      HumidityPct = Humidity - Offsets[3];

      

      Serial.print(buf1);
      Serial.print(";");
      Serial.print(CO2ppm);
      Serial.print(";");
      Serial.print(TempC);
      Serial.print(";");
      Serial.println(HumidityPct);
      
      // Write to the SD card..
      sdfile = SD.open(filename, FILE_WRITE);
      sdfile.print(buf1);  // 20200216T150644Z
      sdfile.print(";");
      sdfile.print(CO2ppm);
      sdfile.print(";");
      sdfile.print(TempC);
      sdfile.print(";");
      sdfile.println(HumidityPct);
    // Execute a flush() to insure it is written since no sdfile.close() will be issued.
      sdfile.close();
      timerLast = millis();
      //sdfile.flush();
  }
  
  yield();
  
}

This works for me finally!

Mark the topic as solved.