DS3231 breakout weird behaviour

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

DS3231 breakout weird behaviour

Post by blnkjns »

I run the DS3231 breakout with the default RTC Arduino library, as it allows easy change of code between various RTC modules.
I ran into some weird issues though:
- If I update the date to test displaying weekdays, it gives the value 6 for both saturdays and sundays
- If I update the date to a day prior to the current one, the date change is not implemented. I can only set a day after the current day. If I need to set it to an earlier moment, I can remove the battery and the power via de Arduino Leonardo for like 10 seconds, then it goes back to 0. Adding rtc.stopClock(); before setting it does not help.
This is the code I use, I stripped away a lot of lines from the samples with the library.

Code: Select all

#include <Wire.h>
#include <I2C_RTC.h>

static DS3231 rtc;

void setup()
{
    Serial.begin(9600);
    while (!Serial);
    rtc.begin();
    rtc.setHourMode(CLOCK_H24);  
    rtc.setDateTime(__DATE__, __TIME__);
    rtc.updateWeek();
    rtc.startClock(); //Start the Clock;
}

void loop()
{
  Serial.print(rtc.getWeek());
  Serial.print(" ");
  Serial.print(rtc.getDay());
  Serial.print(" ");
  Serial.print(rtc.getMonth());
  Serial.print(" ");
  Serial.print(rtc.getYear());
  Serial.print(" ");
  Serial.print(rtc.getHours());
  Serial.print(":");
  Serial.print(rtc.getMinutes());
  Serial.print(":");
  Serial.println(rtc.getSeconds());
  delay(1000);
}

User avatar
barshatriplee
 
Posts: 200
Joined: Wed Mar 22, 2023 10:11 am

Re: DS3231 breakout weird behaviour

Post by barshatriplee »

The DS3231 RTC module returns the weekday value in the range 1-7, where 1 represents Sunday and 7 represents Saturday. In your code, you are using the getWeek() function to retrieve the weekday value, which returns the value in the range 0-6, where 0 represents Sunday and 6 represents Saturday. To fix this issue, you can add 1 to the value returned by the getWeek() function to get the correct weekday value.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”