DS1307 Raspberry Pi RTC:
DS1307 Raspberry Pi RTC– This article describes how to connect an external RTC real time clock DS1307 to the Raspberry Pi. The Raspberry Pi itself does not have a hardware clock and receives the current time over NTP (Network Time Protocol). There are situations when you need to display the current time using Raspberry Pi without internet access. Such a clock is called Hardware RTC (Hardware Real Time Clock). The DS1307 RTC module is recommended here. This component is again in several versions. Before I am going to explain how to interface the DS1307 with Raspberry Pi first let’s take a look at the DS1307 Real Time Clock Pinout and technical specifications.
Amazon Purchase Links:
Other Tools and Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
DS1307 RTC I2C supported Real Time Clock:
- The module comes fully assembled and pre-programmed with the current time.
- The DS1307 is accessed via the I2C protocol.
- Two wire I2C interface. Hour:Minutes : Seconds AM/PM.
- Day Month, Date – Year. 1Hz output pin. Note: Battery is not include
- 56 Bytes of Non-volatile memory available to user.
The DS1307 serial real-time clock (RTC) is a low power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2 C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date 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 AM/PM indicator. The DS1307 has a built-in power-sense circuit that detects power failures and automatically switches to the backup supply. Timekeeping operation continues while the part operates from the backup supply.
The DS1307 chip installed on such modules requires a supply voltage of 5V. That is actually not a problem there are two small pull-up resistors that pull SDA and SCL to the 5V level (see Figure 1). You have to remove this (see Figure 2)!
As you have already read several times, the Raspberry Pi does 5V on its GPIO pins not good at all. Don’t worry, the I2C lines are still connected with pull-up resistors, namely the internal resistors of the Raspberry Pi, which pull the level of the I2C lines to a healthy 3.3V. With soldering iron and tweezers remove the two resistors on the RTC module.
DS1307 Hardware Setup and connection with Raspberry Pi:
- Obtain a DS1307 board or make one yourself (it’s very straightforward).
- Ensure that the I2C data and clock lines (SDA and SCL) does not have any pull-up resistors. If any pull-ups are present, remove them.
- Connect a battery (most boards use CR2032 Lithium Cells) to the DS1307.
- Make the connections to the Raspberry Pi by following the circuit diagram given below.
As soon as the real-time clock is connected, the drivers are loaded in the operating system of the Raspberry Pi. For the first test, make the settings by hand i.e enable I2C on the Raspberry Pi. At the end of this section you will learn how all the necessary settings have already been made can be done automatically during the boot process.
Make sure that you have activated the I2C interface and installed the i2c tools (read my article on Raspberry Pi I2C). Now run the command for in the terminal Detection of connected I2C hardware from:
i2cdetect -y 1
You should now see a table in which the value 68 is entered. If additional I2C devices are connected, additional Addresses are displayed. However, 68 is the address of the real-time clock. Now create a device tree overlay in the file /boot/config.txt. Put To do this, enter the following line at the end of the file:
dtoverlay = rtc-i2c, RTC type
RTC type stands for the type of real-time clock you are using. Replace the placeholder according to your RTC type These RTC models are possible:
As an example: dtoverlay = rtc-i2c, ds1307
Next, enter the device as a virtual file:
sudo echo ds1307 0x68> / sys / class / i2c – adapter / i2c -1 / new_device
If you are still using the old Raspberry Pi version Rev 1, change the penultimate folder of the line in i2c-0. All settings have now been completed, and the clock can be read:
sudo hwclock -r
You should now see a date similar to the line below:
Sa 01 Jan 2000 01:00:05 CET
Of course the date is wrong. Therefore, the clock must be set in the next step. It is best to establish an Internet connection via LAN or WLAN. Now call up the current system time with the date command. With the command sudo hwclock -w you write the current time in the real-time clock. Another query with sudo hwclock -r then shows the current date. If everything works fine, you can take one more step, so that all settings are loaded directly during the boot process and the system clock is the RTC is adjusted. To do this, open the file /etc/rc.local/:
sudo nano /etc/rc.local
Now write the following lines at the end of the file, but before exit 0:
echo ds1307 0 x68> / sys / class / i2c – adapter / i2c -1 / new_device
sudo hwclock -s
Now save and close the file. From now on there is no more internet connection necessary to set the system time of the Raspberry Pi to the current time. The system time of the real-time clock is now adjusted to each restart. Of course, this only happens if you are using an RTC module with battery. So, if your DS1307 RTC real time clock has the battery, you won’t lose the current date and time information. But if incase there is no battery then you will have to manually adjust the current date and time, which can be quite frustrating at times.