The DS1302 is a popular RTC module that allows your Arduino projects to keep accurate time even after power loss. This tutorial will guide you through connecting and using the DS1302 with your Arduino.
Specs
- Timekeeping:
- Tracks seconds, minutes, hours, date (including day of month), month, day of week, and year.
- Automatically accounts for leap years up to 2100.
- Selectable 12-hour or 24-hour format.
- Memory:
- 31 bytes of battery-backed static RAM (SRAM) for general-purpose data storage.
- Communication:
- Simple serial interface compatible with most microcontrollers.
- Uses I2C protocol for communication (typically connects to SCL and SDA pins on Arduino).
- Supports single-byte and multi-byte data transfer modes.
- Power:
- Operates between 2.0V and 5.5V.
- Low power consumption (less than 300 nA at 2.0V) for extended battery life.
- Requires a CR2032 lithium coin cell for backup power when main power is lost.
- Other:
- Operating temperature range: 0°C to +70°C
Pinout
- VCC (Power Supply): This pin supplies power to the DS1302. It typically requires a voltage between 2.0V and 5.5V.
- GND (Ground): This pin connects the DS1302 to the ground circuit of your project.
- SCL (Serial Clock): This pin is part of the I2C communication interface. It synchronizes the clock signal between the DS1302 and your microcontroller (like Arduino).
- SDA (Serial Data): This pin is also part of the I2C interface. It transmits and receives serial data between the DS1302 and your microcontroller.
.
Components
Arduino Uno
Breadboard
Real time clock DS1302
Jumper wires
CR2032 Battery
Connection Diagram
DS1302 Library
The Code!
#include <LiquidCrystal.h> #include <DS1302.h> // Init the DS1302 DS1302 rtc(2, 3, 4); void setup() { // Set the clock to run-mode, and disable the write protection Serial.begin(9600); rtc.halt(false); rtc.writeProtect(false); // The following lines can be commented out to use the values already stored in the DS1302 rtc.setDOW(FRIDAY); // This line is only for the first time to set the correct day rtc.setTime(12, 0, 0); // This line is only for the first time to set the correct time rtc.setDate(6, 8, 2010); // This line is only for the first time to set the correct date } void loop() { Serial.print("Time "); Serial.println(rtc.getTimeStr()); Serial.println(" "); Serial.println("Day of Week"); Serial.println(rtc.getDOWStr(FORMAT_SHORT)); Serial.println(" "); Serial.print("Date "); Serial.println(rtc.getDateStr()); Serial.println(" "); // Wait one second before repeating :) delay (1000); }
Greetings! Very helpful advice in this particular article!
It’s the little changes that will make the most significant
changes. Many thanks for sharing!