Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- To use the ChronoDot (DS3231) with an Arduino, you can use the "RTClib" library by Adafruit. This library provides an easy way to interact with the DS3231 RTC using I2C communication.
- Here's an example of Arduino code to read and set the time on a ChronoDot DS3231 RTC module:
- Requirements:
- Arduino board (e.g., Uno, Mega, Nano)
- ChronoDot DS3231 RTC module
- Jumper wires
- RTClib library (install from Arduino IDE Library Manager)
- In this example, the ChronoDot DS3231 RTC module is connected to the Arduino using the I2C interface (A4 for SDA and A5 for SCL on an Arduino Uno). The module is initialized, and the current date and time are set if the module has lost power. The code reads the current date and time from the DS3231 RTC module and prints it to the Serial Monitor every second.
- Before uploading the code, make sure you have the RTClib library installed. You can install it through the Arduino IDE Library Manager. Search for "RTClib" by Adafruit and install the library.
- */
- #include <Wire.h>
- #include "RTClib.h"
- // Create a DS3231 RTC object
- RTC_DS3231 rtc;
- void setup() {
- Serial.begin(9600);
- Wire.begin();
- // Initialize the DS3231 RTC module
- if (!rtc.begin()) {
- Serial.println("Couldn't find the RTC module!");
- while (1);
- }
- // Check if the RTC module lost power
- if (rtc.lostPower()) {
- Serial.println("RTC lost power. Setting the time...");
- // Set the RTC module to the current date and time
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- }
- void loop() {
- // Read the current date and time from the DS3231 RTC module
- DateTime now = rtc.now();
- // Print the date and time to the Serial Monitor
- Serial.print("Date: ");
- Serial.print(now.year(), DEC);
- Serial.print('/');
- Serial.print(now.month(), DEC);
- Serial.print('/');
- Serial.print(now.day(), DEC);
- Serial.print(" Time: ");
- Serial.print(now.hour(), DEC);
- Serial.print(':');
- Serial.print(now.minute(), DEC);
- Serial.print(':');
- Serial.print(now.second(), DEC);
- Serial.println();
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement