Overview

In this tutorial, we are going to make a digital clock with Arduino UNO, an RTC module and an OLED display.

Hardware & Software Needed

  1. Arduino IDE
  2. Arduino UNO
  3. DS3231
  4. SSD1306 128×32 OLED Screen

Project circuit:

DS3231 RTC

Description

DS3231 RTC is a precise real-time clock module with 32Kbit EEPROM and a built-in 10bit temperature sensor with 0.25C resolution.

The DS3231 RTC Precise Real-Time Clock Module is a low cost, extremely accurate I²C Real-Time Clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device includes a battery input and maintains accurate timing when the main power to the device is interrupted.

The integration of the crystal resonator improves the long-term accuracy of the device and also reduces the number of parts on the production line. The Arduino ds3231 is available in commercial and industrial temperature ranges and is offered in a 300 mil thick 16-pin SO package.

For more detailed information on the IC, datasheet DS 3231 is attached.

Features:

  1. Two Time-of-Day Alarms.
  2. Digital Temp Sensor Output.
  3. Register for Aging Trim.
  4. DS 3231 RTC with 2032 Battery Holder.
  5. Highly Accurate RTC Completely Manages All Timekeeping Functions.
  6. Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100.
  7. Configurable I2C device Address for AT24C32 using SMD jumpers on PCB (A0, A1, A2).
  8. Programmable Square-Wave Output Signal.
  9. Battery-Backup Input for Continuous Timekeeping.
  10. Low Power Operation Extends Battery-Backup Run Time.

Specification

OLED Graphic Display

The OLED module shown in the image above is a very popular module available in the market.

There are many variants of this module on the market with different resolutions, communication protocols or pixel colours.

These OLED modules are powered by the SSD1306 chip, which is a controller chip for 128×64 dot-matrix OLED segments. SSD1306 has its own controller and supports SPI and I2C communication protocols. Therefore, there are various OLED modules on the market, some of which only support SPI communication, some of which only support I2C communication, and some of which support both I2C and SPI communication. (Different number of pins for different modules) Since the controller chip supports 128×64 resolution, there are lower resolution options such as 128×32.

Different modules support different colours like blue, yellow, and white. Some modules also support multiple colours. You will need to check your display module specifications to see what colours are supported.

We are using a 4-pin 128×64 OLED module with I2C support, similar to the one shown in the image above.

For more information about the OLED display and how to use it, see SSD1306 OLED Display in the Sensors and Modules section.

The above image shows a 128×64 I2C based OLED module.

VCC: This is the power pin for the module. A supply of 3.3V or 5V can be provided to this pin to power the display.

GND: This is the ground pin for the module.

SCL and SDA: These are the serial clock and serial data pins for I2C communication.

Oled Display Basic Code

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* Initialize display with address 0x3C */ 

display.clearDisplay(); /* Clear display */ display.setCursor(15,5); /* Set x,y coordinates */ 

display.setTextSize(1); /* Select font size of text. Increases with size of argument. */ 

display.setTextColor(WHITE); /* Color of text*/ display.println("ElectronicWings"); /* Text to be displayed */ 

display.display();

delay(500); /* Draw rectangle with round edges. Parameters (x_co-ord,y-co-ord,width,height,color) */ 

display.drawRoundRect(5, 35, 35, 25, 1, WHITE); /* Draw circle. Parameters (x_co-ord of origin,y-co-ord of origin,radius,color) */ 

display.drawCircle(65, 50, 12, WHITE); /* Draw triangle. Parameters (x0,y0,x1,y1,x2,y2,width,color). (x0,y0), (x1,y1) and (x2,y2) are the co-ord of vertices of the triangle */

display.drawTriangle(90, 60, 105, 35, 120, 60, WHITE); 

display.display();

delay(1000);

display.clearDisplay();

Reading time from ds3231 RTC module and display it on OLED

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup () 
{
  Serial.begin(9600);
  delay(3000); // wait for console opening

 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
  
  // Comment out below lines once you set the date & time.
    // Following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
    // Following line sets the RTC with an explicit date & time
    // for example to set January 27 2017 at 12:56 you would call:
    // rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
  }

   display.display();
 delay(2);
 display.clearDisplay();
 
 
display.clearDisplay();
display.setTextColor(WHITE);
//display.startscrollright(0x00, 0x0F);
display.setTextSize(2);
display.setCursor(0,5);
display.print("  Clock ");
display.display();
delay(3000);
}

void loop () 
{
    DateTime now = rtc.now();
    
    Serial.println("Current Date & Time: ");
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    display.clearDisplay();
    display.setTextSize(2);
    display.setCursor(75,0);
    display.println(now.second(), DEC);
    display.setTextSize(2);
    display.setCursor(25,0);
    display.println(":");
     
    display.setTextSize(2);
    display.setCursor(65,0);
    display.println(":");
     
    display.setTextSize(2);
    display.setCursor(40,0);
    display.println(now.minute(), DEC);
     
    display.setTextSize(2);
    display.setCursor(0,0);
    display.println(now.hour(), DEC);
     
    display.setTextSize(2);
    display.setCursor(0,20);
    display.println(now.day(), DEC);
     
    display.setTextSize(2);
    display.setCursor(25,20);
    display.println("/");
     
    display.setTextSize(2);
    display.setCursor(40,20);
    display.println(now.month(), DEC);
     
    display.setTextSize(2);
    display.setCursor(55,20);
    display.println("/");
     
    display.setTextSize(2);
    display.setCursor(70,20);
    display.println(now.year(), DEC);
     
    display.setTextSize(2);
    display.setCursor(0,40);
    display.print(daysOfTheWeek[now.dayOfTheWeek()]);
     
    display.display(); 
        
    Serial.println();
    delay(1000);
}

By Devilal

Leave a Reply

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