{"id":166437,"date":"2025-04-03T16:01:16","date_gmt":"2025-04-03T16:01:16","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=166437"},"modified":"2025-04-03T16:01:20","modified_gmt":"2025-04-03T16:01:20","slug":"arduino-digital-clock-ds3231-oled","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-digital-clock-ds3231-oled\/","title":{"rendered":"Arduino Digital Clock with DS3231 RTC and OLED Display"},"content":{"rendered":"\n<p>In this project, you&#8217;ll learn how to create a digital clock with the Arduino using the DS3231 Real Time Clock (RTC) module and an OLED display. We&#8217;ll provide you with all the required steps, including the circuit diagram, code, and an explanation of the code.<\/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\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Digital Clock with DS3231 RTC and OLED Display\" class=\"wp-image-166510\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.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>Recommended resources:<\/strong><\/p>\n\n\n\n<p>To better understand the steps to build the digital clock, we recommend that you get familiar with the DS3231 RTC module and the OLED display, by taking a look at the following guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-oled-display-with-arduino\/\">Guide for I2C OLED Display with Arduino<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-ds3231-real-time-clock\/\">Arduino with DS3231 Real Time Clock (RTC): Getting Time and Setting Alarms<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\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\/2025\/01\/DS3231-Arduino-Clock-OLED-Display.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Clock with DS3231 and OLED Display\" class=\"wp-image-166511\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/DS3231-Arduino-Clock-OLED-Display.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/DS3231-Arduino-Clock-OLED-Display.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>This project is quite simple to understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We first set the time of the DS3231 Real Time Clock module;<\/li>\n\n\n\n<li>Then, we get the date, time, and temperature from the RTC;<\/li>\n\n\n\n<li>We display the date, time, and temperature on the OLED display;<\/li>\n\n\n\n<li>We refresh the OLED with the new current time and readings every second.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>Here&#8217;s a list of the parts required for this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino UNO<\/a>&nbsp;(or other Arduino board) \u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best Arduino Starter Kits<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ds3231-rtc-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">DS3231 Module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/oled-display-128x64-0-96-inch\/\" target=\"_blank\" rel=\"noopener\" title=\"\">SSD1306 I2C 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<h2 class=\"wp-block-heading\">Wiring the Circuit<\/h2>\n\n\n\n<p>Follow the next schematic diagram to wire the circuit, or use the following table as a reference. Both modules communicate with the Arduino board using I2C communication protocol. <\/p>\n\n\n\n<p>Since each module has a unique I2C address, we can use the same I2C bus (same I2C pins) to establish a communication between the Arduino and the modules.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>OLED Display<\/strong><\/td><td><strong>DS3231 RTC<\/strong><\/td><td><strong>Arduino<\/strong><\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><tr><td>VCC<\/td><td>VCC<\/td><td><span class=\"rnthl rntcred\">5V<\/span><\/td><\/tr><tr><td>SCL<\/td><td>SCL<\/td><td><span class=\"rnthl rntcyellow\">A5<\/span><\/td><\/tr><tr><td>SDA<\/td><td>SDA<\/td><td><span class=\"rnthl rntclgray\">A4<\/span><\/td><\/tr><tr><td><\/td><td>SQW<\/td><td>Don&#8217;t connect<\/td><\/tr><tr><td><\/td><td>32K<\/td><td>Don&#8217;t connect<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"500\" height=\"664\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/arduino-digital-clock-wiring-diagram_bb.jpg?resize=500%2C664&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino digital clock ds3231 and oled display - schematic diagram\" class=\"wp-image-166451\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/arduino-digital-clock-wiring-diagram_bb.jpg?w=500&amp;quality=100&amp;strip=all&amp;ssl=1 500w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/arduino-digital-clock-wiring-diagram_bb.jpg?resize=226%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 226w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Installing Libraries<\/h2>\n\n\n\n<p>For this tutorial, you need to install the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/adafruit\/RTClib\" target=\"_blank\" rel=\"noopener\" title=\"\">RTCLib library from Adafruit<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_SSD1306\" target=\"_blank\" rel=\"noopener\" title=\"\">Adafruit_SSD1306 library<\/a><\/li>\n<\/ul>\n\n\n\n<p>Follow the next instructions to install the libraries:<\/p>\n\n\n\n<p>In the Arduino IDE, go to <strong>Sketch<\/strong> &gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>. Search for <strong>RTCLib <\/strong>and install the library by Adafruit. We&#8217;re using version 2.1.4.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"391\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/install-RTCLib.png?resize=750%2C391&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE install RTCLib Library\" class=\"wp-image-163965\" style=\"width:746px;height:auto\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/install-RTCLib.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/install-RTCLib.png?resize=300%2C156&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Then, search for Adafruit SSD1306 and install the library.<\/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=\"861\" height=\"586\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/installing-oled-display-library-ssd1306.jpg?resize=861%2C586&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing OLED Library Arduino IDE\" class=\"wp-image-166448\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/installing-oled-display-library-ssd1306.jpg?w=861&amp;quality=100&amp;strip=all&amp;ssl=1 861w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/installing-oled-display-library-ssd1306.jpg?resize=300%2C204&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/installing-oled-display-library-ssd1306.jpg?resize=768%2C523&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/figure><\/div>\n\n\n<p>Now you have installed all the required libraries for this project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Arduino Digital Clock &#8211; Code<\/h2>\n\n\n\n<p>The following code creates the digital clock. Copy to your Arduino IDE and upload it to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/arduino-digital-clock-ds3231-oled\/\n*********\/\n\n#include &lt;Wire.h&gt;\n#include &lt;RTClib.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;\n\nRTC_DS3231 rtc;\n\n#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 64\n\n\/\/ Create OLED object\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\n\n\/\/ Days of the week array\nchar daysOfTheWeek[7][12] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};\n\nvoid setup() {\n  Serial.begin(9600);\n\n  \/\/ Initialize OLED display\n  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\n    Serial.println(&quot;SSD1306 allocation failed&quot;);\n    for (;;);\n  }\n\n  \/\/ Clear the display\n  display.clearDisplay();\n  display.display();\n\n  \/\/ Initialize RTC\n  if (!rtc.begin()) {\n    Serial.println(&quot;Couldn't find RTC&quot;);\n    while (1);\n  }\n\n  \/\/ Check if RTC lost power and set the time if necessary\n  if (rtc.lostPower()) {\n    Serial.println(&quot;RTC lost power! Setting the time...&quot;);\n    \/\/ Set the RTC to the date &amp; time this sketch was compiled\n    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n  }\n  \/\/ Or, if you need to set the time for the first time, uncomment the following line:\n  \/\/ rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n\n}\n\nvoid loop() {\n  \/\/ Get the current time from the RTC\n  DateTime now = rtc.now();\n\n  \/\/ Clear the OLED display buffer\n  display.clearDisplay();\n\n  \/\/ Display the date\n  display.setTextSize(1);\n  display.setTextColor(WHITE);\n  display.setCursor(0, 0);\n  display.print(&quot;Date: &quot;);\n  display.print(now.year());\n  display.print(&quot;-&quot;);\n  if (now.month() &lt; 10) display.print(&quot;0&quot;);\n  display.print(now.month());\n  display.print(&quot;-&quot;);\n  if (now.day() &lt; 10) display.print(&quot;0&quot;);\n  display.print(now.day());\n\n  \/\/ Display the time\n  display.setCursor(0, 15);\n  display.print(&quot;Time: &quot;);\n  if (now.hour() &lt; 10) display.print(&quot;0&quot;);\n  display.print(now.hour());\n  display.print(&quot;:&quot;);\n  if (now.minute() &lt; 10) display.print(&quot;0&quot;);\n  display.print(now.minute());\n  display.print(&quot;:&quot;);\n  if (now.second() &lt; 10) display.print(&quot;0&quot;);\n  display.print(now.second());\n\n  \/\/ Display the day of the week\n  display.setCursor(0, 30);\n  display.print(&quot;Day: &quot;);\n  display.print(daysOfTheWeek[now.dayOfTheWeek()]);\n\n  \/\/ Display the temperature from DS3231\n  display.setCursor(0, 45);\n  display.print(&quot;Temp: &quot;);\n  display.print(rtc.getTemperature());\n  display.cp437(true);\n  display.write(167);\n  display.print(&quot;C&quot;);\n\n  \/\/ Display everything on the OLED\n  display.display();\n\n  delay(1000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Arduino\/Arduino_DS3231_OLED_Clock.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does the Code Work<\/h2>\n\n\n\n<p>Let&#8217;s take a quick look at how the code works.<\/p>\n\n\n\n<p>First, include the required libraries. The <span class=\"rnthl rntliteral\">Wire<\/span> for I2C communication protocol, the <span class=\"rnthl rntliteral\">RTCLib<\/span> to interface with the DS3231, and the <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> to interface with the OLED.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h&gt;\n#include &lt;RTClib.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">RTC_DS3231<\/span> instance called <span class=\"rnthl rntliteral\">rtc<\/span> to refer to the RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>RTC_DS3231 rtc;<\/code><\/pre>\n\n\n\n<p>Define the OLED screen width and height.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 64<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> called <span class=\"rnthl rntliteral\">display<\/span> with the screen width and height defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);<\/code><\/pre>\n\n\n\n<p>Then, you create a char array with the days of the week.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>char daysOfTheWeek&#091;7]&#091;12] = {\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"};<\/code><\/pre>\n\n\n\n<p>You can access each day by its index. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">daysOfTheWeek[0]<\/span> will return &#8220;Sunday&#8221;.<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">daysOfTheWeek[1]<\/span> will return &#8220;Monday&#8221;.<\/li>\n<\/ul>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, the following lines will initialize the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Initialize OLED display\nif (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\n  Serial.println(\"SSD1306 allocation failed\");\n  for (;;);\n}<\/code><\/pre>\n\n\n\n<p>These two commands will clear the display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.clearDisplay();\ndisplay.display();<\/code><\/pre>\n\n\n\n<p>Next, we intiailzie the RTC module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Initialize RTC\nif (!rtc.begin()) {\n  Serial.println(\"Couldn't find RTC\");\n  while (1);\n}<\/code><\/pre>\n\n\n\n<p>Then, check if the RTC has lost power with the <span class=\"rnthl rntliteral\">lostPower()<\/span> function. If it is not running, because it is a new device or because the battery backup failed, we&#8217;ll print a message in the Serial Monitor and set the time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (rtc.lostPower()) {\n  Serial.println(\"RTC lost power, let's set the time!\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting the Time<\/h4>\n\n\n\n<p>To set the time on the RTC, we can use the <span class=\"rnthl rntliteral\">adjust()<\/span> method on our <span class=\"rnthl rntliteral\">rtc<\/span> object. The following line sets the RTC\u2019s date and time to the current date and time when this sketch was last compiled.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));<\/code><\/pre>\n\n\n\n<p><span class=\"rnthl rntliteral\">__DATE__<\/span> and <span class=\"rnthl rntliteral\">__TIME__<\/span> are macros that provide the current date and time at compilation.<\/p>\n\n\n\n<p>If you need to re-set the time on a previously configured device, you can uncomment the following line.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we get data from the RTC module as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DateTime now = rtc.now();<\/code><\/pre>\n\n\n\n<p>It returns a <span class=\"rnthl rntliteral\">DateTime<\/span> object containing the values for the current year, month, day, hour, minute, and second.<\/p>\n\n\n\n<p>To access each field of the date and time, we can use the following methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><span class=\"rnthl rntliteral\">now.year()<\/span><\/td><td>Gets the current year (e.g., 2024)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.month()<\/span><\/td><td>Gets the current month (1\u201312)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.day()<\/span><\/td><td>Gets the current day of the month (1\u201331)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.dayOfTheWeek()<\/span><\/td><td>Gets the day of the week (0-6), where 0 is Sunday, and 6 is Saturday<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.hour()<\/span><\/td><td>Gets the current hour (0\u201323)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.minute()<\/span><\/td><td>Gets the current minute (0\u201359)<\/td><\/tr><tr><td><span class=\"rnthl rntliteral\">now.second()<\/span><\/td><td>Gets the current second (0\u201359)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We set the display text size and color.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setTextSize(1);\ndisplay.setTextColor(WHITE);<\/code><\/pre>\n\n\n\n<p>And then, we start displaying the information on the display. The following lines display the date.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setCursor(0, 0);\ndisplay.print(\"Date: \");\ndisplay.print(now.year());\ndisplay.print(\"-\");\nif (now.month() &lt; 10) display.print(\"0\");\ndisplay.print(now.month());\ndisplay.print(\"-\");\nif (now.day() &lt; 10) display.print(\"0\");\ndisplay.print(now.day());<\/code><\/pre>\n\n\n\n<p>Next, we display the time:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setCursor(0, 15);\ndisplay.print(\"Time: \");\nif (now.hour() &lt; 10) display.print(\"0\");\ndisplay.print(now.hour());\ndisplay.print(\":\");\nif (now.minute() &lt; 10) display.print(\"0\");\ndisplay.print(now.minute());\ndisplay.print(\":\");\nif (now.second() &lt; 10) display.print(\"0\");\ndisplay.print(now.second());<\/code><\/pre>\n\n\n\n<p>The day of the week:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setCursor(0, 30);\ndisplay.print(\"Day: \");\ndisplay.print(daysOfTheWeek&#091;now.dayOfTheWeek()]);<\/code><\/pre>\n\n\n\n<p>And finally, the temperature.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setCursor(0, 45);\ndisplay.print(\"Temp: \");\ndisplay.print(rtc.getTemperature());\ndisplay.cp437(true);\ndisplay.write(167);\ndisplay.print(\"C\");<\/code><\/pre>\n\n\n\n<p>To display the <span class=\"rnthl rntliteral\">\u00ba<\/span> symbol, we use the&nbsp;<a href=\"https:\/\/www.ascii-codes.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Code Page 437<\/a>&nbsp;font. For that, you need to set the&nbsp;<span class=\"rnthl rntliteral\">cp437<\/span>&nbsp;to&nbsp;<span class=\"rnthl rntliteral\">true<\/span>&nbsp;as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.cp437(true);<\/code><\/pre>\n\n\n\n<p>Then, use the <span class=\"rnthl rntliteral\">write()<\/span> method to display your chosen character. The <span class=\"rnthl rntliteral\">\u00ba<\/span> symbol corresponds to character 167.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.write(167);<\/code><\/pre>\n\n\n\n<p>Finally, you need to call <span class=\"rnthl rntliteral\">display.display()<\/span> so that the information is actually written on the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.display();<\/code><\/pre>\n\n\n\n<p>Note that we add a leading zero when the number is smaller than 10 on the time and date. So, instead of having, for example: 3:5:6 (which is weird for a time format), you&#8217;ll get <span class=\"rnthl rntliteral\">03:05:06<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your Arduino board. Make sure you select the right board and COM port on the Arduino IDE.<\/p>\n\n\n\n<p>After uploading, the OLED display will show the date, time, and temperature. The data is updated every second.<\/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\/2025\/01\/Arduino-Clock-OLED-Display-DS3231.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Digital DS3231 Digital Clock and OLED Display\" class=\"wp-image-166512\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Clock-OLED-Display-DS3231.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Clock-OLED-Display-DS3231.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>Congratulations! You&#8217;ve built a digital clock with the Arduino, the DS3231 RTC module, and the OLED display. Instead of an OLED display, you may want to use an LCD instead.<\/p>\n\n\n\n<p>We have other projects with the Arduino that you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-ir-remote-control\/\" title=\"\">Arduino \u2013 Control LEDs with IR Remote Control<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-time-attendance-system-with-rfid\/\">Arduino Time Attendance System with RFID<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-temperature-data-logger-with-sd-card-module\/\">Arduino Temperature Data Logger with SD Card Module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-free-guides-sensors-modules\/\">Arduino: 40 Free Guides for Sensors and Modules<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the Arduino with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\" title=\"\">&gt;&gt;All Our Free Arduino Projects and Guides&lt;&lt;<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" title=\"\">Arduino Step-by-step Projects &#8211; Build 25 Projects<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this project, you&#8217;ll learn how to create a digital clock with the Arduino using the DS3231 Real Time Clock (RTC) module and an OLED display. We&#8217;ll provide you with &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Arduino Digital Clock with DS3231 RTC and OLED Display\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/arduino-digital-clock-ds3231-oled\/#more-166437\" aria-label=\"Read more about Arduino Digital Clock with DS3231 RTC and OLED Display\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":166510,"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":[303,267,2,302,245,264],"tags":[],"class_list":["post-166437","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-0-guide","category-arduino-ide","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/01\/Arduino-Digital-Clock-DS3231-OLED-Display.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\/166437","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=166437"}],"version-history":[{"count":7,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/166437\/revisions"}],"predecessor-version":[{"id":168532,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/166437\/revisions\/168532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/166510"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=166437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=166437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=166437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}