Using a RTC 3231 With OLED As a Clock
by sister mariaM in Circuits > Arduino
15 Views, 0 Favorites, 0 Comments
Using a RTC 3231 With OLED As a Clock
This Instructables will illustrate how to use a RTC 3231 with an OLED to show time, day and date .
Supplies
The materials used are ;
Arduino
USB cable
DS3231
OLED and leads
Theory
The RTC 3231 is an accurate electronics part that keeps track of time. It uses a Temperature compensated crystal oscillator .It keeps time +_ 2 minutes per year .It also automatically adjusts the months .It uses I2 communication (SDA and SCL) to connect to Arduino.
The next part is the OLED .It uses self illuminating carbon pixels .It produces light when electricity runs through it .It is a display
Circuit With Arduino
How to set up;
connect 5 v Arduino to breadboard
Connect ground to breadboard
Connect 5 v to RTC3231 to breadboard
connect ground to of RTC to breadboard .
connect SDA to SDA orA4 of RTC to Arduino
Connect SCL or A5 to RTC to Arduino
OLED connect 5v to 5v of RTC
Connect Ground to ground of RTC
connect SCL or A5 to RTC
connect SDA or A4 of RTC
Conclusion
The Code
upload this code first ;
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
// Screen configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin shared or not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
// Initialize OLED Display at I2C address 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED allocation failed"));
for(;;);
}
// Initialize RTC module
if (!rtc.begin()) {
Serial.println("Couldn't find RTC module");
for(;;);
}
Automatically sets the clock to the exact time your computer uploaded the code
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
}
void loop() {
DateTime now = rtc.now();
display.clearDisplay();
// Print Date
display.setTextSize(1);
display.setCursor(10, 10);
display.print("Date: ");
display.print(now.day(), DEC);
display.print('/');
display.print(now.month(), DEC);
display.print('/');
display.print(now.year(), DEC);
// Print Time
display.setTextSize(2);
display.setCursor(10, 35);
// Format leading zero for hours
if(now.hour() < 10) display.print('0');
display.print(now.hour(), DEC);
display.print(':');
// Format leading zero for minutes
if(now.minute() < 10) display.print('0');
display.print(now.minute(), DEC);
display.print(':');
// Format leading zero for seconds
if(now.second() < 10) display.print('0');
display.print(now.second(), DEC);
// Push buffer data to screen hardware
display.display();
delay(1000);
}
Then when the time shows upload this code;
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
// Screen configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin shared or not used
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
// Initialize OLED Display at I2C address 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED allocation failed"));
for(;;);
}
// Initialize RTC module
if (!rtc.begin()) {
Serial.println("Couldn't find RTC module");
for(;;);
}
// Automatically sets the clock to the exact time your computer uploaded the code
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
}
void loop() {
DateTime now = rtc.now();
display.clearDisplay();
// Print Date
display.setTextSize(1);
display.setCursor(10, 10);
display.print("Date: ");
display.print(now.day(), DEC);
display.print('/');
display.print(now.month(), DEC);
display.print('/');
display.print(now.year(), DEC);
// Print Time
display.setTextSize(2);
display.setCursor(10, 35);
// Format leading zero for hours
if(now.hour() < 10) display.print('0');
display.print(now.hour(), DEC);
display.print(':');
// Format leading zero for minutes
if(now.minute() < 10) display.print('0');
display.print(now.minute(), DEC);
display.print(':');
// Format leading zero for seconds
if(now.second() < 10) display.print('0');
display.print(now.second(), DEC);
// Push buffer data to screen hardware
display.display();
delay(1000);
}
Made sure to include all the libraries .
Conclusion
This Instructables shows how to use a 3231 RTC with an OLED to show time ,date and day .I have been using this
clock for a month now .It is extremely accurate .Hope you enjoyed this and can use this Instructables to set up a clock.Thank you