stevie135s
Published © CC BY-SA

Oled clock on nano with rtc DS1302

Easy clock project using Arduino nano and RTC DS1302 module with an Oled SSD1306 Display

BeginnerProtip4,085
Oled clock on nano with rtc DS1302

Things used in this project

Hardware components

Arduino Nano
×1
Graphic OLED, 128 x 64
Graphic OLED, 128 x 64
×1
Breadboard and hook up wire
×1
rtc ds1302 module
×1

Story

Read more

Schematics

Oled Clock

Code

Oled Clock on Nano

Arduino
#include <Arduino.h>
#include <Ds1302.h>
#include <LowPower.h>

 // DS1302 RTC instance
Ds1302 rtc(9,7,8); // RST , CLK , DAT

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


const static char* WeekDays[] =
{
    "Monday    ",
    "Tuesday   ",
    "Wednesday ",
    "Thursday  ",
    "Friday    ",
    "Saturday  ",
    "Sunday    "
};


void setup() {
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
   
    rtc.init(); // initialize the RTC
    display.clearDisplay();   // clears the screen and buffer
//    display.drawRect(0, 0, 84, 30, BLACK);
//    display.drawRect(0, 29, 84, 12, BLACK);
    
}


void loop()
{

    // get the current time
    Ds1302::DateTime now;
    rtc.getDateTime(&now);
    static uint8_t last_second = 0;
    if (last_second != now.second)
    {
        last_second = now.second;
        display.setTextColor(SSD1306_WHITE);
        display.setTextSize(2);
        display.setCursor(18,0);
        if (now.hour <= 9) { //If Hour is single figures, put a 0 in front
        display.print("0");
        }
        display.print(now.hour);
        display.print(":");
     if (now.minute <= 9) {  //If Minute is single figures, put a 0 in front
        display.print("0");
     }
        display.print(now.minute);
        display.print(":"); 
        if (now.second <= 9) {  //If Seconds is single figures, put a 0 in front
        display.print("0");
     } 
        display.print(now.second);

        display.setTextSize(1);
        display.setCursor(27,20);
        display.print(WeekDays[now.dow -1]);
        display.print(now.day);
        display.print("/");
        display.print(now.month);
        
        display.display();

        
        display.setTextColor(SSD1306_BLACK);
        display.setCursor(27,20);
        display.print(WeekDays[now.dow -1]);
        display.print(now.day);
        display.print("/");
        display.print(now.month);           
        
                
        
        display.setTextSize(2);
        display.setCursor(18,0);
     if (now.hour <= 9) {
        display.print("0");
    }
       display.print(now.hour);
       display.print(":");
     if (now.minute <= 9) {
       display.print("0");
  }
       display.print(now.minute);
       display.print(":");
     if (now.second <= 9) {
       display.print("0");
  } 
      display.print(now.second);
 
// No need to display the screen again
 
    }
    LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF);
}

Tweaked Updated Display

Arduino
#include <Arduino.h>
#include <Ds1302.h>
#include <LowPower.h>

 // DS1302 RTC instance
Ds1302 rtc(9,7,8); // RST , CLK , DAT

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


const static char* WeekDays[] =
{
    "Monday    ",
    "Tuesday   ",
    "Wednesday ",
    "Thursday  ",
    "Friday    ",
    "Saturday  ",
    "Sunday    "
};
const static char* MonthText[] =
{
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
};


void setup() {
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
   
    rtc.init(); // initialize the RTC
    display.clearDisplay();   // clears the screen and buffer
    display.drawLine(12,18,112,18,WHITE);
    
}


void loop()
{

    // get the current time
    Ds1302::DateTime now;
    rtc.getDateTime(&now);
    static uint8_t last_second = 0;
    if (last_second != now.second)
    {
        last_second = now.second;
        display.setTextColor(SSD1306_WHITE);
        display.setTextSize(2);
        display.setCursor(15,2);
        if (now.hour <= 9) { //If Hour is single figures, put a 0 in front
        display.print("0");
        }
        display.print(now.hour);
        display.print(":");
     if (now.minute <= 9) {  //If Minute is single figures, put a 0 in front
        display.print("0");
     }
        display.print(now.minute);
        display.print(":"); 
        if (now.second <= 9) {  //If Seconds is single figures, put a 0 in front
        display.print("0");
     } 
        display.print(now.second);

        display.setTextSize(1);
        display.setCursor(17,22);
        display.print(WeekDays[now.dow -1]);
        display.print(now.day);
        display.print(" ");
        display.print(MonthText[now.month -1]);
        
        display.display();

        
        display.setTextColor(SSD1306_BLACK);
        display.setCursor(17,22);
        display.print(WeekDays[now.dow -1]);
        display.print(now.day);
        display.print(" ");
        display.print(MonthText[now.month -1]);           
        
                
        
        display.setTextSize(2);
        display.setCursor(15,2);
     if (now.hour <= 9) {
        display.print("0");
    }
       display.print(now.hour);
       display.print(":");
     if (now.minute <= 9) {
       display.print("0");
  }
       display.print(now.minute);
       display.print(":");
     if (now.second <= 9) {
       display.print("0");
  } 
      display.print(now.second);
 
// No need to display the screen again
 
    }
    LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF);
}

Credits

stevie135s

stevie135s

22 projects • 10 followers

Comments