Advertisement
microrobotics

ChronoDot- Ultra-precise Real Time Clock

May 8th, 2023
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3.  
  4. Here's an example of Arduino code to read and set the time on a ChronoDot DS3231 RTC module:
  5.  
  6. Requirements:
  7.  
  8. Arduino board (e.g., Uno, Mega, Nano)
  9. ChronoDot DS3231 RTC module
  10. Jumper wires
  11. RTClib library (install from Arduino IDE Library Manager)
  12.  
  13. 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.
  14.  
  15. 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.
  16. */
  17.  
  18. #include <Wire.h>
  19. #include "RTClib.h"
  20.  
  21. // Create a DS3231 RTC object
  22. RTC_DS3231 rtc;
  23.  
  24. void setup() {
  25.   Serial.begin(9600);
  26.   Wire.begin();
  27.  
  28.   // Initialize the DS3231 RTC module
  29.   if (!rtc.begin()) {
  30.     Serial.println("Couldn't find the RTC module!");
  31.     while (1);
  32.   }
  33.  
  34.   // Check if the RTC module lost power
  35.   if (rtc.lostPower()) {
  36.     Serial.println("RTC lost power. Setting the time...");
  37.     // Set the RTC module to the current date and time
  38.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  39.   }
  40. }
  41.  
  42. void loop() {
  43.   // Read the current date and time from the DS3231 RTC module
  44.   DateTime now = rtc.now();
  45.  
  46.   // Print the date and time to the Serial Monitor
  47.   Serial.print("Date: ");
  48.   Serial.print(now.year(), DEC);
  49.   Serial.print('/');
  50.   Serial.print(now.month(), DEC);
  51.   Serial.print('/');
  52.   Serial.print(now.day(), DEC);
  53.   Serial.print(" Time: ");
  54.   Serial.print(now.hour(), DEC);
  55.   Serial.print(':');
  56.   Serial.print(now.minute(), DEC);
  57.   Serial.print(':');
  58.   Serial.print(now.second(), DEC);
  59.   Serial.println();
  60.  
  61.   delay(1000);
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement