Learn interfacing DS3231 Real Time Clock in Raspberry Pi

Learn the working of DS3231 Real Time Clock in Raspberry Pi.  Raspberry Pi only shows correct time when it is connected to the internet as it doesn’t have battery powered RTC. DS3231 RTC module is used to provide real-time to raspberry even when it is not connected to the internet. So, let’s start.

Operating Voltage: 3.3 – 5V
Current: 15mA
Accuracy: ±2ppm at 0°C to +40°C and ±3.5ppm at -40°C to +85°C
Digital Temp Sensor Output: ±3°C Accuracy
Alarms: Two times of a day
Battery Backup for continuous timekeeping

Step 1: Required Components

DS3231 RTC module x 1
Raspberry Pi x 1
Jumper Wires

Step 2: Circuit Time

Make circuit as per given diagram. Six pins are available in RTC module.  We will be using only four pins. Connect VCC to 3.3V and GND pin to Ground of Raspberry Pi. Now, connect SDA pin to SDA pin of Raspberry Pi (GPIO3) and connect SCL pin to SCL pin of Raspberry Pi (GPIO2).

Circuit of DS3231 Real Time Clock in Raspberry Pi

Step 3: Code Time

Follow the below given steps to add DS3231 Real Time Clock in Raspberry Pi.

  1. Open /boot/config.txt by typing the following in terminal
    sudo nano /boot/config.txt
  2. Edit the file to add device tree overlay for RTC.
    dtoverlay=i2c-rtc,ds1307
  3. Now, reboot your Raspberry Pi and detect the i2c device.
    sudo reboot
    sudo i2cdetect -y 1
    You should see UU at 68.
  4. Disable the fake hardware clock
    sudo apt-get -y remove fake-hwclock
    sudo update-rc.d -f fake-hwclock remove
  5. To start original hardware clock, comment following lines in a file located at:
    sudo nano /lib/udev/hwclock-set
    The to comment are:
    #if [ -e /run/systemd/system ] ; then
    # exit 0
    #fi
  6. Now, connect your Raspberry Pi to the internet and sync the proper time according to your time zone.
  7. Use the following command to Write time from Raspberry Pi to RTC module.
    sudo hwclock -w
  8. Use the following command to Read time from RTC.
    sudo hwclock -r

Step 4: Run code to Raspberry Pi

Make the circuit as per given diagram and perform instructions given in Code Time. By performing write command RTC will be initialized with the time given by Raspberry Pi. To get the time to RTC use commandsudo hwclock -r.

For Basic hardware details about Raspberry PI 3, Click here

Leave a Reply