Learn interfacing DS3231 Real Time Clock in Raspberry Pi

Learn the working of DS3231 Real Time Clock in Raspberry Pi.  Raspberry Pi only shows the 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 the circuit as per the given diagram. Six pins are available in the RTC module.  We will be using only four pins. Connect VCC to 3.3V and GND pin to Ground of Raspberry Pi. Now, connect the SDA pin to the SDA pin of Raspberry Pi (GPIO3) and connect the SCL pin to the 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 a 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 the 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 the 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 the RTC module.
      sudo hwclock -w
  1. Use the following command to Read time from RTC.
    sudo hwclock -r

Step 4: Run code to Raspberry Pi

Make the circuit as per the 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.

Learn basic hardware information of Raspberry Pi 3.

Leave a Reply

Your email address will not be published. Required fields are marked *