Bestilt før kl. 14.00 på hverdage = afsendt samme dag 30 dages fortrydelsesret 1 års garanti
← Terug til oversigten

Tilslutningsdiagram og kodeeksempel

Sådan tilslutter du DS3231 Real Time Clock Module I2C (RTC høj præcision) til en Arduino UNO eller ESP32, med en eksempelsketch, du kan uploade med det samme.

DS3231 realtidsur med Arduino Uno

Dette projekt forbinder et højpræcist DS3231 RTC-modul med en Arduino Uno via I2C. Sketchen sætter RTC'en til kompileringstiden, hvis strømmen har været afbrudt, og læser derefter hver sekund den formaterede dato, tid og målingerne fra den indbyggede temperatursensor og viser dem i Serial Monitor.

Tilslutningsdiagram: DS3231 Real Time Clock Module I2C (RTC høj præcision) på en Arduino UNO
Tilslutningsdiagram · Klik for at forstørre
Tilslutningstabel: DS3231 Real Time Clock Module I2C (RTC høj præcision) på en Arduino UNO
Tilslutningstabel · Klik for at forstørre
DS3231_Real_Time_Clock_with_Arduino_Uno.ino · kompileret og testet
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : DS3231 Real-Time Clock with Arduino Uno
 *  Board     : Arduino UNO (arduino:avr:uno)
 *  Parts     : DS3231 Real Time Clock Module I2C
 *  Libraries : RTClib 2.1.4
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

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

RTC_DS3231 rtc;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!rtc.begin()) {
    Serial.println(F("Couldn't find DS3231 RTC"));
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println(F("RTC lost power, setting time to compile time..."));
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  Serial.println(F("DS3231 RTC initialized."));
}

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

  char dateBuffer[11];
  char timeBuffer[9];
  snprintf(dateBuffer, sizeof(dateBuffer), "%04d-%02d-%02d", now.year(), now.month(), now.day());
  snprintf(timeBuffer, sizeof(timeBuffer), "%02d:%02d:%02d", now.hour(), now.minute(), now.second());

  Serial.print(F("Date: "));
  Serial.print(dateBuffer);
  Serial.print(F(" | Time: "));
  Serial.print(timeBuffer);
  Serial.print(F(" | Temp: "));
  Serial.print(rtc.getTemperature());
  Serial.println(F(" C"));

  delay(1000);
}

Nødvendige biblioteker: RTClib 2.1.4

Wiring diagram and example code generated by Codey.online — https://www.codey.online

ESP32 realtidsur med DS3231

Dette projekt aflæser den aktuelle dato, tid og interne temperatur fra DS3231 Real Time Clock-modulet via I2C. Tilslut modulet til ESP32'ens 3.3V-rail og standard I2C-benene, og åbn derefter Serial Monitor ved 115200 baud for at se tidsstemplet i realtid.

Tilslutningsdiagram: DS3231 Real Time Clock Module I2C (RTC høj præcision) på en ESP32 DevKit V1
Tilslutningsdiagram · Klik for at forstørre
Tilslutningstabel: DS3231 Real Time Clock Module I2C (RTC høj præcision) på en ESP32 DevKit V1
Tilslutningstabel · Klik for at forstørre
ESP32_Real_Time_Clock_with_DS3231.ino · kompileret og testet
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : ESP32 Real-Time Clock with DS3231
 *  Board     : ESP32 DEVKIT V1 (esp32:esp32:esp32doit-devkit-v1)
 *  Parts     : DS3231 Real Time Clock Module I2C
 *  Libraries : RTClib 2.1.4
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

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

RTC_DS3231 rtc;

void setup() {
  Serial.begin(115200);
  while (!Serial) { delay(10); }

  Wire.begin(21, 22); // SDA = GPIO 21, SCL = GPIO 22

  if (!rtc.begin()) {
    Serial.println("Couldn't find DS3231 RTC module!");
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting time to compilation time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

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

  char buffer[30];
  snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d %02d:%02d:%02d",
           now.year(), now.month(), now.day(),
           now.hour(), now.minute(), now.second());

  Serial.print("Current Time: ");
  Serial.print(buffer);
  Serial.print(" | Temp: ");
  Serial.print(rtc.getTemperature());
  Serial.println(" °C");

  delay(1000);
}

Nødvendige biblioteker: RTClib 2.1.4

Wiring diagram and example code generated by Codey.online — https://www.codey.online

Set for nylig

Nano V3 Arduino Compatible CH340

Nano V3 Arduino Compatible CH340

€ 6,60
Step-Up Power Module 0.9-5v til 5v DC-DC

Step-Up Power Module 0.9-5v til 5v DC-DC

€ 3,05
PH2 lille Vippesensor Plast  55 MM - til sideinstallation

PH2 lille Vippesensor Plast 55 MM - til sideinstallation

€ 2,10