Go Down

Topic: Trouble with Data Logger (Read 755 times) previous topic - next topic


Hi all, fairly new here. I'm trying to do a very simple data logger and have been having trouble. I've simplified my code (attached) to simply read an RTC time and write it to an SD card. I have measurements with a DHT sensor I want to timestamp, but my troubleshooting says its a memory or other problem.

My issue now: It works for awhile, writes the time to my card, but after so long, it slows down substantially and eventually stops writing to the file at all. I'm using some LEDs to help me see what's going on.

If I press the reset button, it works again.

My loop is running fast so I can more quickly see when it fails vs. waiting several hours. Any ideas what I am screwing up?


Please read the readmes and post your code in the proper manner, with code tags </>.
What you are screwing up may be the fast loop. It would pay to run it at the speed that is intended.


Sorry about that. I'm very new here.

Anyways, when I run it slow (2 seconds between readings), the same thing happens, it just takes longer before it fails.

Code: [Select]
#include <SPI.h>
#include <SD.h>

#include "RTClib.h" //include Adafruit RTC library

RTC_DS3231 rtc; //Make a RTC DS3231 object

//Set the names of days
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int led = 2;
int led2 = 3;

File myFile;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  if (!SD.begin(4)) {
    Serial.println("SD initialization failed!");
    digitalWrite(led2, HIGH);
    while (1);

  if (! rtc.begin()) {
   Serial.println("Couldn't find RTC");
   digitalWrite(led, HIGH);
   digitalWrite(led2, HIGH);
   while (1);
 //Setup of time if RTC lost power or time is not set
 if (rtc.lostPower()) {

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

void loop() {

 //Set now as RTC time
 DateTime now = rtc.now();
myFile = SD.open("test.txt", FILE_WRITE);
digitalWrite(led, HIGH);
  // if the file opened okay, write to it:
  if (myFile) {   
    myFile.print(now.year(), DEC);
    myFile.print(now.month(), DEC);
    myFile.print(now.day(), DEC);
    myFile.print(" ,(");
    myFile.print(", ");
    myFile.print(now.hour(), DEC);
    myFile.print(now.minute(), DEC);
    myFile.println(now.second(), DEC);


    digitalWrite(led, LOW);
  } else {
    // if the file didn't open, print an error:
    Serial.println(F("error opening test.txt"));
    digitalWrite(led2, HIGH);
    digitalWrite(led2, LOW);



Oct 20, 2018, 05:43 am Last Edit: Oct 20, 2018, 04:15 pm by Nick_Pyner
It may not be the solution, but your loop is running unreasonably fast. Your clock can only deliver useful information at one second intervals but the loop is 1/10th second, thereby making your data 90% rubbish. Perhaps this is just a typo, but make it
and see if it helps.

Also, confine the SD tests to setup, and keep those conditionals out of the loop, i.e. get the time, and record it - no unnecessary fartarsing about (unless you seriously think somebody is going to come in and steal the card).

No comment on the LED stuff, but it seems redundant.


Why are youbusing leds and not the Serial monitor? You can use it to see better ehat the program does or not, and other usefull informations like values of your variabke and ehat the program is writing.
Dove va un numero va una variabile o una funzione. E dove va una boolean va un insieme di test.

Se vuoi ottenere devi saper spiegare

In pochi capiscono l'importanza di formattare, sii tra di essi


Long term, this data logger will be placed outside and I wanted a way to be able to check that its running without having to take my laptop out and plug it in. A quick glance out the window and I can see if all is well.

Go Up