Help with DS3231 RTC

I'm trying to get a DS3231 RTC module working with an Arduino Nano but I get strange numbers back for the date time, e.g. "21165 85 165 165 8 16 32 24h T=13.75 O+"

I am just testing with various example sketches and I have tried several DS3231 libraries and associated examples, they all give the same result. Please can someone confirm that the examples work? I note that some don't even compile without modification.

I assume it is either because:

  1. my DS3231 is broken
  2. the data being sent to set the clock is being mangled
  3. or the returned values are getting screwed up

If the examples work for other people then I guess it is 1.

TIA

Choose a library and stick with it

Post an example here that illustrates the problem

Well I would if I found one that worked :slightly_smiling_face:

Here's one example

/*
DS3231_test.pde
Eric Ayars
4/11

Test/demo of read routines for a DS3231 RTC.

Turn on the serial monitor after loading this to check if things are
working as they should.

*/

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

DS3231 dsclock;
bool century = false;
bool h12Flag;
bool pmFlag;
byte alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits;
bool alarmDy, alarmH12Flag, alarmPmFlag;

void setup() {
	// Start the I2C interface
	Wire.begin();
 
	// Start the serial interface
	Serial.begin(57600);
}

void loop() {
	// send what's going on to the serial monitor.
	
	// Start with the year
	Serial.print("2");
	if (century) {			// Won't need this for 89 years.
		Serial.print("1");
	} else {
		Serial.print("0");
	}
	Serial.print(dsclock.getYear(), DEC);
	Serial.print(' ');
	
	// then the month
	Serial.print(dsclock.getMonth(century), DEC);
	Serial.print(" ");
  
	// then the date
	Serial.print(dsclock.getDate(), DEC);
	Serial.print(" ");
  
	// and the day of the week
	Serial.print(dsclock.getDoW(), DEC);
	Serial.print(" ");
  
	// Finally the hour, minute, and second
	Serial.print(dsclock.getHour(h12Flag, pmFlag), DEC);
	Serial.print(" ");
	Serial.print(dsclock.getMinute(), DEC);
	Serial.print(" ");
	Serial.print(dsclock.getSecond(), DEC);
 
	// Add AM/PM indicator
	if (h12Flag) {
		if (pmFlag) {
			Serial.print(" PM ");
		} else {
			Serial.print(" AM ");
		}
	} else {
		Serial.print(" 24h ");
	}
 
	// Display the temperature
	Serial.print("T=");
	Serial.print(dsclock.getTemperature(), 2);
  
	// Tell whether the time is (likely to be) valid
	if (dsclock.oscillatorCheck()) {
		Serial.print(" O+");
	} else {
		Serial.print(" O-");
	}
 
	// Indicate whether an alarm went off
	if (dsclock.checkIfAlarm(1)) {
		Serial.print(" A1!");
	}
 
	if (dsclock.checkIfAlarm(2)) {
		Serial.print(" A2!");
	}
 
	// New line on display
	Serial.println();
	
	// Display Alarm 1 information
	Serial.print("Alarm 1: ");
	dsclock.getA1Time(alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits, alarmDy, alarmH12Flag, alarmPmFlag);
	Serial.print(alarmDay, DEC);
	if (alarmDy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(' ');
	Serial.print(alarmHour, DEC);
	Serial.print(' ');
	Serial.print(alarmMinute, DEC);
	Serial.print(' ');
	Serial.print(alarmSecond, DEC);
	Serial.print(' ');
	if (alarmH12Flag) {
		if (alarmPmFlag) {
			Serial.print("pm ");
		} else {
			Serial.print("am ");
		}
	}
	if (dsclock.checkAlarmEnabled(1)) {
		Serial.print("enabled");
	}
	Serial.println();
 
	// Display Alarm 2 information
	Serial.print("Alarm 2: ");
	dsclock.getA2Time(alarmDay, alarmHour, alarmMinute, alarmBits, alarmDy, alarmH12Flag, alarmPmFlag);
	Serial.print(alarmDay, DEC);
	if (alarmDy) {
		Serial.print(" DoW");
	} else {
		Serial.print(" Date");
	}
	Serial.print(" ");
	Serial.print(alarmHour, DEC);
	Serial.print(" ");
	Serial.print(alarmMinute, DEC);
	Serial.print(" ");
	if (alarmH12Flag) {
		if (alarmPmFlag) {
			Serial.print("pm");
		} else {
			Serial.print("am");
		}
	}
	if (dsclock.checkAlarmEnabled(2)) {
		Serial.print("enabled");
	}
 
	// display alarm bits
  Serial.println();
	Serial.print("Alarm bits: ");
	Serial.println(alarmBits, BIN);

	Serial.println();
	delay(1000);
}

Exactly which library are you using and how did you install it ?

My hunch is that it isn't the library and more likely a duff DS3231 which is why I was asking if any of the library examples work for other people. At the moment I have Andrew Wikert's library - GitHub - NorthernWidget/DS3231: Communicates between Arduino-programmed AVR and Maxim DS3231 RTC: splice of Ayars' (http://hacks.ayars.org/2011/04/ds3231-real-time-clock.html) and Jeelabs/Ladyada's (https://github.com/adafruit/RTClib) libraries installed through the IDE library manager.

Pardon the silly question, but have you set the date and time on the clock ?

using a ESP8266 NodeMCU just ran File>Examples>RTClib>ds3231

20:38:03.002 ->  since midnight 1/1/1970 = 1670359007s = 19332d
20:38:03.035 ->  now + 7d + 12h + 30m + 6s: 2022/12/14 9:6:53
20:38:03.035 -> Temperature: 23.50 C
20:38:03.035 -> 
20:38:06.016 -> 2022/12/6 (Tuesday) 20:36:50
20:38:06.016 ->  since midnight 1/1/1970 = 1670359010s = 19332d
20:38:06.016 ->  now + 7d + 12h + 30m + 6s: 2022/12/14 9:6:56
20:38:06.049 -> Temperature: 23.50 C
20:38:06.049 -> 
20:38:09.037 -> 2022/12/6 (Tuesday) 20:36:53
20:38:09.037 ->  since midnight 1/1/1970 = 1670359013s = 19332d
20:38:09.037 ->  now + 7d + 12h + 30m + 6s: 2022/12/14 9:6:59
20:38:09.037 -> Temperature: 23.50 C

does the I2C scanner work? e.g.

20:53:02.390 -> Scanning (SDA : SCL) - GPIO4 : GPIO5 - I2C device found at address 0x57  !
20:53:02.390 -> I2C device found at address 0x68  !

1 Like

I have tried, again via various means, I can get it to change but it is still of the form "85 165 165" for YY MM DD

I2C Scanner gives
Scanning...
I2C device found at address 0x57 !
I2C device found at address 0x68 !
done

File>Examples>RTClib>ds3231 says "Couldn't find RTC" however the example posted above seems to find it and read it just with the wrong values but those values do increment by one every second

just tried another device labled TinyRTC

21:08:03.015 ->  since midnight 1/1/1970 = 1670360859s = 19332d
21:08:03.048 ->  now + 7d + 12h + 30m + 6s: 2022/12/14 9:37:45
21:08:03.048 -> Temperature: 45.00 C
21:08:03.048 -> 
21:08:06.020 -> 2022/12/6 (Tuesday) 21:7:42
21:08:06.020 ->  since midnight 1/1/1970 = 1670360862s = 19332d
21:08:06.054 ->  now + 7d + 12h + 30m + 6s: 2022/12/14 9:37:48
21:08:06.054 -> Temperature: 45.00 C
21:08:06.054 -> 

temperature is incorrect otherwise OK?

1 Like

Thanks all for the replies. Looks like it was a bad ground connection :man_facepalming:

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