Interfacing SD card and RTC to Wemos D1 mini

I have an interface problem that i hope someone can solve.
I am using a Wemos mini D1 and I wish to connect up a micro sd card and an RTC but the two seem to be incompatible. The RTC is a DS3231 and I have used the library by Makuna which is recommended and supposed to be compatible with the WEmos D1 mini.
Individually they both work fine, but not together. I have attached a code that first initialises the RTC and then after 10 seconds initialises the SD card.
to start with the date and time are correct but a soon as the SD card is opened the date is rubbish.
Any help would be appreciated. Thanks

#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
#include <SPI.h>
RTC_DS3231 rtc;
int sec = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void openSD() {
Serial.println();
Serial.println("Open SD card");
if (!SD.begin(4)) {
Serial.println("Open SD card failed");
return;
}
Serial.println("SD card open");
}

void setup () {
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
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));
}

}

void loop () {
DateTime now = rtc.now();

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

sec++;
Serial.println(sec);
if (sec == 10)
openSD();

Serial.println();
delay(1000);
}

Your post was MOVED to its current location as it is more suitable.

alternative A) use TimeLib to keep time while running. only initialize it from the RTC at power-up or once a day (esp8266 has only software I2C)

alternative B) esp8266 has built in RTC which can be synchronized from NTP service so you don't need external RTC.

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags).

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