DS3231 RTC Interrupt Time Display Problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Alandinho10
 
Posts: 2
Joined: Tue May 23, 2023 11:36 am

DS3231 RTC Interrupt Time Display Problem

Post by Alandinho10 »

Hello Everyone,

This code is printing the current time and temperature in Fahrenheit no problem, but the time in milli seconds is not going up even when I have the SQW wired to pin 32. Here is the code and output below:

Code:

Code: Select all

#include "RTClib.h"

RTC_DS3231 rtc;

int interrupt_pin = 32;
int current = 0;
int milliseconds = 0;

void IRAM_ATTR function()
{
    milliseconds = millis() - current;
    current = millis();
}

void setup() {
  Serial.begin(9600);
  rtc.begin();

  pinMode(interrupt_pin, INPUT_PULLUP);
  attachInterrupt(interrupt_pin, function, FALLING);

  // Set the time to 11:30 AM
  rtc.adjust(DateTime(__DATE__, __TIME__));
  rtc.adjust(DateTime(0, 0, 0, 11, 20, 0));  // Set the desired time here
}

void loop() {
  DateTime now = rtc.now();

  // Display time
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print('.');
  Serial.print(milliseconds);

  float temperatureC = rtc.getTemperature();
  float temperatureF = (temperatureC * 9 / 5) + 32; // Convert Celsius to Fahrenheit
  Serial.print(" ");
  Serial.println(temperatureF);
}

Output:

11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:42.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30
11:20:43.0 74.30

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: DS3231 RTC Interrupt Time Display Problem

Post by adafruit_support_bill »

The square wave output defaults to off at power up. You need to enable it: https://adafruit.github.io/RTClib/html/ ... a72dae1f72

User avatar
Alandinho10
 
Posts: 2
Joined: Tue May 23, 2023 11:36 am

Re: DS3231 RTC Interrupt Time Display Problem

Post by Alandinho10 »

Great and what is the default output Square wave is it 1000hz?

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: DS3231 RTC Interrupt Time Display Problem

Post by adafruit_support_bill »

The mode options are defined in the header file:

https://github.com/adafruit/RTClib/blob ... c/RTClib.h

Code: Select all

/** DS3231 SQW pin mode settings */
enum Ds3231SqwPinMode {
  DS3231_OFF = 0x1C,            /**< Off */
  DS3231_SquareWave1Hz = 0x00,  /**<  1Hz square wave */
  DS3231_SquareWave1kHz = 0x08, /**<  1kHz square wave */
  DS3231_SquareWave4kHz = 0x10, /**<  4kHz square wave */
  DS3231_SquareWave8kHz = 0x18  /**<  8kHz square wave */
};

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

Return to “Arduino”