Datalogger with UNO and SD/RTC shield

Hello,

I'm hanging with a simple project that must have been done in millions over the last 10 years...

I have an old UNO (with the DIP ATMega328) and an RTC/SD-shield for it which is still sold from several suppliers. IDE is 2.2.1

But I can't get the SD functions and RTC functions working in the same sketch, to fullfill my simple aim: Logging some measurements/events with timestamps.

Writing something to the SD card works, using (built-in) SD.h.
Setting and reading the RTC works using RTC.h from "Manjunath CV".

But if I try to combine the two sketches, all readings from RTC like RTC.getDay() result in strange numbers, until I strip off anything which initializes the SD card.

What's the problem?
I found a YT tutorial that propagates another RTC lib, but I did not get how to install this lib. Simply copying the lib folder into the library folder did not work.

Thanks for your help in advance...
Tom.

Edit:
I copied - according to the old YT tutorial - the alternative RTC lib into the Arduino lib folder in "Program Files(x86)"... obviously the latest IDE resides in "Program Files".
So this cannot work.
The folder structure in "Program Files/Arduino IDE" is much more complicated, how does it work to install a library which is not handled by the Library Manager?

i see no sketch

That shouldn't be a problem.

Which one? Do you have any documentation for it. A schematic would be better.

There's an Adafruit data logger shield that looks like this:
image
And I suspect there are many clones of it.

The Adafruit board uses SPI for the card access and I2C for the RTC access.

You need to post details of your datalogger board as well as the code that you have that does not work in order to get more help from the forum.

Thanks for your answers.

Yes, it is a clone of the Adafruit board.

Of course, after my first despair I kept on trying.
So I set the filter in the library manager to "recommended", and the search for "RTC" showed the Adafruit RTClib.h first in row, not Manjunath's RTC.h
Uninstalled the latter and installed Adafruit's - and the example code for a datalogger works.

And yes, the sketch includes not only SD.h and RTClib.h, but also Wire.h and SPI.h.

//more Information at: https://www.aeq-web.com/
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

const int chipSelect = 10; //10 is default by shield, but normally on Pin 4
int interval = 5;  //Log to SD Card every 5 seconds

long timer;
String timestring;
String mvalue;

RTC_DS1307 rtc;

void setup() {
  Serial.begin(9600);
  delay(3000);
  Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("SD Card error");
    return;
  }
  Serial.println("card initialized");
  if (! rtc.begin()) {
    Serial.println("No RTC found");
  } else {
    Serial.println("RTC clock found");
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is not configured");
  }
}

void loop() {
  if ((timer + interval * 1000) < millis()) {
    timer = millis();
    get_logvalue(); //Get your value
    get_time(); //Get time from RTC
    write_data(); //Write value and Time to SD
  }
}

void get_logvalue() {
  mvalue = "My Value"; //mvalue is your log parameter eg. Temperature
}

void get_time(){ //Read Time from RTC
  DateTime now = rtc.now();
  timestring = now.day();
  timestring += "-";
  timestring += now.month();
  timestring += "-";
  timestring += now.year();
  timestring += " ";
  timestring += now.hour();
  timestring += ":";
  timestring += now.minute();
  timestring += ":";
  timestring += now.second();
  Serial.println(timestring);
}

void write_data() { //Write to SD card
  String dataString = mvalue + "," + timestring;
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else {
    Serial.println("error writing datalog.txt");
  }
}

This is crazy.
Although a have a working example now, I wanted to find back to the situation where I was so despaired yesterday. Maybe I forgot to include Wire.h?
But I can't get back to the sketch that malfunctioned yesterday.

The malfunction was:
The RTC was (probably successfully) set to the date 23-10-2023.
But reading the day with String(RTC.getDay()) always gave me back "165".
Unless I stripped off everything which dealt with the SD card.
That's what I remember from yesterday.

Today, I can mistreat my code in any way - it still gives me back 23.
Even skipping Wire.h and SPI.h, and just including SD.h and Manjunath's I2C_RTC.h: Still works.

Here's the mistreated code:

//#include <Wire.h>
#include <SD.h>
#include <I2C_RTC.h>

const int chipSelect = 10; //10 is default by shield, but normally on Pin 4

static DS1307 RTC;

void setup()
{
  Serial.begin(9600);
  delay(1000);

  Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect))
  {
    Serial.println("SD Card error");
    return;
  }
  Serial.println("card initialized");
    
  RTC.begin();
  RTC.setHourMode(CLOCK_H24);
  RTC.setWeek(2);
  RTC.setDate(23,10,23);
  RTC.setTime(15,41,00);
}

void loop()
{
  delay(1000);
  String dataString = String(RTC.getDay());
  Serial.println(dataString);
  
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
    if (dataFile)
    {
      dataFile.println(dataString);
      dataFile.close();
    }
    else
    {
      Serial.println("error writing datalog.txt");
    }
}

This say nothing about the fact either the RTC works or not.
You first send this date to the RTC :

and then read it from the RTC in the same code

To be sure that RTC is running try to comment the lines of the code that set the time in the setup()

Ok, I did a short test and read Seconds instead of Days. They count up. The RTC works.

My problem yesterday, which I can't provoke any longer, was the communication. Wrong readings like 165 instead of 23.

But let's not put too much time into investigating what went wrong yesterday.
I have a working example now, and will devolop this into my application.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.