Hallo zusammen,
ich möchte Messdaten von einen Sensor auf einer SD Karte speichern und diese mit einen Zeitstempel versehen.
Arduino nano Clone von Joy-It
Die Zeit soll eine RTC DS1302 (VMA301 velleman) liefern. (RtcDS1302.h Libray)
Abgespeichert wird über ein Micro SD Kartenmodul (Daypower, Pollin) auf eine Micro SD Karte. (Standard SD Libray)
Problem:
Die Bauteile funktionieren, wenn man die Sketche einzeln testet.
Zusammen gesetzt funktioniert aber die RTC nicht mehr und liefert kein Datum/Uhrzeit:
Initializing SD card...initialization done.
13:22:20.169 -> Writing to test.txt...done.
13:22:20.169 -> test.txt:
13:22:20.169 -> testing 1, 2, 3.
13:22:20.216 -> compiled: Oct 11 202313:22:06
13:22:20.250 -> 10/11/2023 13:22:06
13:22:20.297 -> RTC lost confidence in the DateTime!
13:22:20.297 -> RTC is newer than compile time. (this is expected)
13:22:20.344 -> 00/00/2000 00:00:00
13:22:20.391 -> RTC lost confidence in the DateTime!
13:22:30.360 -> 00/00/2000 00:00:00
13:22:30.360 -> RTC lost confidence in the DateTime!
Der Sketch verwendet 17454 Bytes (56%) des Programmspeicherplatzes. Das Maximum sind 30720 Bytes.
Globale Variablen verwenden 1337 Bytes (65%) des dynamischen Speichers, 711 Bytes für lokale Variablen verbleiben. Das Maximum sind 2048 Bytes.
Ich habe auch eine andere Libray (DS1304.h) ausprobiert; Liefert aber auch keine Uhrzeit...
Mein Code:
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 9 (for MKRZero SD: SDCARD_SS_PIN)
*/
#include <SPI.h>
#include <SD.h>
#include <RtcDS1302.h>
File myFile;
ThreeWire myWire(4,13,3); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(9)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
//RTC
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop() {
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
Ich hoffe mir kann jemand helfen oder Tipps geben
Vielen Dank schon mal im Voraus