{"id":163509,"date":"2025-01-16T14:15:23","date_gmt":"2025-01-16T14:15:23","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=163509"},"modified":"2025-01-16T15:36:42","modified_gmt":"2025-01-16T15:36:42","slug":"raspberry-pi-pico-ds1307-rtc-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds1307-rtc-micropython\/","title":{"rendered":"Raspberry Pi Pico: DS1307 RTC Module &#8211; Keep Track of Time (MicroPython)"},"content":{"rendered":"\n<p>In this guide you&#8217;ll learn how to interface a DS1307 RTC module with the Raspberry Pi Pico programmed with MicroPython. We&#8217;ll cover the basic functioning of the module, how to connect it to the Pico, how to set and keep track of its time. Finally, we&#8217;ll create a simple digital clock with an OLED display.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico: DS1307 RTC Module - Keep Track of Time (MicroPython)\" class=\"wp-image-163568\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Table of Contents:<\/strong><\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll cover the following subjects:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#intro-rtc\" title=\"\">Introducing Real-Time Clock (RTC) Modules<\/a><\/li>\n\n\n\n<li><a href=\"#DS1307-intro\" title=\"\">Introducing the DS1307 RTC Module<\/a><\/li>\n\n\n\n<li><a href=\"#ds1307-wiring-RPi-Pico\" title=\"\">Connecting the DS1307 RTC Module to the Pico<\/a><\/li>\n\n\n\n<li><a href=\"#urtc-library\" title=\"\">MicroPython Library for RTC Modules<\/a><\/li>\n\n\n\n<li><a href=\"#sync-rtc-module\" title=\"\">Synchronizing the RTC Module<\/a><\/li>\n\n\n\n<li><a href=\"#getting-time\" title=\"\">Getting Time from the RTC Module (display on OLED)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites &#8211; MicroPython Firmware<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board. <\/p>\n\n\n\n<p>The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/#install-thonny-ide\" title=\"\">Programming Raspberry Pi Pico using MicroPython<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"intro-rtc\">Real-Time Clock (RTC) Modules<\/h2>\n\n\n\n<p>To keep track of time when your Raspberry Pi Pico is not connected to your computer or to the internet, we can use Real-Time Clock (RTC) modules.<\/p>\n\n\n\n<p>RTC modules, such as the DS3231 and DS1307, have their own tiny clock inside to keep track of time by themselves. Usually, they come with a battery holder to connect a battery so that they keep working even if the Raspberry Pi Pico resets or loses power.<\/p>\n\n\n\n<p>The DS3231 and the DS1307 are some of the most popular choices to use with microcontrollers. Both are compatible with the Raspberry Pi Pico and communicate via I2C communication protocol. The DS3231 is more accurate because it comes with a temperature sensor and gives temperature-compensated results. Nonetheless, the DS1307 is also accurate and suitable for most applications that need to keep track of time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"DS1307-intro\">Introducing the DS1307 RTC Module<\/h2>\n\n\n\n<p>Throughout this tutorial, we\u2019ll be using the DS1307 RTC Module, but if you have a DS3231, most of the information provided applies to both modules. Additionally, all the code should be compatible with both modules with just a small change.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-RTC-Module.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS1307 RTC Module\" class=\"wp-image-163510\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-RTC-Module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-RTC-Module.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The DS1307 RTC Module comes with the DS1307 chip (to keep track of time) and the AT24C32 EEPROM (to save data permanently). It can also be programmed to output square waves with different frequencies. We\u2019ll only use the DS1307 chip features for timekeeping.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DS1307 Battery Holder<\/h3>\n\n\n\n<p>It comes with a battery holder to connect a CR2032 battery to keep accurate timekeeping. In the event of a power outage, it can still keep track of time accurately.<\/p>\n\n\n\n<p>This module also comes with the option to connect a DS18B20 temperature. After connecting that sensor to the module, you can get the temperature from the module DS pin.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder-2.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS1307 RTC Real Time Clock Battery Holder\" class=\"wp-image-163511\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder-2.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder-2.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DS1307 RTC Real Time Clock Battery Holder with Battery\" class=\"wp-image-163512\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/DS1307-battery-holder.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">DS1307 RTC Module I2C Address<\/h3>\n\n\n\n<p>By default, the address of the DS1307 RTC is <strong>0x68<\/strong> and the EEPROM connected to the module is 0x50. You can run an I2C scanner sketch to double-check these values:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-i2c-scanner-micropython\/\" title=\"\">Raspberry Pi Pico: I2C Scanner (MicroPython) \u2013 Finding the Address of I2C Devices<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">DS1307 RTC Module Pinout<\/h3>\n\n\n\n<p>The following table quickly describes the DS1307 RTC Module Pinout.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>SQ<\/strong><\/td><td>Output for square waves (we won\u2019t use)<\/td><\/tr><tr><td><strong>DS<\/strong><\/td><td>Output for temperature readings if DS18B20 is connected (we won\u2019t use)<\/td><\/tr><tr><td><strong>SCL<\/strong><\/td><td>SCL pin for I2C<\/td><\/tr><tr><td><strong>SDA<\/strong><\/td><td>SDA pin for I2C<\/td><\/tr><tr><td><strong>VCC<\/strong><\/td><td>Provides power to the module (3.3V or 5V)<\/td><\/tr><tr><td><strong>GND<\/strong><\/td><td>GND<\/td><\/tr><tr><td><strong>BAT<\/strong><\/td><td>Backup supply input (we won\u2019t use) or use the default battery holder<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ds1307-wiring-RPi-Pico\">Connecting the DS1307 RTC Module to the Pico<\/h2>\n\n\n\n<p>Here\u2019s a list of the parts required for this tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/real-time-clock-module-ds1307\/\" target=\"_blank\" rel=\"noopener\" title=\"\">DS1307 RTC Module<\/a> or DS3231 Module<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/oled-display-128x64-0-96-inch\/\" target=\"_blank\" rel=\"noopener\" title=\"\">SSD1306 OLED Display<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Jumper wires<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Breadboard<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<p>We\u2019ll only use the I2C and power pins to interface the RTC module with the Raspberry Pi Pico. We\u2019ll connect SCL to GPIO 5 and SDA to GPIO 4. You can use any other suitable I2C pins, as long as you change them on the code. You can use the following table as a reference or take a look at the schematic diagram.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>DS1307 RTC Module<\/strong><\/td><td><strong>Raspberry Pi Pico<\/strong><\/td><\/tr><tr><td>SCL<\/td><td>GPIO 5<\/td><\/tr><tr><td>SDA<\/td><td>GPIO 4<\/td><\/tr><tr><td>VCC<\/td><td>3V3(OUT)<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-wiring.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"391\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-wiring.png?resize=969%2C391&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico DS1307 RTC Module Wiring\" class=\"wp-image-163513\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-wiring.png?w=969&amp;quality=100&amp;strip=all&amp;ssl=1 969w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-wiring.png?resize=300%2C121&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-wiring.png?resize=768%2C310&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 969px) 100vw, 969px\" \/><\/a><\/figure><\/div>\n\n\n<p><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\" title=\"\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"urtc-library\">MicroPython Library for RTC Modules<\/h2>\n\n\n\n<p>To make it easier to program the Raspberry Pi Pico to interface with the RTC module, we\u2019ll use the library that can be found in the following link.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/refs\/heads\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/urtc.py\" target=\"_blank\" rel=\"noopener\" title=\"\">Click here to download the <em>urtc.py<\/em> library<\/a><\/strong><\/li>\n<\/ul>\n\n\n\n<p>This library is compatible with both the DS1307 and DS3231 RTC modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Uploading the <em>urtc.py<\/em> Library<\/h3>\n\n\n\n<p>Upload the library to your Raspberry Pi Pico by following the next steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/refs\/heads\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/urtc.py\" target=\"_blank\" rel=\"noopener\" title=\"\">Click here to download the <em>urtc.py<\/em>&nbsp;library code.<\/a><\/strong><\/li>\n\n\n\n<li>Create a new file in Thonny IDE and copy the library code.<\/li>\n\n\n\n<li>Go to&nbsp;<strong>File<\/strong>&nbsp;&gt;&nbsp;<strong>Save&nbsp;as<\/strong>&nbsp;and select&nbsp;<strong>Raspberry Pi Pico<\/strong>.<\/li>\n\n\n\n<li>Name the file&nbsp;<em><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/refs\/heads\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/urtc.py\" target=\"_blank\" rel=\"noopener\" title=\"\">urtc.py<\/a><\/em>&nbsp;and click&nbsp;<strong>OK<\/strong>&nbsp;to save the file on the Raspberry Pi Pico.<\/li>\n<\/ol>\n\n\n\n<p>And that\u2019s it. The library was uploaded to your board. Now, you can use the library functionalities in your code by importing the library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sync-rtc-module\">Synchronizing the RTC Module<\/h2>\n\n\n\n<p>The first thing you should do when using an RTC module in your projects is to synchronize the RTC time with your local time. For that, you can use the following code:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-ds1307-rtc-micropython\/\n\nimport time\nimport urtc\nfrom machine import I2C, Pin\n\ndays_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\n\n# Initialize RTC (connected to I2C)\ni2c = I2C(0, scl=Pin(5), sda=Pin(4))\nrtc = urtc.DS1307(i2c)\n\n# Set the current time using a specified time tuple\n# Time tupple: (year, month, day, day of week, hour, minute, seconds, milliseconds)\n#initial_time = (2024, 1, 30, 1, 12, 30, 0, 0)\n\n# Or get the local time from the system\ninitial_time_tuple = time.localtime() #tuple (microPython)\ninitial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds\n\n# Convert to tuple compatible with the library\ninitial_time = urtc.seconds2tuple(initial_time_seconds)\n\n# Sync the RTC\nrtc.datetime(initial_time)\n\nwhile True:\n    current_datetime = rtc.datetime()\n    print('Current date and time:')\n    print('Year:', current_datetime.year)\n    print('Month:', current_datetime.month)\n    print('Day:', current_datetime.day)\n    print('Hour:', current_datetime.hour)\n    print('Minute:', current_datetime.minute)\n    print('Second:', current_datetime.second)\n    print('Day of the Week:', days_of_week[current_datetime.weekday])\n\n    time.sleep(1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/sync_rtc_time.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Let\u2019s take a quick look at the relevant parts of this code.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Import Libraries<\/h4>\n\n\n\n<p>First, you need to import the <span class=\"rnthl rntliteral\">urtc<\/span> module you\u2019ve imported previously that contains the functions to interact with the RTC. You also need to import the <span class=\"rnthl rntliteral\">Pin<\/span> and <span class=\"rnthl rntliteral\">I2C<\/span> classes to establish an I2C communication with the module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import time\nimport urtc\nfrom machine import I2C, Pin<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize the RTC Module<\/h4>\n\n\n\n<p>Then, initialize I2C communication and create an object called <span class=\"rnthl rntliteral\">rtc<\/span> to refer to our DS1307 RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>i2c = I2C(0, scl=Pin(5), sda=Pin(4))\nrtc = urtc.DS1307(i2c)<\/code><\/pre>\n\n\n\n<p>If you\u2019re using a DS3231, you should replace the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>rtc = urtc.DS1307(i2c)<\/code><\/pre>\n\n\n\n<p>With:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>rtc = urtc.DS3231(i2c)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Synchronize the Time<\/h4>\n\n\n\n<p>To synchronize the RTC time, you must use the <span class=\"rnthl rntliteral\">datetime()<\/span> method on the <span class=\"rnthl rntliteral\">rtc<\/span> object and pass as an argument a time tuple with the following format:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>(year, month, day, day of week, hour, minute, seconds, milliseconds)<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> this tuple is different from the one used by the MicroPython time module.<\/p>\n\n\n\n<p>We can synchronize the RTC with the system\u2019s local time.<\/p>\n\n\n\n<p>First, we get the time tuple of the local time using <span class=\"rnthl rntliteral\">time.localtime()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>initial_time = urtc.seconds2tuple(initial_time_seconds)<\/code><\/pre>\n\n\n\n<p>The tuple returned is different from the one used by the RTC module.<\/p>\n\n\n\n<p>So, first, we convert it to seconds using <span class=\"rnthl rntliteral\">time.mktime()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>initial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds<\/code><\/pre>\n\n\n\n<p>And finally, we convert it to a tuple that is compatible with the library using the <span class=\"rnthl rntliteral\">seconds2tuple()<\/span> function from the <span class=\"rnthl rntliteral\">urtc<\/span> library that accepts as an argument the number of seconds since epoch for the local time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>initial_time = urtc.seconds2tuple(initial_time_seconds)<\/code><\/pre>\n\n\n\n<p>Finally, we can pass our <span class=\"rnthl rntliteral\">initial_time<\/span> variable that contains the local time in a tuple compatible with the RTC library to the <span class=\"rnthl rntliteral\">datetime()<\/span> function as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>rtc.datetime(initial_time)<\/code><\/pre>\n\n\n\n<p>After this line of code, the RTC module is synchronized with your local time and you can simply call <span class=\"rnthl rntliteral\">rtc.datetime()<\/span> to get the current time from the RTC. This returns an object with all the time elements.<\/p>\n\n\n\n<p>To get and print each time element, we can do as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>current_datetime = rtc.datetime()\nprint('Current date and time:')\nprint('Year:', current_datetime.year)\nprint('Month:', current_datetime.month)\nprint('Day:', current_datetime.day)\nprint('Hour:', current_datetime.hour)\nprint('Minute:', current_datetime.minute)\nprint('Second:', current_datetime.second)\nprint('Day of the Week:', days_of_week&#091;current_datetime.weekday])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Run the Code<\/h3>\n\n\n\n<p>Run this previous code to synchronize the RTC time with the local time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"114\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=470%2C114&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Run script Thonny IDE MicroPython\" class=\"wp-image-144594\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?w=470&amp;quality=100&amp;strip=all&amp;ssl=1 470w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/12\/thonny-ide-run-button.png?resize=300%2C73&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n<p>From now on, if the RTC has an attached battery, it will keep the time synchronized with your local time. So, you don\u2019t need to synchronize it anymore, and you can call <span class=\"rnthl rntliteral\">rtc.datetime()<\/span> to get the current time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-time\">Getting Time from the RTC Module<\/h2>\n\n\n\n<p>In this section, we provide an example that gets the time from the RTC module (that should already be synchronized after running the previous example) and displays it on an OLED display.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-clock-OLED.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Digital Clock with OLED and RTC Module\" class=\"wp-image-163567\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-clock-OLED.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-clock-OLED.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Make sure you\u2019ve run the example of the previous section first to synchronize the RTC. Also, don\u2019t forget that you need the <a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP-MicroPython\/master\/code\/Others\/OLED\/ssd1306.py\" target=\"_blank\" rel=\"noopener\" title=\"\"><em>ssd1306.py<\/em> module<\/a> imported to your Raspberry Pi Pico. Follow the next instructions to upload the ssd1306.py module to your board.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a new file in Thonny IDE and copy the library code. The&nbsp;<a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/ESP-MicroPython\/master\/code\/Others\/OLED\/ssd1306.py\" target=\"_blank\" rel=\"noreferrer noopener\">OLED library code can be found here<\/a>.<\/li>\n\n\n\n<li>Go to&nbsp;<strong>File&nbsp;<\/strong>&gt;&nbsp;<strong>Save&nbsp;<\/strong>as and select&nbsp;<strong>Raspberry Pi Pico<\/strong>.<\/li>\n\n\n\n<li>Name the file&nbsp;<em>ssd1306.py<\/em>&nbsp;and click&nbsp;<strong>OK&nbsp;<\/strong>to save the file on the Raspberry Pi Pico.<\/li>\n<\/ol>\n\n\n\n<p class=\"rntbox-rntclgreen\">Recommended reading: <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ssd1306-oled-micropython\/\" title=\"\">Raspberry Pi Pico: SSD1306 OLED Display (MicroPython)<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Wiring the Circuit<\/h3>\n\n\n\n<p>We\u2019ll connect the OLED display to GPIO 0 (SDA) and GPIO 1 (SCL\/SCK). We\u2019ll use GPIO 4 (SDA) and GPIO 5 (SCL) to connect the RTC. You can use the following table or diagram as a reference.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><\/td><td><strong>SDA<\/strong><\/td><td><strong>SCL\/SDK<\/strong><\/td><\/tr><tr><td><strong>OLED Display<\/strong><\/td><td>GPIO 0<\/td><td>GPIO 1<\/td><\/tr><tr><td><strong>RTC Module<\/strong><\/td><td>GPIO 4<\/td><td>GPIO 5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-OLED_bb-Copy.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"781\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-OLED_bb-Copy.png?resize=711%2C781&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico with RTC and OLED Display\" class=\"wp-image-163564\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-OLED_bb-Copy.png?w=711&amp;quality=100&amp;strip=all&amp;ssl=1 711w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-RTC-OLED_bb-Copy.png?resize=273%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 273w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>Upload the following code as <span class=\"rnthl rntliteral\"><em>main.py<\/em><\/span> to your Raspberry Pi Pico.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n# Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-ds1307-rtc-micropython\/\n\nimport time\nimport urtc\nfrom machine import SoftI2C, Pin\nimport ssd1306\n\ndays_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\n\n# Initialize RTC Module\ni2c_rtc = SoftI2C(scl=Pin(5), sda=Pin(4))\nrtc = urtc.DS1307(i2c_rtc)\n\n# Initialize OLED \noled_width = 128\noled_height = 64\ni2c_oled = SoftI2C(scl=Pin(0), sda=Pin(1))\noled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)\n\nwhile True:\n    # Get current time from the RTC\n    current_datetime = rtc.datetime()\n\n    # Format the date and time as strings\n    formatted_date = '{:02d}-{:02d}-{:04d}'.format(current_datetime.day, current_datetime.month, current_datetime.year)\n    formatted_time = '{:02d}:{:02d}:{:02d}'.format(current_datetime.hour, current_datetime.minute, current_datetime.second)\n    formatted_day_week = days_of_week[current_datetime.weekday]\n\n    # Clear the OLED display\n    oled.fill(0)\n\n    # Display the formatted date and time\n    oled.text('Date: ' + formatted_day_week, 0, 0)\n    oled.text(formatted_date, 0, 16)\n    oled.text('Time: ' + formatted_time, 0, 32)\n    oled.show()\n\n    # Print the formatted date and time to the shell\n    print('Formatted date:', formatted_date)\n    print('Formatted time:', formatted_time)\n\n    # Wait for 1 second\n    time.sleep(1)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Raspberry-Pi-Pico\/MicroPython\/get_time_rtc_module.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Does the Code Work?<\/h3>\n\n\n\n<p>First, we import the required libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import time\nimport urtc\nfrom machine import SoftI2C, Pin\nimport ssd1306<\/code><\/pre>\n\n\n\n<p>We create an array with the days of the week.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>days_of_week = &#091;'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']<\/code><\/pre>\n\n\n\n<p>Initialize the RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Initialize RTC Module\ni2c_rtc = SoftI2C(scl=Pin(5), sda=Pin(4))\nrtc = urtc.DS1307(i2c_rtc)<\/code><\/pre>\n\n\n\n<p>Initialize the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Initialize OLED \noled_width = 128\noled_height = 64\ni2c_oled = SoftI2C(scl=Pin(0), sda=Pin(1))\noled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c_oled)<\/code><\/pre>\n\n\n\n<p>Get the current date and time from the RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>while True:\n    # Get current time from the RTC\n    current_datetime = rtc.datetime()<\/code><\/pre>\n\n\n\n<p>Extract the date and time from the <span class=\"rnthl rntliteral\">current_datetime<\/span> variable and format them as strings. Also, get the day of the week.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Format the date and time as strings\nformatted_date = '{:02d}-{:02d}-{:04d}'.format(current_datetime.day, current_datetime.month, current_datetime.year)\nformatted_time = '{:02d}:{:02d}:{:02d}'.format(current_datetime.hour, current_datetime.minute, current_datetime.second)\nformatted_day_week = days_of_week&#091;current_datetime.weekday]<\/code><\/pre>\n\n\n\n<p>Finally, we display the formatted date and time on the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Clear the OLED display\noled.fill(0)\n\n# Display the formatted date and time\noled.text('Date: ' + formatted_day_week, 0, 0)\noled.text(formatted_date, 0, 16)\noled.text('Time: ' + formatted_time, 0, 32)\noled.show()<\/code><\/pre>\n\n\n\n<p>We also display the date and time on the shell.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Print the formatted date and time to the shell\nprint('Formatted date:', formatted_date)\nprint('Formatted time:', formatted_time)<\/code><\/pre>\n\n\n\n<p>The time is updated on the screen every second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code># Wait for 1 second\ntime.sleep(1)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>After uploading the code to your board with the name <span class=\"rnthl rntliteral\"><em>main.py<\/em><\/span>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to&nbsp;<strong>File<\/strong>&nbsp;&gt;&nbsp;<strong>Save&nbsp;as<\/strong> and select&nbsp;<strong>Raspberry Pi Pico<\/strong>.<\/li>\n\n\n\n<li>Name the file&nbsp;<strong>main.py<\/strong>&nbsp;and click&nbsp;<strong>OK<\/strong>&nbsp;to save the file on the Raspberry Pi Pico.<\/li>\n<\/ol>\n\n\n\n<p>Disconnect the Raspberry Pi from the REPL and notice that it displays the correct date and time thanks to timekeeping with the RTC module. You can remove and apply power again and it will always show the accurate date and time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Clock-OLED-Display-RTC.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Clock with OLED Display and RTC Module\" class=\"wp-image-163565\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Clock-OLED-Display-RTC.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Clock-OLED-Display-RTC.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this guide, you learned how to synchronize the RTC module with the system&#8217;s time and how to get synchronized time from it. You also learned how to create a simple digital clock that display the day of the week, date, and time.<\/p>\n\n\n\n<p>We hope you found this guide useful. Another alternative to get date and time is using a <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-neo-6m-micropython\/\" title=\"\">GPS module<\/a>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-neo-6m-micropython\/\" title=\"\">Raspberry Pi Pico: NEO-6M GPS Module \u2013 Get Location and Time (MicroPython)<\/a><\/li>\n<\/ul>\n\n\n\n<p>&nbsp;If you want to learn more about the Raspberry Pi Pico, check out our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-micropython-ebook\/\"><strong>Learn Raspberry Pi Pico\/PicoW with MicroPython (eBook)<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\">Free Raspberry Pi Pico projects and tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide you&#8217;ll learn how to interface a DS1307 RTC module with the Raspberry Pi Pico programmed with MicroPython. We&#8217;ll cover the basic functioning of the module, how to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: DS1307 RTC Module &#8211; Keep Track of Time (MicroPython)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-ds1307-rtc-micropython\/#more-163509\" aria-label=\"Read more about Raspberry Pi Pico: DS1307 RTC Module &#8211; Keep Track of Time (MicroPython)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":163568,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[324,326],"tags":[],"class_list":["post-163509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-micropython"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/Raspberry-Pi-Pico-Real-Time-Clock-Module-Micropython.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=163509"}],"version-history":[{"count":9,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163509\/revisions"}],"predecessor-version":[{"id":166670,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163509\/revisions\/166670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/163568"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=163509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=163509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=163509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}