Clock on Data Logger not keeping time when disconnected from

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
KG457
 
Posts: 15
Joined: Sat Oct 04, 2014 6:32 pm

Clock on Data Logger not keeping time when disconnected from

Post by KG457 »

I am using an Arduino Uno with an Adafruit Data Logger attached. My development system is running on a Mac.

I have combined elements of the example data logger and ds137 programs to create a program that sets the time to the current PC time, then every three seconds, writes that time to a text file on the SD card.

The program works perfectly when the Arduino/Adafruit boards are connected to the Mac, and observed via the development system’s serial terminal. However, when the board is disconnected from the Mac, and is running on only the CR1220 button battery, it seems the time does not advance correctly. When the boards are connected to external power via a USB charger, the time is 3 to five minutes behind where it was when power was disconnected.

Below is a summary from the log file: Note that then the power is disconnected from the Mac, the minutes change from 57 to 55. After that, the time never catches up until the unit is reconnected to the Mac. In this example, the hour field never advanced at all.
In this example, used an external wall clock as a comparison because when disconnecting the Arduino/Adafruit unit, I can no longer watch the time until I read it in the log file. So I started the program, let it run for two minutes, disconnected from the Mac, connected to a USB charger, waited, and then disconnected. The key change points are shown. below.

In case this reformats for your view, there are three columns: External comparison clock time is on the left, action taken is in the middle, and the time recorded in the log file is on the right. There will be some differences due to the fact that I am connecting and disconnecting the unit based on the wall clock time and then trying to figure out wham that event actually occurred when looking at the log file. In the log file, there are time recordings every three seconds. The log file time is formatted as YYYY/MM/DD/HH:MM:S

External
Comparison Time reported in log file
Clock Time Action YYYY/MM/DD:HH:MM:S
10:56:01 Load Program 2014/10/12:10:56:2
10:58:01 Disconnect from Mac 2014/10/12:10:57:52
And connect to USB Charger 2014/10/12:10:55:50
11:01:04 Disconnect from USB
Charger and remove SD card 2014/10/12:10:57:52

I thought it might be the CR1220 battery but it is at 3.32 volts.

What is likely to be the problem, and what can I do to fix it?

I am adding this comment after posting the above. Seems my table did not format online the same as it looked to me while editing.
I will try to figure out a better way to present the data. Suggestions are welcome.

User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: Clock on Data Logger not keeping time when disconnected

Post by Franklin97355 »

to create a program that sets the time to the current PC time, then every three seconds, writes that time to a text file on the SD card.
When you are not plugged in how does your program get the time? Could you post your code and a description or drawing of your connections between it all?
Please use the code button "</>" or

Code: Select all

 tags when posting code to the forums.

User avatar
KG457
 
Posts: 15
Joined: Sat Oct 04, 2014 6:32 pm

Re: Clock on Data Logger not keeping time when disconnected

Post by KG457 »

Thank you. When the Arduino/Adafruit SD card board is operating without being connected to a Mac or PC, the time comes from the onboard clock. That's a central and important feature of the card. According to what I read in the documentation, that clock is supposed to run continuously, powered by the 3v button lithium battery.

The code is a combination of two programs that are part of the SD card package. When pasting the code, I used the code button for the text shown below.

Code: Select all

/*
  SD card program DataLogger, modified with excerpts from program ds1307
  
 
 The Datalogger example shows how to log data from three analog sensors 
 to an SD card using the SD library.
 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** UNO:  MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
  and pin #10 (SS) must be an output
 ** Mega:  MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
  and pin #52 (SS) must be an output
 ** Leonardo: Connect to hardware SPI via the ICSP header
 		Pin 4 used here for consistency with other Arduino examples
 
 SD portion created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */


// lines from Datalogger
#include <SPI.h>
#include <SD.h>

// lines from ds1307

#include <Wire.h>
#include"RTClib.h"

RTC_DS1307 rtc;  // from Clock set

// lines from Datalogger

// CS stands for ChipSelect

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
//const int chipSelect = 4;
const int chipSelect = 10;  //required for the Aduino Uno

int Counter = 0;

File dataFile;

void setup()
{
 
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }
  Serial.println("card initialized.");
  
  // Open up the file we're going to log to!
  dataFile = SD.open("datalog2.csv", FILE_WRITE); // there appears to be an upper limit on file name length dk the limit
  
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }



// selection from ds1307

#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();
  
 
  // Set the time to the time on the connected computer 
  Serial.println("Setting time");
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    
    // The line below 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));
  }




}

void loop()
{
  
  
  // make a string for assembling the data to log:
  String dataString = "";
  double VoltageConvertor = 1023/5;

// Send current time information to the serial connection
   
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ,");
    Serial.print(now.unixtime());
    Serial.print("s ,");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");

// End from Clock Set

dataString += String(millis());
dataString += ",";


// Add time information to the string

dataString += String(now.year(),DEC);
dataString += ",";
dataString += String(now.month(),DEC);
dataString += ",";
dataString += String(now.day(),DEC);
dataString += ",";
dataString += String(now.hour(),DEC);
dataString += ",";
dataString += String(now.minute(),DEC);
dataString += ",";
dataString += String(now.second(),DEC);
dataString += ",";
dataString += " Since midnight 1970_1_1 "; 
dataString += ",";
dataString +=  String(now.unixtime());
dataString += ",";
dataString += " Seconds and  , ";
dataString += (now.unixtime()/86400L);
dataString += " , ";
dataString += " Days ";

dataString += ",";
Counter +=1;
dataString += String(Counter);
dataString += ",";

// read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor/VoltageConvertor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }

  dataFile.println(dataString);

  // print to the serial port too:
  Serial.println(dataString);
  
  // The following line will 'save' the file to the SD card after every
  // line of data - this will use more power and slow down how much data
  // you can read but it's safer! 
  // If you want to speed up the system, remove the call to flush() and it
  // will save the file only every 512 bytes - every time a sector on the 
  // SD card is filled with data.
  dataFile.flush();
  
  // Take 1 measurement every 3000 milliseconds
  delay(3000);
}

User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: Clock on Data Logger not keeping time when disconnected

Post by Franklin97355 »

Code: Select all

  Serial.println("Setting time");
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
This code sets the clock to the time the program was compiled. This should not be run every time you run your program.

User avatar
KG457
 
Posts: 15
Joined: Sat Oct 04, 2014 6:32 pm

Re: Clock on Data Logger not keeping time when disconnected

Post by KG457 »

Thank you. I will try that.

User avatar
KG457
 
Posts: 15
Joined: Sat Oct 04, 2014 6:32 pm

Re: Clock on Data Logger not keeping time when disconnected

Post by KG457 »

I tested by running the program twice. First run was to set the clock, and the second run was for use over an extended period of time. For the second run I commented out the two lines you mentioned. It worked!!! Now that I see the correct way to run it, it makes perfect sense. The program was probably trying to reset the clock every time it restarted, and when there's no PC connected, it can't do that. Thank you very very much for your assistance.

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

Return to “Arduino Shields from Adafruit”