[SOLVED]Arduino Mega SD and RTC Module code error

#include<virtuabotixRTC.h>
#include<SPI.h> //53-CS, 52-SCK, 51-MOSI, 50-MISO
#include<SD.h>

virtuabotixRTC RealTimeClock(6,7,8); // SCLK, DAT, RST Pins


unsigned long event1 = 0;


void SD_init();
void SD_RTCWrite(String RTC);

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //Setting 9600 as baud rate for Main Serial Communication
  SD_init();
  //comment this line after initial run
  RealTimeClock.setDS1302Time(00,12, 01, 01, 16, 10, 2023); //[Seconds, Minutes, Hours, Day of the Week, Day of the Month,
}

void loop() {
  // put your main code here, to run repeatedly:
  RealTimeClock.updateTime();
  unsigned long Mega_Runtime = millis();
  if ( Mega_Runtime - event1 == 1000 ){
    String datetime = "";                                                                            //| 
    datetime += RealTimeClock.dayofmonth;
    datetime += "/";
    datetime += RealTimeClock.month;
    datetime += "/";
    datetime += RealTimeClock.year;
    datetime += "  ";
    datetime += RealTimeClock.hours;
    datetime += ":";
    datetime += RealTimeClock.minutes;
    datetime += ":";
    datetime += RealTimeClock.seconds;
    Serial.println(datetime);
    SD_RTCWrite(datetime);
    Mega_Runtime += 1000;
  }

}

/*Function Definition*/
void SD_init(){
  if( !SD.begin(53)){
    Serial.println("Error on SD Card");
    while(1); // system halt.restart/ reset needed
  }
  else
    Serial.println("SD Card initialized.");
}

void SD_RTCWrite(String RTC){
   if (!SD.begin(53)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if ( dataFile ){
    dataFile.println(RTC);
    dataFile.close();
  }
  else{
    Serial.println("Error Opening Log File.");
  }
}

Hello every one! can you give me solution about what is wrong with my code.. i run it and the output is "SD Card initialized" nothing more and nothing less... it seems like it freezes after running the SD_init(); fxn and does not proceed with the loop.

btw im using the millis fxn to multitaks certain fxn in the future

What happens if you comment out the call to SD_init()?

Some SD card modules will not play well with other devices on the SPI bus. The problem is with the way the level shifter on the SD module is wired. The ones that do not work well have the MISO signal running through the level shifter. That causes the MISO signal to not be released so that the other devices can control it. The modules made by Adafruit (and some others) have the MISO signal going from the SD card straight to the MISO, bypassing the level shifter. Look at the schematic for the Adafruit module to see what to look for. DO (data out) is the MISO signal.

One solution, if the above is the case, is to use the hardware SPI for one device and a software SPI for the other (if the library in use will allow soft SPI).

Or modify the SD module. There are articles on the net on how to do that.

Hi everyone! i already fixed the code. my mistake is the condition on the if statement on loop where the difference of MegaRuntime and event1 must be GREATER THAN OR EQUAL to 1000ms and not EQUAL to 1000ms

Here is the updated code:

#include<virtuabotixRTC.h>
#include<SPI.h> //53-CS, 52-SCK, 51-MOSI, 50-MISO
#include<SD.h>

virtuabotixRTC RealTimeClock(6,7,8); // SCLK, DAT, RST Pins


unsigned long event1 = 0, interval = 1000;


void SD_init();
void SD_RTCWrite(String RTC);

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //Setting 9600 as baud rate for Main Serial Communication
  SD_init();
  //comment this line after initial run
  RealTimeClock.setDS1302Time(00,12, 01, 01, 16, 10, 2023); //[Seconds, Minutes, Hours, Day of the Week, Day of the Month,
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long MegaRuntime = millis();
  RealTimeClock.updateTime();
  String datetime = "";                                                                            //| 
  if(MegaRuntime - event1 >= 1000){
    datetime += RealTimeClock.dayofmonth;
    datetime += "/";
    datetime += RealTimeClock.month;
    datetime += "/";
    datetime += RealTimeClock.year;
    datetime += "  ";
    datetime += RealTimeClock.hours;
    datetime += ":";
    datetime += RealTimeClock.minutes;
    datetime += ":";
    datetime += RealTimeClock.seconds;
    Serial.println(datetime);
    SD_RTCWrite(datetime);
    event1 = MegaRuntime;
  }
}

/*Function Definition*/
void SD_init(){
  if( !SD.begin(53)){
    Serial.println("Error on SD Card");
    while(1); // system halt.restart/ reset needed
  }
  else
    Serial.println("SD Card initialized.");
}

void SD_RTCWrite(String RTC){
   if (!SD.begin(53)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if ( dataFile ){
    dataFile.println(RTC);
    dataFile.close();
  }
  else{
    Serial.println("Error Opening Log File.");
  }
}

If your question has been answered to your satisfaction, please mark the thread as solved so that other members that wish to help will not waste their time opening and reading the thread only to find that you no longer require assistance.

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