How to use the DS3231 Real Time Clock with Arduino
How to use the DS3231 Real Time Clock with Arduino
INTRODUCTION
The DS3231 Real Time clock provides highly accurate information about time which contains hours, minutes and seconds, as well as, day, month and year information. Also, it has automatic compensation for leap-years and for months with fewer than 31 days.
APPLICATIONS
Due to its low cost it is a good fit for every electronics projects where date and time is required. Other applications include Servers, Utility power meters, Telematics, GPS Systems, etc..
WIRING DIAGRAM
VCC – 5V
SCL – Analog Pin A5
SDA – Analog Pin A4
GND - GND
CODE
Note: Download the required library and follow these steps below:
Download the DS3231 library here:
rinkydinkelectronics.com/library.php?id=73
Put the DS3231 folder in “arduino/libraries”.
/* Code for testing the DS3231 Real Time Clock * Code taken from DS3231 library example * DS3231 library: http://www.rinkydinkelectronics.com/library.php?id=73 */ #include <DS3231.h> DS3231 rtc(SDA, SCL); void setup() { // Setup Serial connection Serial.begin(115200); // Uncomment the next line if you are using an Arduino Leonardo //while (!Serial) {} // Initialize the rtc object rtc.begin(); // The following lines can be uncommented to set the date and time //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 } // Code from the Demo Example of the DS3231 Library void loop() { // Send Day-of-Week Serial.print(rtc.getDOWStr()); Serial.print(" "); // Send date Serial.print(rtc.getDateStr()); Serial.print(" -- "); // Send time Serial.println(rtc.getTimeStr()); // Wait one second before repeating delay (1000); }
END RESULT
Open serial monitor to see day, date and time.
ADDITIONAL RESOURCES
https://howtomechatronics.com/tutorials/arduino/arduino-ds3231-real-time-clock-tutorial/