Can anyone tell me why my RTC module (DS3231) is not keeping time?

Could someone possibly please look at the code and tell me what needs to be changed in order for this to keep time properly?

Thanks in advance


#include <RTClib.h>
RTC_DS3231 rtc;

// minutes 
uint8_t min, prev_min;

// ports used to control the stepper motor
// if your motor rotate to the opposite direction, 
// change the order as {4, 5, 6, 7};
// for Arduino nano
int port[4] = {7, 6, 5, 4};
// for ESP32 (example)
// int port[4] = {12, 32, 25, 27};

// Motor and clock parameters
#define STEPS_PER_ROTATION 1536 // steps for a minute

// wait for a single step of stepper
#define DELAYTIME 3

// sequence of stepper motor control
int seq[4][4] = {
  {  LOW,  LOW, HIGH,  LOW},
  {  LOW,  LOW,  LOW, HIGH},
  { HIGH,  LOW,  LOW,  LOW},
  {  LOW, HIGH,  LOW,  LOW}
};

void rotate(int step) {
  static int phase = 0;
  int i, j;
  int delta = (step > 0) ? 1 : 3;
  int dt = 20;

  step = (step > 0) ? step : -step;
  for(j = 0; j < step; j++) {
    phase = (phase + delta) % 4;
    for(i = 0; i < 4; i++) {
      digitalWrite(port[i], seq[phase][i]);
    }
    delay(dt);
    if(dt > DELAYTIME) dt--;
  }
  // power cut
  for(i = 0; i < 4; i++) {
    digitalWrite(port[i], LOW);
  }
}

void setup() {
  DateTime thetime;

  //Serial for debugging
  Serial.begin(9600);

  // setup rtc 
  rtc.begin();
  
  if (!rtc.begin()) {
    Serial.println("RTC is NOT running!");
    abort();
  }
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(__DATE__, __TIME__));
  
  thetime = rtc.now();

  Serial.println("RTC is running!"); 
  Serial.print(thetime.year(), DEC);
  Serial.print('/');
  Serial.print(thetime.month(), DEC);
  Serial.print('/');
  Serial.print(thetime.day(), DEC);
  Serial.print(' ');
  Serial.print(thetime.hour(), DEC);
  Serial.print(':');
  Serial.print(thetime.minute(), DEC);
  Serial.print(':');
  Serial.print(thetime.second(), DEC);
  Serial.println();

  // setup stepper
  pinMode(port[0], OUTPUT);
  pinMode(port[1], OUTPUT);
  pinMode(port[2], OUTPUT);
  pinMode(port[3], OUTPUT);

  // set prev_min to an invalid value, so that program starts with a min rotation after reset
  prev_min = 99;
}

void loop() {
  
  min = rtc.now().minute();

  if(prev_min != min) {
    rotate(-10); // for approach run
    rotate(10); // approach run without heavy load
    rotate(STEPS_PER_ROTATION);
    prev_min = min;
  }
  
  delay(100);
}

you reset the time every time you boot up your Arduino

@agzorb1842

1. Enter you current time and upload the sketch.
2. Comment out the following line:

//rtc.adjust(DateTime(__DATE__, __TIME__));

3. Uplaod the sketch again.

What makes you think that the module is not keeping time?

The time is printed out only once, in setup().

Is there a battery installed, is it NOT dead?

It does not advance every minute as it should

yes

I am aware. But it then does not advance every minute. Its more like 45 seconds (ish)

I have a few of these and have tested them. They are all extremely accurate. Have you tried the library examples, if they work then it's your code.

Please post a wiring diagram, as motor electrical interference might be causing problems.

Hand drawn is preferred, with all pins, connections and parts clearly identified. Also post links to the motors and motor drivers.

1 Like

When you post the wiring diagram as @jremington requested make a not as to the voltages on its pins. It may be part of this taken from the data sheet:
"A precision temperature-compensated voltage reference and comparator circuit monitors the status of VCC to detect power failures, to provide a reset output, and to automatically switch to the backup supply when necessary. Additionally, the active-low RST pin is monitored as a pushbutton input for generating a µP reset." We cannot be sure until we see the schematic.

If you have a 2032 standard battery (not rechargable) installed (as well as not commenting out the date set) it must have the charging diode removed from the battery circuit.
Otherwise it will bog down your supply voltage with weird results.

Plenty of places on the web show you how to do it.