Data Logging Shield: wrong RTC in the logged Data


I’m ussing the Data Logging Shield for Arduino. In my code, the data will be logged. This works, but I have a problem with the RTC. In my logged Data on the SD Card, I don’t get the real time. I get only the real time in ste serial monitor.
The is the code:

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include “RTClib.h”

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB

RTC_Millis rtc;
File dataFile;

const int audioPin = A0;
const int sensitivity = 450;

const int chipSelect = 10;

void setup() {

rtc.begin(DateTime(F(DATE), F(TIME)));

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 =“datalog.txt”, FILE_WRITE);
if (! dataFile) {
Serial.println(“error opening datalog.txt”);
// Wait forever since we cant write data
while (1) ;

void loop() {

// make a string for assembling the data to log:
String dataString = “”;

DateTime now =;

Serial.print(now.year(), DEC);
dataString += (now.year(), DEC);
dataString = (’/’);
Serial.print(now.month(), DEC);
dataString += (now.month(), DEC);
dataString += (’/’);
Serial.print(, DEC);
dataString += (, DEC);
Serial.print(’ ‘);
dataString += (’ ‘);
Serial.print(now.hour(), DEC);
dataString +=(now.hour(), DEC);
dataString += (’:’);
Serial.print(now.minute(), DEC);
dataString += (now.minute(), DEC);
dataString += (’:’);
Serial.print(now.second(), DEC);
dataString += (now.second(), DEC);
dataString += (’ ');
int soundWave = analogRead(audioPin);
dataString += String(soundWave);
if (soundWave>sensitivity) {
Serial.println(“Achtung!! Lärm!!”);
dataString += (“Achtung!! Lärm!!”);





Maybe some can help me?


code.txt (2.31 KB)

 dataString += (now.year(), DEC);

What, EXACTLY, do you think is being appended to the String instance, and why?

Just because (now.year(), DEC) following Serial.print accomplished something does NOT mean that that snippet of text in some other place will accomplish the same thing.

Something like

dataString += (, DEC);

is nonsense.

I don't use String with capital S and you should also forget that it exists (it can result in undefined behaviour at run time), but I think the following would be a step in the right direction if you want to stick to String.

dataString += String(;