0% found this document useful (0 votes)
51 views3 pages

Arduino DS1302 RTC OLED Display Code

This Arduino code is displaying the current date and time on an OLED display. It includes libraries for interfacing with a DS1302 real-time clock chip and an SSD1306 OLED display. In setup, it initializes the RTC and clears the display. The main loop gets the current date and time from the RTC, formats and displays it on the OLED screen every second, then puts the Arduino to sleep to conserve power between updates.

Uploaded by

insert992
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Arduino DS1302 RTC OLED Display Code

This Arduino code is displaying the current date and time on an OLED display. It includes libraries for interfacing with a DS1302 real-time clock chip and an SSD1306 OLED display. In setup, it initializes the RTC and clears the display. The main loop gets the current date and time from the RTC, formats and displays it on the OLED screen every second, then puts the Arduino to sleep to conserve power between updates.

Uploaded by

insert992
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#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);
}

You might also like