DS3231 RTC stuck

Hallo everyone :slight_smile:

I can’t get my DS3231SN RTC to work. It just seems as if someone pressed the pause button ???
The RTC Module is directly connected via jumper wire to my UNO, no other hardware connected, Backup Battery provides 4,4V, which should be more than enough.

VCC------5V
GND-----GND
SCL------A5
SDA-----A4

I have tried different libraries, none of them could solve my problem.
This is the code I used to set the time on it:

#include <DS3231.h>
DS3231  rtc(SDA, SCL);

void setup()
{
  Serial.begin(115200);
  rtc.begin();
  
  rtc.setDOW(WEDNESDAY);
  rtc.setTime(12, 0, 0);
  rtc.setDate(1, 1, 2014);
}

void loop()
{
}

And this is my code for reading from the RTC:

#include <DS3231.h>
DS3231  rtc(SDA, SCL);

void setup()
{
  Serial.begin(115200);
  rtc.begin();
}

void loop()
{
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
  Serial.println(rtc.getTimeStr());
  delay (1000);
}

And here is the result:

Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00

And so on…

BUT: After some minutes, the time changed a little bit and then stopped again:

Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:00
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:01
Wednesday 01.01.2014 -- 12:00:02
Wednesday 01.01.2014 -- 12:00:02
Wednesday 01.01.2014 -- 12:00:02
Wednesday 01.01.2014 -- 12:00:02
Wednesday 01.01.2014 -- 12:00:02
Wednesday 01.01.2014 -- 12:00:02
Wednesday 01.01.2014 -- 12:00:03
Wednesday 01.01.2014 -- 12:00:03
Wednesday 01.01.2014 -- 12:00:03
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04
Wednesday 01.01.2014 -- 12:00:04

Is it even possible to stop the RTC? Is my code or wiring wrong, or is the RTC just defective?

lg, couka

I have a DS3231 RTC as well, but the library is chaotic. i use these libraries and can help you with the code for them if you want to download them and try them out

#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Time.h> //http://www.arduino.cc/playground/Code/Time

You might also find some sanity here

Note that there are two programmes, one to set the time, and the other to read it.

You should have 4.7k pullup resistors on SDA and SCL.

If that doesn't fix it, leave them on and run Nick Gammon's I2C Scanner which is at the bottom of this web page:

It should find the DS3231 address. If not, you've got a wiring problem somewhere.

Pete

el_supremo Is was 100% confident that there were proper internal pull-ups on A4 and A5. :o Without the rest of the project it seems to work now. I forgot to mention, that the temperature reading didn't work either (0°C), but now it does. Thanks a lot!

I understand why missing/weak Pull-Ups lead to communication errors, but reading the same wrong value over and over again? :confused:

I hope this will fix the problem permanently :slight_smile:

Thanks to Nick_Pyner and Thomas499 as well, I tried these libs, with similiar results.

lg, couka

No… still not too much luck :confused:
I’m using JChristensens DS3232RTC Library and the included SetSerial-Sketch:

05Nov2015 14:53:53 
05Nov2015 14:53:54 
05Nov2015 14:53:55   0.00 C  32.00 F
05Nov2015 14:53:56 
RTC set to: 05Nov2015 15:01:15 <<-----Setting the correct time (15:01:15)
05Nov2015 15:01:15   0.00 C  32.00 F
05Nov2015 15:01:16                  <<------The arduino continues counting
05Nov2015 15:01:17 
05Nov2015 15:01:18 
05Nov2015 15:01:19 
05Nov2015 15:01:20   0.00 C  32.00 F
05Nov2015 15:01:21 
05Nov2015 15:01:22 <<----------------------------HERE I PRESSED RESET
RTC Sync
05Nov2015 15:01:15   0.00 C  32.00 F <<--------after syncing, it's 15:01:15 again
05Nov2015 15:01:16 
05Nov2015 15:01:17 
05Nov2015 15:01:18 
05Nov2015 15:01:19 
05Nov2015 15:01:20   0.00 C  32.00 F
05Nov2015 15:01:21 
05Nov2015 15:01:22 
05Nov2015 15:01:23 
05Nov2015 15:01:24 
05Nov2015 15:01:25   0.00 C  32.00 F

The RTC remains giving me the last set time. Even more weird: At first it worked, after pressing reset or removing power syncing to the RTC always returned the correct time and temperature reading. Then after some minutes, without touching anything, 0.00°C again and after pressing reset, the time was resetted as well.

Then I resoldered some wires, it worked again, after some minutes again 15:01:15, resoldered some other connections, RTC resets to 1.1.2000 00:00:00, sticking at 0.00°C again…

I have no idea what’s going on :confused:

lg, couka

do you have any way to connect the arduino to internet?

here
take this out

setTime(t); // problem here

and replace with

setSyncProvider(RTC.get);// use this instead, basically it has to get the time from the RTC this way.

better yet, use the foolproof way that makes it easier to debug

tm.Minute = Serial.parseInt();
            tm.Second = Serial.parseInt();
            t = makeTime(tm);
            RTC.set(t);        //use the time_t value to ensure correct weekday is set
            //setTime(t); // problem here, it doesnt matter if Rtc.set(t) works or not, settime(t) sets the time using the arduino millis and time library.        
            if (RTC.get()!=0)
          { setSyncProvider(RTC.get);
            Serial.println(F("RTC has set the system time"));
          } else  { setTime(t);
                    Serial.println(F("Unable to save time to RTC"));
                  }

Thomas499:
do you have any way to connect the arduino to internet?

I have an ethernet shield for my UNO, but the RTC is meant to speak to a Pro Mini.
Why?

Your code outputs:

01Jan2015 01:01:33 
01Jan2015 01:01:34 
01Jan2015 01:01:35   0.00 C  32.00 F
01Jan2015 01:01:36 
RTC has set the system time
RTC set to: 05Nov2015 15:52:30 
05Nov2015 15:52:30   0.00 C  32.00 F
05Nov2015 15:52:31 
05Nov2015 15:52:32 
05Nov2015 15:52:33 
05Nov2015 15:52:34 
05Nov2015 15:52:35   0.00 C  32.00 F
05Nov2015 15:52:36 
05Nov2015 15:52:37

Btw. this is how the full sketch looks like now, since i may have made a mistake dropping in your portion:

/*----------------------------------------------------------------------*
 * Display the date and time from a DS3231 or DS3232 RTC every second.  *
 * Display the temperature once per minute. (The DS3231 does a          *
 * temperature conversion once every 64 seconds. This is also the       *
 * default for the DS3232.)                                             *
 *                                                                      *
 * Set the date and time by entering the following on the Arduino       *
 * serial monitor:                                                      *
 *    year,month,day,hour,minute,second,                                *
 *                                                                      *
 * Where                                                                *
 *    year can be two or four digits,                                   *
 *    month is 1-12,                                                    *
 *    day is 1-31,                                                      *
 *    hour is 0-23, and                                                 *
 *    minute and second are 0-59.                                       *
 *                                                                      *
 * Entering the final comma delimiter (after "second") will avoid a     *
 * one-second timeout and will allow the RTC to be set more accurately. *
 *                                                                      *
 * No validity checking is done, invalid values or incomplete syntax    *
 * in the input will result in an incorrect RTC setting.                *
 *                                                                      *
 * Jack Christensen 08Aug2013                                           *
 *                                                                      *
 * Tested with Arduino 1.0.5, Arduino Uno, DS3231/Chronodot, DS3232.    *
 *                                                                      *
 * This work is licensed under the Creative Commons Attribution-        *
 * ShareAlike 3.0 Unported License. To view a copy of this license,     *
 * visit http://creativecommons.org/licenses/by-sa/3.0/ or send a       *
 * letter to Creative Commons, 171 Second Street, Suite 300,            *
 * San Francisco, California, 94105, USA.                               *
 *----------------------------------------------------------------------*/

#include <DS3232RTC.h>        //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h>        //http://arduiniana.org/libraries/streaming/
#include <Time.h>             //http://playground.arduino.cc/Code/Time
#include <Wire.h>             //http://arduino.cc/en/Reference/Wire

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

  //setSyncProvider() causes the Time library to synchronize with the
  //external RTC by calling RTC.get() every five minutes by default.
  setSyncProvider(RTC.get);
  Serial << F("RTC Sync");
  if (timeStatus() != timeSet) Serial << F(" FAIL!");
  Serial << endl;
}

void loop(void)
{
  static time_t tLast;
  time_t t;
  tmElements_t tm;

  //check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
  if (Serial.available() >= 12) {
    //note that the tmElements_t Year member is an offset from 1970,
    //but the RTC wants the last two digits of the calendar year.
    //use the convenience macros from Time.h to do the conversions.
    int y = Serial.parseInt();
    if (y >= 100 && y < 1000)
      Serial << F("Error: Year must be two digits or four digits!") << endl;
    else {
      if (y >= 1000)
        tm.Year = CalendarYrToTm(y);
      else    //(y < 100)
        tm.Year = y2kYearToTm(y);
      tm.Month = Serial.parseInt();
      tm.Day = Serial.parseInt();
      tm.Hour = Serial.parseInt();
      tm.Minute = Serial.parseInt();
      tm.Second = Serial.parseInt();
      t = makeTime(tm);
      RTC.set(t);        //use the time_t value to ensure correct weekday is set
      //setTime(t); // problem here, it doesnt matter if Rtc.set(t) works or not, settime(t) sets the time using the arduino millis and time library.
      if (RTC.get() != 0)
      { setSyncProvider(RTC.get);
        Serial.println(F("RTC has set the system time"));
      } else  {
        setTime(t);
        Serial.println(F("Unable to save time to RTC"));
      }
      Serial << F("RTC set to: ");
      printDateTime(t);
      Serial << endl;
      //dump any extraneous input
      while (Serial.available() > 0) Serial.read();
    }
  }

  t = now();
  if (t != tLast) {
    tLast = t;
    printDateTime(t);
    if (second(t) % 5 == 0) {
      float c = RTC.temperature() / 4.;
      float f = c * 9. / 5. + 32.;
      Serial << F("  ") << c << F(" C  ") << f << F(" F");
    }
    Serial << endl;
  }
}

//print date and time to Serial
void printDateTime(time_t t)
{
  printDate(t);
  Serial << ' ';
  printTime(t);
}

//print time to Serial
void printTime(time_t t)
{
  printI00(hour(t), ':');
  printI00(minute(t), ':');
  printI00(second(t), ' ');
}

//print date to Serial
void printDate(time_t t)
{
  printI00(day(t), 0);
  Serial << monthShortStr(month(t)) << _DEC(year(t));
}

//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
  if (val < 10) Serial << '0';
  Serial << _DEC(val);
  if (delim > 0) Serial << delim;
  return;
}

lg, couka

i’m getting these errors when i load your sketch

sketch_nov05a.ino: In function 'void setup()':
sketch_nov05a:47: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
sketch_nov05a:48: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
sketch_nov05a:49: error: 'endl' was not declared in this scope
sketch_nov05a.ino: In function 'void loop()':
sketch_nov05a:65: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
sketch_nov05a:65: error: 'endl' was not declared in this scope
sketch_nov05a:86: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
sketch_nov05a:88: error: 'endl' was not declared in this scope
sketch_nov05a:101: error: no match for 'operator<<' in 'Serial << (const __FlashStringHelper*)({...})'
sketch_nov05a:103: error: 'endl' was not declared in this scope
sketch_nov05a.ino: In function 'void printDate(time_t)':
sketch_nov05a:127: error: no match for 'operator<<' in 'Serial << monthShortStr(((uint8_t)month(t)))'
sketch_nov05a:127: error: '_DEC' was not declared in this scope
sketch_nov05a.ino: In function 'void printI00(int, char)':
sketch_nov05a:136: error: '_DEC' was not declared in this scope

to be honest, i don’t know how to read

Serial << F(" “) << c << F(” C “) << f << F(” F");

Krupski tried to explain the maddness to me on post #13 but the whole concept looks sloppy to me.

Would you be opposed to pulling the time from the NPT database to sync that to the rtc or using the computers time to update the rtc instead? I can help with that

Post 4 and 10 will be helpful.

The RTC remains giving me the last set time. Even more weird: At first it worked, after pressing reset or removing power syncing to the RTC always returned the correct time and temperature reading. Then after some minutes, without touching anything, 0.00°C again and after pressing reset, the time was resetted as well.

Then I resoldered some wires, it worked again, after some minutes again 15:01:15, resoldered some other connections, RTC resets to 1.1.2000 00:00:00, sticking at 0.00°C again...

I have no idea what's going on :confused:

The Christensen library is solid. The unmodified SetSerial example will work. Do not vary from the library example code as you attempt to trouble shoot this.

You must have some sort of problem with your wiring or the rtc module. What module do you have, and how is it conneted.

cattledog:
The Christensen library is solid. The unmodified SetSerial example will work. Do not vary from the library example code as you attempt to trouble shoot this.

You must have some sort of problem with your wiring or the rtc module. What module do you have, and how is it conneted.

This is the RTC I bought: http://www.amazon.de/dp/B00P9278HM

I tried different Arduinos, wires, power supplies, librarys, backup batteries and even versions of the Arduino software and PCs/Laptops.
Since it's the only remaining constant, I guess the RTC is just broken for unknown reasons. It'll take about 3-4 weeks to get another one and verify that. For now, I'll hardcode the time into the sketch, which isn't great but functional.

Thanks to everyone spending their time helping, you guys are great :slight_smile:

Here are some pictures of the full project btw. It's a solar-tracking model for school, designed for measuring the advantage over a stationary system.

Greets from germany :slight_smile:
lg, couka