RTC DS3231 not working right

Im using a DS3231 and sometimes when i hit the reset button on the MCU it will stop communicating with the RTC module. the only way to make it start working again is to power cycle the RTC module or short the SDA SCL pins. why is this happening?

Can you please provide your code; the easiest is probably to use edit → copy for forum in the IDE and next paste it here. It will take care of the formatting and it will be easy for us to copy.

Can you please provide details of the board that you’re using.
And provide a schematic diagram (including all power connections); photo of handrawn one is fine.

Im using ESP8266 as MCU, i found that if i manually digitalWrite the SDA and SCL pins low in setup then 75 percent of the time theres no problem. There are pullup resistor onboard the DS3231. when the RTC module fails the epoch always == 94668480. so i put a check in the setup. if epoch==946684800 then ESP.reboot(). this is not ideal but it works. if i remove the digitalWrites then it will just endlessly reboot.

#include <Wire.h> //I2C library
#include <RtcDS3231.h> //RTC library

unsigned long now = 0;
unsigned long now2 = 0;

RtcDS3231<TwoWire> rtc(Wire);

void setup() {
  Serial.begin(115200);  //Starts serial connection
  Serial.println("rebooting");
  rtc.Begin();     //Starts I2C
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
  RtcDateTime currentTime = RtcDateTime(16, 05, 18, 21, 20, 0); //define date and time object
  rtc.SetDateTime(currentTime); //configure the RTC with object
  RtcDateTime epoch32 = rtc.GetDateTime();
  unsigned long  epochTime = epoch32.Epoch32Time();
  if (epochTime == 946684800) {
    ESP.restart();
  }
}

void handleGetTime() {
  RtcDateTime currentTime = rtc.GetDateTime();    //get the time from the RTC
  char str[20];   //declare a string as an array of chars
  sprintf(str, "%d/%d/%d %d:%d:%d",     //%d allows to print an integer to the string
          currentTime.Year(),   //get year method
          currentTime.Month(),  //get month method
          currentTime.Day(),    //get day method
          currentTime.Hour(),   //get hour method
          currentTime.Minute(), //get minute method
          currentTime.Second()  //get second method
         );
  Serial.println(str); //print the string to the serial port
}
void loop() {

  RtcDateTime epoch32 = rtc.GetDateTime();
  unsigned long  epochTime = epoch32.Epoch32Time();
  if (epochTime == 946684800) {
    ESP.restart();
  }
  if (epochTime - now2 > 1) {
    Serial.println("hello");
    now2 = epochTime;
  }
  if (millis() - now > 1000) {
    Serial.println(epochTime);
    //   handleGetTime();
    now = millis();
  }
}

Not sure if it will work on a 3.3 volt output, but the DS3231 can be powered by an output pin, making it easy to power cycle the RTC.

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