Need Help (ESP8266, DS3231, and SD Card Module)

Hello, I am stuck with my coding, the DS3231 didn't work if I insert my SD Card Module. Here are my code, also my rtc is late by 2 hours.

include <SD.h>
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
const int chipSelect = 5;
File SdFile;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

rtc.begin(); {
  rtc.adjust(DateTime(2023,9,6,9,0,0));;
  }

while (!Serial)
    Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    while (1)
      ;
  }
  Serial.println("card initialized.");
  SdFile = SD.open("dox.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (SdFile) {
    Serial.print("Writing to test.txt...");
    // close the file:
    SdFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

}

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(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);

  SdFile = SD.open("dox.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (SdFile) {
    SdFile.print(now.year(), DEC);
    SdFile.print("/");
    SdFile.print(now.month(), DEC);
    SdFile.print("/");
    SdFile.print(now.day(), DEC);
    SdFile.print("  ");
    SdFile.print(now.hour(), DEC);
    SdFile.print(":");
    SdFile.print(now.minute(), DEC);
    SdFile.print(":");
    SdFile.print(now.second(), DEC);
    SdFile.println(";");
  }
  delay(1000);
}

Post your project schematic.

Hi @azfarimahindra ,

welcome to the arduino-forum.

For really effective help you should provide more information.

What exact type of microcontroller are you using?
to whcih IO-pins do you have the SD-card connecteded?
Does your SD-Card-reader have pullup-resistors?
did you do a test if SD-card alone is working correctly ?
did you do a test if the rtc-module is working correctly?

best regards Stefan

Not sure what a tight loop of file open attempts with no closes will do, but it is probably not good.

Hello Stefan
I use node mcu ESP 8266 MOD microcontroller
I use D1 as SCL, D2 as SDA, D5 as SCK, D6 Miso, D7 mosi and D8 CS
No, I dont think so it's a standard SD card module
yes. it works
yes, it also works
the main problem is the ds3231 didn't work if I include sd program

I'm sorry i am quite new to this stuff, would you mind explain? Thank you

Yes, but you never close it !

Please post the code that is testing only the DS3231 RTC

Please post the code that is testing only the SD-card

I am sorry I dont have one, I am new so I tend to do it with notes rather than image
For SD I use D5 as SCK, D6 Miso, D7 mosi and D8 CS
For DS I use D1 as SCL, D2 as SDA

Serial.println("done.");
  } else

but I did tho? I am really sorry but I am confused

Yes, for that part I dont use a test... I immediately use the working program and test it if it works and it does... so I am just going to send the code that I use, I hope that's okay, still quite new with this stuff and didn't quite learn how to use the testing code.

DS3231
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  Serial.begin(9600);
  

rtc.begin(); 
rtc.adjust(DateTime(2023,9,20,14,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(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.println();
    delay(3000);
}
#include <SPI.h>
#include <SD.h>

File myFile;

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(4)) {
    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");
  }
}

void loop() {
  // nothing happens after setup
}

I use Sdfile from an example but it's not that much of a difference except for the SPI part which I don't understand what it does. also I use Readwrite from SD examples to do this not the datalogger one, because i don't understand string, i use it once and still don't know what i do with it.

@azfarimahindra

if you want effectice help you have to write more words.

This is not whatsApping for fun. This is serious engineering work even if it is just a hobby.

This says nothing about
what
you have tested. You will have to clearly name the component. SD-Card or RTC.
Trying to be fast by not writing the real and SPECIFIC names just slows everything down because I have to ask back for confirmation.

again
about
what
are you really confused???

Ok does the code that starts with
DS3231

show how time is printed and the time is increasing?
.
.
.
does the code that starts with

#include <SPI.h>
#include <SD.h>

File myFile;

Work? Which means this testcode writes some data into a textfile.
Did you confirm that the data was really stored on the SD-card by

  1. ejecting the SD-Card from the microcontroller-SD-card reader
  2. inserting the SD-Card into a cardreader directly connected to a computer
  3. looking up if the file with name "test.txt" exists on the SD-card
    and
  4. if you open the file "test.txt" that inside the file there is written
testing 1, 2, 3.

??

In the demo-code there is this line of code

 // close the file:
    myFile.close();

And this line is missing in your combined code.
This means you open the file again and again and again in function loop.
And this is what is wrong in your code.
You should open the file and then after writing the time into the file you have to close the file again

best regards Stefan

I am so sorry for my lack of words

This says nothing about
what
you have tested. You will have to clearly name the component . SD-Card or RTC.
Trying to be fast by not writing the real and SPECIFIC names just slows everything down because I have to ask back for confirmation.

Ok does the code that starts with
DS3231

I use example code, I use SD library, and RTClib. Not the actual DS3231 lib because it didn't work after multiple tries of verifying in the Arduino app.

Ok does the code that starts with
DS3231 shows how time is printed and the time is increasing.

Yes so this down here is when I combine the SD module code with RTC

2164/83/12 164:163:0
2000/0/64 164:88:0
2000/4/56 164:88:0
2000/8/48 164:88:0
2000/12/40 164:88:0
2000/10/31 164:88:0
2000/14/18 0:7:0
2000/18/6 164:88:0

And down here is how it works without the SD card module, do not mind the since midnight part because i use the original code in RTClib library.

since midnight 1/1/1970 = 1695218681s = 19620d
now + 7d + 12h + 30m + 6s: 2023/9/28 2:34:47
Temperature: 29.75 C
...
2023/9/20 (Wednesday) 14:4:44
since midnight 1/1/1970 = 1695218684s = 19620d
now + 7d + 12h + 30m + 6s: 2023/9/28 2:34:50
Temperature: 29.75 C

For SD cards it does store, for example, i use the

include <SD.h>
#include "RTClib.h"

code

2000/33/121 164:88:02002000/41/104 164:88:02002000/43/81 164:88:02002000/51/65 164:88:02002000/53/48 164:88:02002000/61/31 164:88:02002000/63/14 164:88:02002000/70/152 164:882000/2000/72/135 164:882000/2000/80/118 164:882000/2000/2/102 164:88:20002000/10/85 164:882000/2000/12/61 164:882000/2000/20/44 164:882000/2000/22/28 164:882000/2000/30/11 164:882000/2000/31/155 164:882000/35/142 164:88:0;
2000/41/115 164:88:02002000/49/92 164:88:0;202000/51/76 164:88:0;202000/59/59 164:88:0;202000/61/43 164:88:0;202000/69/20 164:88:0;202000/71/6 164:88:0;
22000/78/149 164:88:0;
22001/0/135 164:88:0;
2001/8/112 164:88:0;
2001/10/96 164:88:0;
2001/18/79 164:88:0;
2001/20/62 164:88:0;
2001/28/45 164:88:0;
2001/30/23 164:88:0;

Its jumbled up, but i believe it will solved that with a close program.

inside function loop()
add the this line to your code

SdFile.close();

If you don't know what how to do
inside function loop()
add the this line to your code

SdFile.close();

this indicates that you should learn basics about programming.

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