4

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);}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Ege F
  • 107
  • 1
  • 1
  • 9

5 Answers5

4

Having the same problem here, it turns out that the DS3231 communication can get unsynchronised with the microcontroller due to different events. Seems like the cryptic output is coming from that, at least here debugging with a DS3231 connected to an ESP8266 Arduino.

Following the datasheet specification:

The I2C interface is accessible whenever either VCC or VBAT is at a valid level. If a microcontroller connected to the DS3231 resets because of a loss of VCC or other event, it is possible that the microcontroller and DS3231 I2C communications could become unsynchronized, e.g., the microcontroller resets while reading data from the DS3231. When the microcontroller resets, the DS3231 I2C interface may be placed into a known state by toggling SCL until SDA is observed to be at a high level. At that point the microcontroller should pull SDA low while SCL is high, generating a START condition.

And inspired on their official application note 3506

Using the ESP8266 and using their I2C implementation. You can get macros functions on how to access the SDA and SCL pins bitwise. Here is one implemented reset function based on the official example for the 8051.

#define SDA_LOW()   (GPES = (1 << SDA))
#define SDA_HIGH()  (GPEC = (1 << SDA)) 
#define SCL_LOW()   (GPES = (1 << SCL))
#define SCL_HIGH()  (GPEC = (1 << SCL))
#define SDA_READ()  ((GPI & (1 << SDA)) != 0)

void resetRTC() {

  pinMode(SDA, INPUT_PULLUP);
  pinMode(SCL, INPUT_PULLUP);
  do {
    SDA_HIGH();
    SCL_HIGH();
    if (SDA_READ()) {
      SDA_LOW();
      SDA_HIGH();
    }
    SCL_LOW();
  } while (SDA_READ() == 0);

}

This is working fine and seems to solve the problem

A more simple solution would be calling Wire.status(), it also seems to work.

Wire.status();

I'm not sure if for all cases. This method is doing checks for status and for one case it calls twi_write_start() which has some similarities with the function resetRTC() above.

In case you want to implement a similar function for that ATMEL Arduino you will need to look into the bitwise implementation to manipulate SDA and SCL on Arduino I2C core.

calmar
  • 1,930
  • 1
  • 14
  • 15
  • 1
    Thank you very much! Your solution of resetting I2C is worked well for me. I had a strange trouble with rtc: if I connect esp8266 to power supply then rtc works fine, however after pushing a reset button rtc.begin() returns false. Then if I make a continuity test with multimeter between SCL and GND I see like there is a short circuit but with high resistance. If I make the same test right after disconnecting rtc (while it was working normal) then the continuity test will be negative. So indeed resetting I2C by pulling up the pins did the job. Cheers – rvaliev Nov 15 '19 at 14:02
  • @pawisoon I guess so, it's about a I2C commands, so it will probably work the same way – calmar Jun 24 '20 at 04:02
1

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.

KLiFF
  • 382
  • 2
  • 18
  • Seems like my problem. I won't be able to try this for a couple months but will try and come back. – Ege F Jul 18 '16 at 05:54
0

What pins are you connecting it to? I had the same issue until I connected RTC's SDA and SCL to Arduino's SDA and SCL respectively. Depending on the model, these are pins 20 and 21 on Mega2560, 19 and 18 on a Micro....

Gabi
  • 1
0

I had same issue, finger trouble, I had reversed SDA and SCL connections to RTC. When corrected RTC worked perfectly. FYI a bad connection to RTC will also result in a similar weird output.

-3

Use : Serial.begin(9600); / another baud rate instead of Serial.begin(115200);