Keeping time in all your projects!

Introduction

One of the essential aspects of many Raspberry Pi projects is time management. Whether you want to create an alarm clock, a data logger or any other device requiring precise time synchronization, using an RTC (Real Time Clock) module is the ideal solution.

The RTC module is fitted with a button cell that enables it to continue calculating the time even when the Raspberry Pi is switched off, so that the correct time is maintained when the Pi is restarted.

In this course, we’ll explore the DS1307 RTC module for Raspberry Pi. This module uses the Raspberry’s I2C to transmit these values.

Here’s a diagram of how to connect the DS1307 to the Raspberry Pi:

Programming

One of the essential aspects of many Raspberry Pi projects is time management. Whether you want to create an alarm clock, a data logger or any other device requiring precise time synchronization, using an RTC (Real Time Clock) module is the ideal solution.

Here’s what you get when you type this command. Our RTC module has i2c address 0x068. It appears with I2Cdetect, which means it’s connected to our Raspberry Pi :

If you get an error when typing i2cdetect, it means that i2cdetect is not enabled in the Raspberry pi configuration. In our course on i2c on the Raspberry we explain how to activate it.

Once I2C has been activated and your PSTN module has been detected by the board, we’ll move on to the time-reading program. The first step is to install the library needed to manage the I2C:

Next, we’ll create a python file in which to write our program:

Here’s the program to read the time from the module:

To run the program, use the following command line:

As you can see, the time is not necessarily correct. So we’re going to have to configure it correctly. Here’s the program to modify the time:

You need to replace the time in “new time” with your own, so that it’s correct. Then restart the first program to see the correct time displayed.