Complete guide to setup automatic time with USB RTC

Introduction: Real Time Clock for Rapsberry Pi

USB RTC or Real Time Clock is an open-source electronic device that measures the time of the device which aligns the time of the device with the “Real-Time”. In this guide, we’ll cover the topics “Automatic Time Setup” and “Automatic Time Sinking” that would ease the process for the users. USB RTC Auto sync time on raspberry pi

One of the things that came as an obstacle for the user is the lack of a system to update the “Real-Time” or current time in the system without using the internet. We came up with a device “USB RTC For Raspberry Pi” that would not only solve the problem but also make the product more efficient.   

What is USB RTC?

USB RTC is an open source real time clock device that comprises MCP2221, a USB-to-UART/I2C serial converter, which enables USB connectivity, in the processes that include a USB, UART(Serial), GPIO, Battery holder (CR1220 recommended) and I2C interfaces. It runs on Hi-tech DS3231 RTC chip, an accurate I2C, runs the USB RTC with extremely high efficiency at a low cost.

USB RTC for Raspberry Pi has cross platform support which makes it enabled to work on different platforms like Windows, MAC, etc. Save energy and increase the battery life with the USB RTC for Raspberry Pi. Even though microcontrollers with built-in RTCs go into sleep or on low-power modes, the clock must continue to run to provide accurate timekeeping for things like alarm functions.

Setup and Test USB RTC

USB RTC is designed to perform the tasks with no complicated wiring as it is a plug and play device. The test for working of library of USB RTC will be done as per the below commands : 

  • Install MCP2221 Library by running below command on terminal:

         sudo pip3 install PyMCP2221AUSB RTC Library Installation

  • Connect USB-RTC on the USB Port of Raspberry Pi.
  • Now clone/download USB-RTC Github Repository by running below command:

         git clone repo USb RTC

  • Now enter downloaded folder from home/pi or by running below command:

         cd USB-RTC

  • Now run file by running below command:

         sudo python3

  • After running above commands, you will get the below output(Module is running or not)Run Test code USB RTC
  • To modify Date & Time, you have to change the last value of each line in the hexadecimal form inside def SetTime(address) For Example : 

         #sec min hour week day month year

         NowTime = [0x00,0x00,0x18,0x04,0x12,0x08,0x15]  #Edit   this line to set RTC Date and Time

  • To read time, you can call "ds3231ReadTime()" Function
  • To read the internal temperature of USB-RTC, uncomment below the line by removing from start and end of the line :

         '''Celsius = getTemp(address)

            Fahrenheit = 9.0/5.0 * Celsius + 32

            print (Fahrenheit, "*F /", Celsius, "*C") '''

Keeping your Raspberry Pi on time with USB RTC

The Raspberry Pi does not have a battery-powered clock on it’s own due to which it does not show real time. A NTP daemon keeps the clock accurate while connected to the internet but if there is no internet for the device, the clock of the Rpi will not run accurately. The USB RTC came as a solution to provide the real-time clock system for the Rpi, which keeps the time even when the Raspberry is turned off.

Components Required

  • Raspberry Pi(With Rpi OS)
  • Battery CR1220(Coin-Cell)

Library Installation

  • Install MCP2221 Library by running below command on terminal:

         sudo pip3 install PyMCP2221A

  • Now clone/download USB-RTC Github Repository by running below command:

         git clone

  • Insert CR1220 Battery in the battey holder of USB RTC
  • Now connect USB RTC on the USB port of the RPi

Note : First the user need to setup the USB RTC time manually by the use of a python script ( ,after that user need to run the script ( using a file(rc.local)  after the boot process completion of Rpi. To know more about the above mentioned process in details kindly read below.

Set Time of USB RTC :

  • Open File in any editor, and edit the below line to set date and time of the USB RTC

         NowTime = [0x00,0x00,0x18,0x04,0x12,0x08,0x15]  #Edit   this line to set RTC Date and Time

For example - 2021/10/19, 17:15:00,Tuesday will be written as 

NowTime = [0x00,0x15,0x17,0x03,0x19,0x10,0x21]

Where as : Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday will be in order of {1,2,3,4,5,6,7}

Now save and run the above file to set the date and time of USB RTC. 

Fetch Time from USB RTC

  • First disable the NTP(Network Time Protocol) of Rpi so it can allow Rpi to fetch date from USB RTC.

         sudo timedatectl set-ntp false Disable NTP Raspberry Pi

  • In case, you want to make it back to the default time, run below command

         sudo timedatectl set-ntp true

  • Now run the below command to open rc.local file to run the USB RTC script on Rpi boot 

sudo nano /etc/rc.localRC Local file of Rapsberry Pi

  • After this, you will enter the nano file editor and here we have to add a command to execute our python program. Add the complete file execution line before ‘exit 0’ line at the end. After the editing, save the file and exit. For exit in nano type Ctrl x and for saving the file type Y and press enter. 
  • Add the below line in the rc.local file

sudo python3 /home/pi/USB-RTC/Set_Time_Boot.pySet File Path on Boot

  • Now save the file and reboot the system by running the below command

sudo rebootReboot File

After reboot of RPi, the time on the RPi will be automatically adjusted as per the USB RTC module.

Leave a comment

All comments are moderated before being published