Hello,
i have a problem with the DS1307. It should print every 3 hours the time and date. But only the first line is correct.
The file looks like that:
Date, Time, Temp1,Humidity1,Temp2,Humidity2,Temp3,Humidity3
2023.12.08,20:07:02, 15.0,83.0,16.0,76.0,18.0,70.0
2032.31.34,32:30:00, 16.0,82.0,16.0,76.0,17.0,74.0
2031.33.34,33:30:00, 16.0,82.0,17.0,77.0,18.0,74.0
2033.33.34,33:30:00, 15.0,88.0,16.0,83.0,17.0,77.0
2033.33.34,33:30:00, 16.0,86.0,17.0,81.0,18.0,77.0
2033.33.34,33:30:00, 16.0,86.0,17.0,81.0,18.0,77.0
2033.33.34,33:30:00, 17.0,86.0,18.0,81.0,18.0,77.0
If I set the time correctly, it will be written correctly once and then incorrectly...
Can anyone help me to find the problem?
Here is my code:
/*
Program to demonstrate Data Logging/Visualisation using Arduino
###Connection with SD card module###
Vcc->5V
Gnd->Gnd
MISO->pin 12
MOSI->pin 11
SCK-> pin 13
CS-> pin 4
###Connection with DS3231###
Vcc->5V
Gns->Gnd
SCL->pin A5
SDA-> pin A4
###Connection with DT11###
Vcc->5V
Gnd->Gnd
Out-> pin 7
*/
#include <RTClib.h> //Library for RTC module (Download from Link in article)
#include <Wire.h>
#include <SPI.h> //Library for SPI communication (Pre-Loaded into Arduino)
#include <SD.h> //Library for SD card (Pre-Loaded into Arduino)
#include <dht.h> //Library for dht11 Temperature and Humidity sensor (Download from Link in article)
#define DHT11_PIN1 7 //Sensor output pin is connected to pin 7
#define DHT11_PIN2 8
#define DHT11_PIN3 6
dht DHT1; //Sensor object named as DHT
dht DHT2;
dht DHT3;
RTC_DS3231 rtc;
const int chipSelect = 4; //SD card CS pin connected to pin 4 of Arduino
void setup() {
Serial.begin(9600);
Initialize_SDcard();
rtc.begin();
}
void loop() {
Write_SDcard();
delay(10800000); //Wait for 5 seconds before writing the next data
}
void Write_SDcard() {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
int chk = DHT1.read11(DHT11_PIN1);
DHT2.read11(DHT11_PIN2);
DHT3.read11(DHT11_PIN3);
float t = DHT1.temperature;
float h = DHT1.humidity;
float t1 = DHT2.temperature;
float h1 = DHT2.humidity;
float t2 = DHT3.temperature;
float h2 = DHT3.humidity;
Serial.println(t, 1);
Serial.println(h, 1);
Serial.println(t1, 1);
Serial.println(h1, 1);
Serial.println(t2, 1);
Serial.println(h2, 1);
DateTime now = rtc.now();
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
char bufer[100];
sprintf(bufer, "%02u.%02u.%02u,%02u:%02u:%02u,%0u.%0u,%0u.%0u,%0u.%0u,%0u.%0u,%0u.%0u,%0u.%0u\r\n",
// YY MM DD hh mm ss t1 t1 h1 h1 t2 t2 h2 h2
now.year(),
now.month(),
now.day(),
now.hour(),
now.minute(),
now.second(),
uint16_t(floor(t)), uint16_t(t * 10) % 10,
uint16_t(floor(h)), uint16_t(h * 10) % 10,
uint16_t(floor(t1)), uint16_t(t * 10) % 10,
uint16_t(floor(h1)), uint16_t(h * 10) % 10,
uint16_t(floor(t2)), uint16_t(t * 10) % 10,
uint16_t(floor(h2)), uint16_t(h * 10) % 10
);
dataFile.print(bufer);
dataFile.close(); //Close the file
}
else
Serial.println("OOPS!! SD card writing failed");
}
void Initialize_SDcard() {
// 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:
return;
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("Date,Time,Temperatur Ecke,Feuchtigkeit Ecke,Temperatur Schrank,Feuchtigkeit Schrank,Temperatur Bett,Feuchtigkeit Bett"); //Write the first row of the excel file
dataFile.close();
}
}