datalogger shield time stamp on SD card different than println time

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mromero
 
Posts: 94
Joined: Fri Mar 18, 2016 12:10 pm

datalogger shield time stamp on SD card different than println time

Post by mromero »

serial console.png
serial console.png (16.94 KiB) Viewed 272 times
output file.png
output file.png (8.73 KiB) Viewed 272 times
the time stamp in the file on the sd card has the correct date but the time is different than what the time on the computer. the computer time matches the serial console time. I thought if you compiled before sending code to the arduino that the line:

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

would set the time to last time you compiled the sketch but im 1 hr off (see serial console and txt file attached)

what am i doing wrong?





Code: Select all

//2CO2_to_I2Cv4_loggerv4  04/03/2023
#include <Wire.h>
#include <SD.h>//sd card
#include <SPI.h>
#include <RTClib.h>// clock

// We will be using the I2C hardware interface on the Arduino in
// combination with the built-in Wire library to interface.
// Arduino analog input 5 - I2C SCL
// Arduino analog input 4 - I2C SDA
/*
  In this example we will do a basic read of the CO2 value and checksum verification.
  For more advanced applications please see the I2C Comm guide.
*/

//Global variable declaration
//int co2Addr =0;
int co2Addr1 = 0x69;// CO2 sensor 1
int co2Addr2 = 0x56;// CO2 sensor 2

int co2_value_1;
int co2_value_2;


// This is the default address:0x68 of the CO2 sensor, 7bits shifted left.  for sensor board 2 changed to 0X6A

RTC_PCF8523 rtc;// clock on datalogger

// create file
File myFile;
//String Date,Time,CO2_1,CO2_2;
String Date,Time;

void setup() {//=============================================================

  //start clock
    rtc.begin();
    
  //serial
  Serial.begin(9600);
  delay(2000);

   //clock 
 if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
    if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }  // close clock

  // SD card initialize

  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  myFile = SD.open("data.txt", FILE_WRITE); 
  if (myFile) {
    myFile.println( "Date, Time, CO2_1(ppm), CO2_2(ppm)) \r\n");
    myFile.close();
  } 
  else {
    Serial.println("error opening data.txt");
  }

  
  
  Wire.begin ();
  pinMode(13, OUTPUT); // address of the Arduino LED indicator
  Serial.println("Application Note AN-102: Interface Arduino to two  K-333");

  
}//==================================================== close setup Loop


///////////////////////////////////////////////////////////////////
// Function : int readCO2_1()
// Returns : CO2 Value upon success, 0 upon checksum failure
// Assumes : - Wire library has been imported successfully.
// - LED is connected to IO pin 13
// - CO2 sensor address is defined in co2_addr
///////////////////////////////////////////////////////////////////

int readCO2_1()
{//+++++++++++++++++++++++++++++++++++++++++++++++++  read CO2()function definition
   co2_value_1 = 0;  // We will store the CO2 value inside this variable.

  digitalWrite(13, HIGH);  // turn on LED
  // On most Arduino platforms this pin is used as an indicator light.

  //////////////////////////
  /* Begin Write Sequence */
  //////////////////////////
  
  Wire.beginTransmission(0x69);
 // Wire.beginTransmission(co2Addr1);
  Wire.write(0x22);
  Wire.write(0x00);
  Wire.write(0x08);
  Wire.write(0x2A);

  Wire.endTransmission();

  /////////////////////////
  /* End Write Sequence. */
  /////////////////////////

  /*
    We wait 10ms for the sensor to process our command.
    The sensors's primary duties are to accurately
    measure CO2 values. Waiting 10ms will ensure the
    data is properly written to RAM

  */

  delay(10);

  /////////////////////////
  /* Begin Read Sequence */
  /////////////////////////

  /*
    Since we requested 2 bytes from the sensor we must
    read in 4 bytes. This includes the payload, checksum,
    and command status byte.

  */

  Wire.requestFrom(0x69, 4);

  byte i = 0;
  byte buffer[4] = {0, 0, 0, 0};

  /*
    Wire.available() is not nessessary. Implementation is obscure but we leave
    it in here for portability and to future proof our code
  */
  while (Wire.available())
  {
    buffer[i] = Wire.read();
    i++;
  }

  ///////////////////////
  /* End Read Sequence */
  ///////////////////////

  /*
    Using some bitwise manipulation we will shift our buffer
    into an integer for general consumption
  */

  co2_value_1 = 0;
  co2_value_1 |= buffer[1] & 0xFF;
  co2_value_1 = co2_value_1 << 8;
  co2_value_1 |= buffer[2] & 0xFF;


  byte sum = 0; //Checksum Byte
  sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow

  if (sum == buffer[3])
  {
    // Success!
    digitalWrite(13, LOW);
    return co2_value_1;
  }
  else
  {
    // Failure!
    /*
      Checksum failure can be due to a number of factors,
      fuzzy electrons, sensor busy, etc.
    */

    digitalWrite(13, LOW);
    return 0;
  }
}//++++++++++++++++++++++++++++++++++++++++++ close the read CO2 function definition

///////////////////////////////////////////////////////////////////
// Function : int readCO2_2()
// Returns : CO2 Value upon success, 0 upon checksum failure
// Assumes : - Wire library has been imported successfully.
// - LED is connected to IO pin 13
// - CO2 sensor address is defined in co2_addr
///////////////////////////////////////////////////////////////////

int readCO2_2()
{//+++++++++++++++++++++++++++++++++++++++++++++++++  read CO2()function definition
   co2_value_2 = 0;  // We will store the CO2 value inside this variable.

  digitalWrite(13, HIGH);  // turn on LED
  // On most Arduino platforms this pin is used as an indicator light.

  //////////////////////////
  /* Begin Write Sequence */
  //////////////////////////

  Wire.beginTransmission(0x56);
  Wire.write(0x22);
  Wire.write(0x00);
  Wire.write(0x08);
  Wire.write(0x2A);

  Wire.endTransmission();

  /////////////////////////
  /* End Write Sequence. */
  /////////////////////////

  /*
    We wait 10ms for the sensor to process our command.
    The sensors's primary duties are to accurately
    measure CO2 values. Waiting 10ms will ensure the
    data is properly written to RAM

  */

  delay(10);

  /////////////////////////
  /* Begin Read Sequence */
  /////////////////////////

  /*
    Since we requested 2 bytes from the sensor we must
    read in 4 bytes. This includes the payload, checksum,
    and command status byte.

  */

  Wire.requestFrom(0x56, 4);

  byte i = 0;
  byte buffer[4] = {0, 0, 0, 0};

  /*
    Wire.available() is not nessessary. Implementation is obscure but we leave
    it in here for portability and to future proof our code
  */
  while (Wire.available())
  {
    buffer[i] = Wire.read();
    i++;
  }

  ///////////////////////
  /* End Read Sequence */
  ///////////////////////

  /*
    Using some bitwise manipulation we will shift our buffer
    into an integer for general consumption
  */

  co2_value_2 = 0;
  co2_value_2 |= buffer[1] & 0xFF;
  co2_value_2 = co2_value_2 << 8;
  co2_value_2 |= buffer[2] & 0xFF;


  byte sum = 0; //Checksum Byte
  sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow

  if (sum == buffer[3])
  {
    // Success!
    digitalWrite(13, LOW);
    return co2_value_2;
  }
  else
  {
    // Failure!
    /*
      Checksum failure can be due to a number of factors,
      fuzzy electrons, sensor busy, etc.
    */

    digitalWrite(13, LOW);
    return 0;
  }
}//++++++++++++++++++++++++++++++++++++++++++ close the read CO2 function definition

//--------------------subroutines------------------------------------

void data_logging() {
 //  CO2_1 = String(co2_value_1);
 //  CO2_2 = String(co2_value_2);
// 
// Data = CO2_1 + "," + CO2_2  ;
//  

  Serial.print("Save data: ");
 // Serial.println(Data);

  myFile = SD.open("data.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Writing to data.txt...");
    
    //time stamp
    DateTime now = rtc.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(",");
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(",");//delimeter between time and data
    //
   // myFile.println(Data);//ln adds carriage return
   myFile.print(co2_value_1);
   myFile.print(",");
   myFile.println(co2_value_2);
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening data.txt");
  }  
  Serial.println();
}

//--------------------------


void loop() {//#####################################################void loop


 //once for sensor1 ADDRESS0x69//****************************************************************

 // int co2Value = readCO2();// orig
    co2_value_1 = readCO2_1()*10;// what i was told to do. this gives half of what the dev kit prewired one reads.

  
  if (co2_value_1 > 0)
  {
    Serial.print("CO2 Value1: ");
    Serial.println(co2_value_1);
  }
  else
  {
    Serial.println("Checksum failed / Communication failure");
  }
  delay(1000);
  //********************************************************************done with CO2value1

  
//once for sensor2 ADDRESS0x56//****************************************************************

// orig location of dataloging but if placed here then  it would dataloge before it had values of Co2 moved it to end
// 
  co2_value_2 = readCO2_2()*10;// what i was told to do. this gives half of what the dev kit prewired one reads.

  
  if (co2_value_2 > 0)
  {
    Serial.print("CO2 Value2: ");
    Serial.println(co2_value_2);
  }
  else
  {
    Serial.println("Checksum failed / Communication failure");
  }
  delay(1000);
  

     // make a string for assembling the data to log:
     data_logging();
     
     delay(1000);//time between writting to sd card: delay(ms) 1000=1sec ; 900,000=15 min use this for final use 20,000 for testing
  
}//##############################################################################close loop

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: datalogger shield time stamp on SD card different than println time

Post by adafruit_support_bill »

Was the code compiled before or after the daylight saving time-shift?

User avatar
mromero
 
Posts: 94
Joined: Fri Mar 18, 2016 12:10 pm

Re: datalogger shield time stamp on SD card different than println time

Post by mromero »

yes i compiled right before sending to logger

i also noted when i tested the freshly compiled example code pcf8523.ino my output date from showed 9:28 while pc time was11:05 see attachment
pcf8523example.png
pcf8523example.png (61.85 KiB) Viewed 266 times

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: datalogger shield time stamp on SD card different than println time

Post by adafruit_support_bill »

Did you see the message "RTC is NOT running, let's set the time!"?

If the code detected that the RTC was already running, it would not reset the time. To stop the RTC, power everything down and remove the coin-cell on the shield for a minute or so. That should shut down the clock so that it will get reset to the compile time the next time the program runs.

User avatar
mromero
 
Posts: 94
Joined: Fri Mar 18, 2016 12:10 pm

Re: datalogger shield time stamp on SD card different than println time

Post by mromero »

thanks i will try that

User avatar
mromero
 
Posts: 94
Joined: Fri Mar 18, 2016 12:10 pm

Re: datalogger shield time stamp on SD card different than println time

Post by mromero »

thanks now it seems the clock is not working . I did as you suggested took battery out the re-installed re-compiled and sent program now it seems to be stuck in 2010 with no day or month or time.

I noted the 3V battery read 2.96V is there a way to output the value of the voltage from the coin battery in the datalogger shield to the file so when it starts going the user can change it?
output file.png
output file.png (12.11 KiB) Viewed 248 times

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: datalogger shield time stamp on SD card different than println time

Post by adafruit_support_bill »

A bit low, but I'd expect 2.96v to still be enough to run the clock. Try cleaning the battery (a mildly abrasive pencil eraser works well).

There is no circuit to monitor the battery voltage built into the shield. You could solder a wire from the battery holder to one of the analog pins and read the voltage that way.

User avatar
mromero
 
Posts: 94
Joined: Fri Mar 18, 2016 12:10 pm

Re: datalogger shield time stamp on SD card different than println time

Post by mromero »

Thanks for the replies. I was looking over the provided pcf8523.ino and there is code for if power is lost to have the clock reset:

Code: Select all

  if (! rtc.initialized() || rtc.lostPower()) {
    Serial.println("RTC is NOT initialized, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Is there a way to have it reset clock each time you send the program even if it had been initialized before and the battery did not loose power?

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: datalogger shield time stamp on SD card different than println time

Post by adafruit_support_bill »

If you remove the 'if' condition completely, the rtc.adjust will set the time unconditionally. The problem is, if you restart the program, it will reset your clock to the last compile time - not the current time.

What you can do is make a simple program that does nothing except set the clock. You can compile and send that whenever you need to adjust the clock time. Then you can re-send your regular program to continue with normal operation.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”