Search code examples
arduinoi2carduino-idereal-time-clock

I can't get DS3231 RTC to work


I have a DS3231 RTC module and I am trying to read time off of it using my Arduino UNO through I2C. I'm using the sample code provided with the library but it doesn't seem to work.

The only thing I get out of the serial monitor is this:

20165-85-165 25:165:165 Temperature=254

I was getting the same thing with another RTC module as well and my guess (which probably isn't true) is that they might have overflown though there doesn't seem to be a reset pin.

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
    // Start the I2C interface
    Wire.begin();
  #define oneTime
  #ifdef oneTime
    Clock.setSecond(50);//Set the second 
    Clock.setMinute(59);//Set the minute 
    Clock.setHour(11);  //Set the hour 
    Clock.setDoW(5);    //Set the day of the week
    Clock.setDate(31);  //Set the date of the month
    Clock.setMonth(5);  //Set the month of the year
    Clock.setYear(13);  //Set the year (Last two digits of the year)
  #endif
        // Start the serial interface
    Serial.begin(115200);

}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();

  temperature=Clock.getTemperature();

  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);}

Solution

  • For anyone out there who has this issue, just try to change the battery.

    I bought a new DS3231 module and it wasn't working from day zero. I get weird data and zero for the temperture. When I managed to correctly read the date It wasn't kept. I tried all libraries I could find in vain.

    I changed the battery and everything is now working.