Raspberry Pi RTC Tutorial: Using DS1307 and DS3231 RTCs with Raspberry Pi
Do you want to maintain hours, minutes, and seconds, as well as, day, month, and year information for your Raspberry Pi Project? Well then using an RTC (Real Time Clock), will be for you!
Through this blog, you will learn how to use an RTC module with your Raspberry in a few easy steps!
What is an RTC (Real-Time Clock)?
RTC stands for real-time clock and it refers to a clock, usually in the form of an integrated circuit that tracks the current time with seconds, minutes, hours, day, date, month, and year information. It can be used to execute various actions at a specific time.
Why do you need an RTC for your Raspberry Pi?
Raspberry Pi and almost all other microcontrollers have built-in timers, they can run as long as there is power. However, once the power is turned off (either manually or due to a power failure), all timers will be reset to 0.
It is acceptable to use the internal timer for timing for simple projects if you don’t mind when the timer resets to zero once there is no power. But for projects such as data loggers, clocks, alarms, which require the timer to run independently of the external power supply, you need to use an RTC.
By using an RTC with your Raspberry Pi, you can continue to keep track of the time even if you need to reprogram your Raspberry Pi or disconnect it from the main power.
Almost all RTC are low-current built-in ICs and able to run on a single lithium cell battery for many years.
Raspberry Pi RTC modules offered by Seeed
RTC (DS1307) for Raspberry Pi
This is based on the clock chip DS1307 and it can provide a real-time clock(RTC) for Raspberry Pi Zero, 2, 3B/B+, 4 via the I2C interface. The real-time clock of this module counts seconds, minutes, hours, date of the month, month, day of the week, and year with leap-year compensation valid up to 2100. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator.
Also, it has a 56-byte, battery-backed, nonvolatile (NV) RAM for data storage and has a programmable square-wave output signal. If you want to keep this module timing when the Raspberry Pi is powered off, you need to insert a 3-Volt CR1225 lithium cell into the battery-holder. This is equipped with an automatic power-fail detect and switch circuit.
High Accuracy RTC (DS3231) for Raspberry Pi
The High Accuracy Pi RTC is based on the clock chip DS3231. The DS3231 is a low-cost, extremely accurate I2C realtime clock (RTC). It provides an RTC for Raspberry Pi via the I2C interface. With the clock source from the TCXO(temperature compensated crystal oscillator), the RTC maintains seconds, minutes, hours, day, date, month, and year information.
The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator.
The clock provides two programmable time-of-day alarms and programmable square-wave output. The INT/SQW pin either generates an interrupt due to alarm condition or outputs a square-wave signal and the selection is controlled by the bit INTCN.
If you want to retain the time information even when the Raspberry Pi is powered off, you need to insert a 3V CR1225 lithium cell into the battery holder.
What’s the difference between DS1307 and DS3231 RTC?
Now that we have discussed about the RTCs with clock chips DS1307 and DS3231, let’s understand the difference between them.
Generally, RTC modules such as the DS1307 are equipped with an external 32kHz crystal for time-keeping. However, the issue with crystals is that their oscillation frequency is affected by external temperature. This results in the clock being off by around 5 minutes per month. Even though this is a small change, it is not recommended to neglect it.
In order to minimize these effects, we can use an RTC based on the DS3231 clock chip. But how can it solve the above issue? Well. It has a 32kHz temperature compensated crystal oscillator (TCXO) and this is not affected by external temperature changes.
This sensor compensates frequency changes as the name suggests, by adding or removing clock ticks to make sure the timekeeping stays on track. Overall, this kind of RTC provides a much stable and accurate reference clock and maintains the accuracy of the RTC to around 2 minutes per year.
Tutorial: Using DS1307 and DS3231 RTC with Raspberry Pi
Now let’s look at how these RTC modules can be used with a Raspberry Pi. Even though we have introduced two different RTC modules, the steps to set them up will be quite similar.
What do you need?
Step 1. Driver Installation
Type the following command in your Raspberry Pi terminal to clone the pi-hats repository
git clone https://github.com/Seeed-Studio/pi-hats.git
When the download is finished, type the following command in your terminal.
Install the drivers according to your RTC.
- If you have a DS1307, type the following
sudo ./install.sh -u rtc_ds1307
- If you have a DS3231 type the following
sudo ./install.sh -u rtc_ds3231
Step 2. Power off Raspberry Pi
sudo shutdown -h now
Step 3. Insert the RTC HAT to Raspberry Pi
Please make sure to insert the pin 1 of this HAT into the pin 1 of raspberry GPIO, just like the picture above.
Step 4. Power up the Raspberry Pi
Using the RTC with Raspberry Pi
Now you can use the following command to check whether the driver is installed successfully.
If you want to uninstall the driver, you can use the command below
sudo ./install.sh -u
Now let’s see what the RTC module can do:
- Read hardware clock and print result
sudo hwclock -r
- Set the system time from the hardware clock
sudo hwclock -s
- Set the hardware clock from the current system time
sudo hwclock -w
- Access help for more usage
If you are interested to use RTCs with Arduino boards, check our blog Arduino RTC Tutorial: Using DS1307 RTC with Arduino
We believe that you got a clear understanding of using RTC modules with your Raspberry Pi for your timekeeping projects and we hope to see you build your next awesome Raspberry Pi project combined with an RTC module!