DS3231 Finde keine RTC

Hallo, ich habe einen Wemos D1 mini pro, einen DS3231 der separat mit Strom versorgt wird. Folgenden Code versuche ich, den ich später erweitern möchte.

/*
Project:  DS3231 RTC - Alarm

Required Board (Tools -> Board -> Boards Manager...)
 - Board: esp8266 by ESP8266 Community   V3.1.1 

Required libraries (sketch -> include library -> manage libraries)
 - Adafruit RTClib library V2.1.1 / https://github.com/adafruit/RTClib
 - Adafruit BusIO library V1.13.2 / https://github.com/adafruit/Adafruit_BusIO

Wirering for the DS3231 RTC Modul:
DS3231      Wemos d1 mini pro      
SCL         D1/5    
SDA         D2/4     
SQW         D4/2
*/

#include "RTClib.h"
#include "Wire.h"
RTC_DS3231 rtc;
const uint8_t alarmPin = 2;

void IRAM_ATTR onAlarmIntPin() {
  // Diese Funktion wird mit dem SQW Pin bei einem Alarm aufgerufen
  Serial.println("Alarm wurde am Interrupt Pin ausgelöst!");
}
void setup() {
  Serial.begin(74880);
  if (!rtc.begin()) {
    Serial.println("Finde keine RTC");
    while (true)
      ;
  }

  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));  // Zeit vom Compiler
  //rtc.adjust(DateTime(2025, 10, 6, 10, 15, 2)); // J, M, T, Std, Min, Sek

  pinMode(alarmPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(alarmPin), onAlarmIntPin, FALLING);

  //Alarm register löschen und beide Alarme vorerst ausschalten
  rtc.disableAlarm(1);
  rtc.disableAlarm(2);
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);
  rtc.writeSqwPinMode(DS3231_OFF);

  // Alarm 1 und 2 setzen
  rtc.setAlarm1(rtc.now() + TimeSpan(0, 0, 0, 10), DS3231_A1_Second);
  rtc.setAlarm2(DateTime(0, 0, 0, 13, 15, 0), DS3231_A2_Hour);
}

void loop() {
  if (rtc.alarmFired(1)) {
    rtc.clearAlarm(1);
    Serial.println("Alarm 1 ausgelöst über alarmFired Register");
    Serial.println();
  }

  if (rtc.alarmFired(2)) {
    rtc.clearAlarm(2);
    Serial.println("Alarm 2 ausgelöst über alarmFired Register");
    Serial.println();
  }
}

Die Fehlermelung ist:

06:59:06.754 -> Finde keine RTC
06:59:09.053 -> 
06:59:09.053 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
06:59:09.053 -> 
06:59:09.053 -> Soft WDT reset
06:59:09.053 -> 

Das Programm habe ich schon min. 40x hochgeladen und zwischendurch funktioniert es auch. Vielleicht 3x in all den Versuchen. Einen neuen Wemos, einen neuen DS3231 und eine neue Knopfzelle habe ich bestellt mit dem gleichen Ergebnis. Wieso funktioniert es manchmal? Was muss ich an dem Programm ändern?

Danke im Voraus für eure Bemühungen.

Gruß blackvento

Der hat keine PULL-UP auf dem I2C.
Wenn SCL/SDA nicht vertauscht sind, schau mal, ob auf dem Clock-Modul PULL-Ups drauf sind. Wenn nicht müssen die nachgerüstet werden.

GND muss ESP und RTC müssen verbunden sein, wenn die RTC ein Modul ist dann hat die zwei 4,7k PULL-UP Widerstände.

Warum lässt du dir nicht die zeit anzeigen im SerMon? Dazu kommt noch wenn du die RTC mit einer Spannung > als 3,3V versorgst ist das ungesund für den ESP.
DIe RTC funktioniert ab 2,5V wenn gut in Erringung, auf jeden fall mit 3,3V bis 5V.

Danke, wie fony gepostet sind die PULL-Ups auf dem DS3231 vorhanden.

Hallo fony, der ESP wird mit 5V separat versorgt. Ich habe GND-ESP und GND-DS3231 verbunden. Gleiches Ergebnis. Dann habe ich den DS3231 direkt an GND u. VCC 5V-ESP angeschlossen. Gleiches Ergebnis. Oft hochgeladen, einmal hat es funktioniert. Den 220ohm Widerstand habe ich entfernt wegen der Knopfzelle.

Das Modul DS3231 hat PULL-UP Widerstände. Muss ich trotzdem am Wemos für PULL-UP sorgen?

Nein, habe keine Probleme mit der DS3231 + ESP8266, funktioniert seit 6J im Garten, werden PV Daten auf SD geschrieben

Wemos D1 mini ?

Nein ESP8266 NodeMCU. Ist doch egal oder?

Kann ich leide nicht beantworten. Bin totaler Leihe. Ich habe den mini pro genommen wegen der Größe.

Hast du dir Zeit anzeigen lassen? der ESP resettet bei dir.
Habe keine RTC frei zum testen.

Ich werde mal googeln wie das geht und dann melden.

Danke

Auf die schnelle gemacht.

/*
  Project:  DS3231 RTC - Alarm

  Required Board (Tools -> Board -> Boards Manager...)
  - Board: esp8266 by ESP8266 Community   V3.1.1

  Required libraries (sketch -> include library -> manage libraries)
  - Adafruit RTClib library V2.1.1 / https://github.com/adafruit/RTClib
  - Adafruit BusIO library V1.13.2 / https://github.com/adafruit/Adafruit_BusIO

  Wirering for the DS3231 RTC Modul:
  DS3231      Wemos d1 mini pro
  SCL         D1/5
  SDA         D2/4
  SQW         D4/2
*/

#include "RTClib.h"
#include "Wire.h"
RTC_DS3231 rtc;
const uint8_t alarmPin = 2;
DateTime now = rtc.now();
void IRAM_ATTR onAlarmIntPin() {
  // Diese Funktion wird mit dem SQW Pin bei einem Alarm aufgerufen
  Serial.println("Alarm wurde am Interrupt Pin ausgelöst!");
}
void setup() {
  Serial.begin(74880);
  if (!rtc.begin()) {
    Serial.println("Finde keine RTC");
    while (true)
      ;
  }

  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));  // Zeit vom Compiler
  //rtc.adjust(DateTime(2025, 10, 6, 10, 15, 2)); // J, M, T, Std, Min, Sek

  pinMode(alarmPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(alarmPin), onAlarmIntPin, FALLING);

  //Alarm register löschen und beide Alarme vorerst ausschalten
  rtc.disableAlarm(1);
  rtc.disableAlarm(2);
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);
  rtc.writeSqwPinMode(DS3231_OFF);

  // Alarm 1 und 2 setzen
  rtc.setAlarm1(rtc.now() + TimeSpan(0, 0, 0, 10), DS3231_A1_Second);
  rtc.setAlarm2(DateTime(0, 0, 0, 13, 15, 0), DS3231_A2_Hour);
}

void loop() {

  char buf [40];
  sprintf(buf , "%.2d.%.2d.%d %.2d:%.2d:%.2d ",
          now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

 Serial.println(BUf);
  delay (1000);
  if (rtc.alarmFired(1)) {
    rtc.clearAlarm(1);
    Serial.println("Alarm 1 ausgelöst über alarmFired Register");
    Serial.println();
  }

  if (rtc.alarmFired(2)) {
    rtc.clearAlarm(2);
    Serial.println("Alarm 2 ausgelöst über alarmFired Register");
    Serial.println();
  }
}

Danke, werde ich testen.

Habe eine RTC gefunden mit dem funktioniert die Zeit ausgebe

/*
  Project:  DS3231 RTC - Alarm

  Required Board (Tools -> Board -> Boards Manager...)
  - Board: esp8266 by ESP8266 Community   V3.1.1

  Required libraries (sketch -> include library -> manage libraries)
  - Adafruit RTClib library V2.1.1 / https://github.com/adafruit/RTClib
  - Adafruit BusIO library V1.13.2 / https://github.com/adafruit/Adafruit_BusIO

  Wirering for the DS3231 RTC Modul:
  DS3231      Wemos d1 mini pro
  SCL         D1/5
  SDA         D2/4
  SQW         D4/2
*/

#include "RTClib.h"
//#include "Wire.h"
RTC_DS3231 rtc;
//const uint8_t alarmPin = 2;

/*void IRAM_ATTR onAlarmIntPin() {
  // Diese Funktion wird mit dem SQW Pin bei einem Alarm aufgerufen
  Serial.println("Alarm wurde am Interrupt Pin ausgelöst!");
}
*/
void setup() {
  Serial.begin(115200);
  rtc.begin(); 
  

  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));  // Zeit vom Compiler
  //rtc.adjust(DateTime(2025, 10, 6, 10, 15, 2)); // J, M, T, Std, Min, Sek

//  pinMode(alarmPin, INPUT_PULLUP);
 // attachInterrupt(digitalPinToInterrupt(alarmPin), onAlarmIntPin, FALLING);

 /* //Alarm register löschen und beide Alarme vorerst ausschalten
  rtc.disableAlarm(1);
  rtc.disableAlarm(2);
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);
  rtc.writeSqwPinMode(DS3231_OFF);

  // Alarm 1 und 2 setzen
  rtc.setAlarm1(rtc.now() + TimeSpan(0, 0, 0, 10), DS3231_A1_Second);
  rtc.setAlarm2(DateTime(0, 0, 0, 13, 15, 0), DS3231_A2_Hour);
  */
}

void loop() {
DateTime now = rtc.now();
  char buf [40];
  sprintf(buf , "%.2d.%.2d.%d %.2d:%.2d:%.2d ",
          now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
  delay (1000);
   Serial.println (buf);
 /* if (rtc.alarmFired(1)) {
    rtc.clearAlarm(1);
    Serial.println("Alarm 1 ausgelöst über alarmFired Register");
    Serial.println();
  }

  if (rtc.alarmFired(2)) {
    rtc.clearAlarm(2);
    Serial.println("Alarm 2 ausgelöst über alarmFired Register");
    Serial.println();
  }*/
}
 10:56:47.042 -> 07.12.2025 10:56:19 
10:56:48.039 -> 07.12.2025 10:56:20 
10:56:49.028 -> 07.12.2025 10:56:21 
10:56:50.021 -> 07.12.2025 10:56:22 
10:56:51.064 -> 07.12.2025 10:56:23 
10:56:52.050 -> 07.12.2025 10:56:24 
10:56:53.064 -> 07.12.2025 10:56:25 
10:56:54.061 -> 07.12.2025 10:56:26

Jetzt muss man nur den Wecker zum laufen bringen, auch das Demo funktioniert nicht muss finden warum

Ich würde das I²C-Scanner-Programm laden, um den I²C-Bus zu testen:

TIME	DEC	HEX		50	100	150	200	250	300	350	400	[KHz]
--------------------------------------------------------------------------------------------------------
22067	87	0x57		V	V	V	V	V	V	V	V
22091	104	0x68		V	V	V	V	V	V	V	V

Wenn das funktioniert, kannst Du mit dem Anwendungsprogramm starten :wink:

Bei Interrupt kommt zum Resett!
Das sind keine I²C Probleme

Serial.print hat in ISR nichts verloren.

Gruß Tommy

Sogar das Beispiel funktioniert nicht

/* Example implementation of an alarm using DS3231
 *
 * VCC and GND of RTC should be connected to some power source
 * SDA, SCL of RTC should be connected to SDA, SCL of arduino
 * SQW should be connected to CLOCK_INTERRUPT_PIN
 * CLOCK_INTERRUPT_PIN needs to work with interrupts
 */

#include <RTClib.h>
// #include <Wire.h>

RTC_DS3231 rtc;

// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 14

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

    // initializing the rtc
    if(!rtc.begin()) {
        Serial.println("Couldn't find RTC!");
        Serial.flush();
        while (1) delay(10);
    }

    if(rtc.lostPower()) {
        // this will adjust to the date and time at compilation
      //  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }

    //we don't need the 32K Pin, so disable it
    rtc.disable32K();

    // Making it so, that the alarm will trigger an interrupt
    pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);

    // set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
    // if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
    rtc.clearAlarm(1);
    rtc.clearAlarm(2);

    // stop oscillating signals at SQW Pin
    // otherwise setAlarm1 will fail
    rtc.writeSqwPinMode(DS3231_OFF);

    // turn off alarm 2 (in case it isn't off already)
    // again, this isn't done at reboot, so a previously set alarm could easily go overlooked
    rtc.disableAlarm(2);

    // schedule an alarm 10 seconds in the future
    if(!rtc.setAlarm1(
            rtc.now() + TimeSpan(10),
            DS3231_A1_Second // this mode triggers the alarm when the seconds match. See Doxygen for other options
    )) {
        Serial.println("Error, alarm wasn't set!");
    }else {
        Serial.println("Alarm will happen in 10 seconds!");
    }
}

void loop() {
    // print current time
    char date[10] = "hh:mm:ss";
    rtc.now().toString(date);
    Serial.print(date);

    // the stored alarm value + mode
    DateTime alarm1 = rtc.getAlarm1();
    Ds3231Alarm1Mode alarm1mode = rtc.getAlarm1Mode();
    char alarm1Date[12] = "DD hh:mm:ss";
    alarm1.toString(alarm1Date);
    Serial.print(" [Alarm1: ");
    Serial.print(alarm1Date);
    Serial.print(", Mode: ");
    switch (alarm1mode) {
      case DS3231_A1_PerSecond: Serial.print("PerSecond"); break;
      case DS3231_A1_Second: Serial.print("Second"); break;
      case DS3231_A1_Minute: Serial.print("Minute"); break;
      case DS3231_A1_Hour: Serial.print("Hour"); break;
      case DS3231_A1_Date: Serial.print("Date"); break;
      case DS3231_A1_Day: Serial.print("Day"); break;
    }

    // the value at SQW-Pin (because of pullup 1 means no alarm)
    Serial.print("] SQW: ");
    Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));

    // whether a alarm fired
    Serial.print(" Fired: ");
    Serial.print(rtc.alarmFired(1));

    // Serial.print(" Alarm2: ");
    // Serial.println(rtc.alarmFired(2));
    // control register values (see https://datasheets.maximintegrated.com/en/ds/DS3231.pdf page 13)
    // Serial.print(" Control: 0b");
    // Serial.println(read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), BIN);

    // resetting SQW and alarm 1 flag
    // using setAlarm1, the next alarm could now be configurated
    if (rtc.alarmFired(1)) {
        rtc.clearAlarm(1);
        Serial.print(" - Alarm cleared");
    }
    Serial.println();

    delay(2000);
}

void onAlarm() {
    Serial.println("Alarm occured!");
}

/*static uint8_t read_i2c_register(uint8_t addr, uint8_t reg) {
    Wire.beginTransmission(addr);
    Wire.write((byte)reg);
    Wire.endTransmission();

    Wire.requestFrom(addr, (byte)1);
    return Wire.read();
}*/

Haber aber was von Dir gefunden :

Habe vorerst keine Zeit sich damit beschäftigen.
Wo möglich Abends kommt was raus.

Gruß Bernhard

Also das funktioniert schon muss aber angepasst werden, woher das ist stecht unten.

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete instructions at https://RandomNerdTutorials.com/esp8266-nodemcu-ds3231-real-time-clock-arduino/
*********/
// Example based on the library: implementation of an alarm using DS3231
#include <RTClib.h>

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 14

// LED for visual indication
const int ledPin = 2;

// set the alarms
// (year, month, day, hour, minutes, seconds)
DateTime alarm1Time = DateTime(2025, 01, 03, 18, 42, 0);
DateTime alarm2Time = DateTime(2025, 01, 03, 18, 48, 0);

void printCurrentTime(){
  // Get the current time from the RTC
  DateTime now = rtc.now();
  
  // Getting each time field in individual variables
  // And adding a leading zero when needed;
  String yearStr = String(now.year(), DEC);
  String monthStr = (now.month() < 10 ? "0" : "") + String(now.month(), DEC);
  String dayStr = (now.day() < 10 ? "0" : "") + String(now.day(), DEC);
  String hourStr = (now.hour() < 10 ? "0" : "") + String(now.hour(), DEC); 
  String minuteStr = (now.minute() < 10 ? "0" : "") + String(now.minute(), DEC);
  String secondStr = (now.second() < 10 ? "0" : "") + String(now.second(), DEC);
  String dayOfWeek = daysOfTheWeek[now.dayOfTheWeek()];

  // Complete time string
  String formattedTime = dayOfWeek + ", " + yearStr + "-" + monthStr + "-" + dayStr + " " + hourStr + ":" + minuteStr + ":" + secondStr;

  // Print the complete formatted time
  Serial.println(formattedTime);
}

ICACHE_RAM_ATTR void onAlarm() {
  Serial.println("Alarm occured!");
  // toggle the current LED state
  int state = digitalRead(ledPin);
  digitalWrite(ledPin, !state);
}

void setup() {
  Serial.begin(115200);
  pinMode (ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // initializing the rtc
  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC!");
    Serial.flush();
    while (1) delay(10);
  }

  if(rtc.lostPower()) {
    // this will adjust to the date and time at compilation
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  // Uncomment if you need to adjust the time
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  //we don't need the 32K Pin, so disable it
  rtc.disable32K();

  // Trigger an interrupt when the alarm happens
  pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);

  // set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
  // if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);

  // stop oscillating signals at SQW Pin, otherwise setAlarm1 will fail
  rtc.writeSqwPinMode(DS3231_OFF);

  // turn off alarm 2 (in case it isn't off already)
  // again, this isn't done at reboot, so a previously set alarm could easily go overlooked
  rtc.disableAlarm(2);

  // Schedule Alarm1 to fire when the minutes match
  if(!rtc.setAlarm1(alarm1Time, DS3231_A1_Second)) {  // this mode triggers the alarm when the minutes match
    Serial.println("Error, alarm wasn't set!");
  }else {
    Serial.println("Alarm 1 will happen at specified time");
  }
}

void loop() {
  // print current date and time
  printCurrentTime();

  // Get Details about the alarm1
  DateTime alarm1 = rtc.getAlarm1();
  Ds3231Alarm1Mode alarm1mode = rtc.getAlarm1Mode();
  char alarm1Date[12] = "DD hh:mm:ss";
  alarm1.toString(alarm1Date);
  Serial.print("[Alarm1: ");
  Serial.print(alarm1Date);
  Serial.print(", Mode: ");
  switch (alarm1mode) {
    case DS3231_A1_PerSecond: Serial.print("PerSecond"); break;
    case DS3231_A1_Second: Serial.print("Second"); break;
    case DS3231_A1_Minute: Serial.print("Minute"); break;
    case DS3231_A1_Hour: Serial.print("Hour"); break;
    case DS3231_A1_Date: Serial.print("Date"); break;
    case DS3231_A1_Day: Serial.print("Day"); break;
  }
  // the value at SQW-Pin (because of pullup 1 means no alarm)
  Serial.print("] SQW: ");
  Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));
  // whether a alarm fired
  Serial.print(" Fired: ");
  Serial.print(rtc.alarmFired(1));

  // Only one alarm can be set at a time, reset alarm 1 and activate alarm 2
  // resetting SQW and alarm 1 flag
  // the next alarm could now be configurated
  if (rtc.alarmFired(1)) {
    rtc.clearAlarm(1);
    Serial.println(" - Alarm cleared");
    
    // Set Alarm 2
    if(!rtc.setAlarm2(alarm2Time, DS3231_A2_Minute)) {  // this mode triggers the alarm when the minutes match
      Serial.println("Error, alarm wasn't set!");
    }else {
      Serial.println("Alarm 2 will happen at specified time");
    }

    // Get Details about the alarm2
    DateTime alarm1 = rtc.getAlarm2();
    Ds3231Alarm2Mode alarm2mode = rtc.getAlarm2Mode();
    char alarm2Date[12] = "DD hh:mm:ss";
    alarm1.toString(alarm2Date);
    Serial.print("[Alarm2: ");
    Serial.print(alarm2Date);
    Serial.print(", Mode: ");
    switch (alarm2mode) {
      case DS3231_A2_PerMinute: Serial.print("Every Minute"); break;
      case DS3231_A2_Minute: Serial.print("Minute"); break;
      case DS3231_A2_Hour: Serial.print("Hour"); break;
      case DS3231_A2_Date: Serial.print("Date"); break;
      case DS3231_A2_Day: Serial.print("Day"); break;
    }
    // the value at SQW-Pin (because of pullup 1 means no alarm)
    Serial.print("] SQW: ");
    Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));
    // whether a alarm fired
    Serial.print(" Fired: ");
    Serial.print(rtc.alarmFired(1));
  }

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

Meine Zeit ist abgelaufen .
Dort sind auch andere Beispiele.