Arduino hangs up randomly at SD-writing


In my project about a datalogger with temperature, humity, light intensity … i got a problem with no solution until now so i would like to ask some experts.

I have a arduino Uno R3 with an ethernet shield, DHT22 sensor, BMP280 sensor, TSL2591 sensor and a RTC DS3231 module.
My project of datalogging some physical sizes to the sd ard worked fine without any timestamps/time informations … but now i would like to record the physical sizes with a timestamp. So i tried to expand my code but this did not work a better i do not get the timestamp on the sd card.

I decided to try it just with the DHT22 sensor. I took the code of the examples of RTC, DHT, SD and built the code. This code is shown below:

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>

#define DHTPIN 9
#define DHTTYPE DHT22   // DHT 22  (AM2302)

RTC_DS3231 rtc;

const int chipSelect = 4;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char CycleTimeStamp[ ] = "0000/00/00,00:00";

void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero


  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // 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));

  // Setting the SPI pins high helps some sd cards go into sleep mode 
  // the following pullup resistors only need to be enabled for the stand alone logger builds - not the UNO loggers
  pinMode(chipSelect, OUTPUT); digitalWrite(chipSelect, HIGH); //Always pullup the CS pin with the SD library
  //and you may need to pullup MOSI/MISO
  //pinMode(MOSIpin, OUTPUT); digitalWrite(MOSIpin, HIGH); //pullup the MOSI pin
  //pinMode(MISOpin, INPUT); digitalWrite(MISOpin, HIGH);  //pullup the MISO pin
    Wire.begin();          // start the i2c interface for the RTC
   while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card…");
// print lines in the setup loop only happen once
// 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:
  Serial.println("card initialized.");
  // You must already have a plain text file file named ‘datalog.txt’ on the SD already for this to work!
  //————-print a header to the data file———-
  File dataFile ="datalog.txt", FILE_WRITE);
  if (dataFile) { // if the file is available, write to it:
    dataFile.println("Timestamp, Temperatur, ");
    //I often print many extra lines of text in file headers, identifying details about the hardware being used, the code version that was running, etc
  else {
    Serial.println("error opening datalog.txt"); // if the file isn’t open, pop up an error:

void loop () {
    DateTime now =;
    sprintf(CycleTimeStamp, " % 04d / % 02d / % 02d % 02d: % 02d", now.year(), now.month(),, now.hour(), now.minute());
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(, DEC);
    Serial.print(" (");
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);
   float temp = dht.readTemperature();

   Serial.print(". Temperatur: ");
   Serial.println(" Celsius");
   String dataString = ""; //this line simply erases the string
   dataString += CycleTimeStamp;
   dataString += ", ";     //puts a comma between the two bits of data
   dataString = dataString + String(temp);

   //——– Now write the data to the SD card ——–
   File dataFile ="datalog.txt", FILE_WRITE);// if the file is available, write to it:
   if (dataFile) {
   else {
   Serial.println("error opening datalog.txt"); // if the file isn’t open, pop up an error: 

The serial monitor gives me the following informations and hangs up at randomly different points…

Initializing SD card…card initialized.
2017/7/14 (Friday) 16:37:27
. Temperatur: 24.00 Celsius
  2017 /  7 /  14  16:  37, 24.00/r/n/r/n
2017/7/14 (Friday) 16:37:32

In the datalog file i got the following information:

Timestamp, Temperatur, 

Ô  k6  ¸ð6a? €ñ³D
Timestamp, Temperatur, 

(6ñ ð6a? €ñ³D

When i diasable the code for writing on the sd card, the logger works fine and the string which is shown on the serial monitor is correct. Activating the code for sd writing the some problems appear.
The powersupply was first done by the usb cable but there was no change by using an external power supply.

So it would be fantastic if someone could help me.

many thanks

This looks like you ran out of memory. Don't use the String class as it's a memory waster. Learn to use the F() macro to not use RAM for fixed strings. The UNO has 2kB of RAM, the SD library uses more than a quarter of it without having done anything.

Take the spaces out of the sprintf. Your CycleTimeStamp is declared without them so you overflow that variable.