Components and supplies
Breadboard and hook up wire
rtc ds1302 module
Arduino Nano
Graphic OLED, 128 x 64
Project description
Code
Tweaked Updated Display
arduino
1 2#include <Arduino.h> 3#include <Ds1302.h> 4#include <LowPower.h> 5 6 // DS1302 RTC instance 7Ds1302 rtc(9,7,8); // RST , CLK , DAT 8 9#include <SPI.h> 10#include <Wire.h> 11#include <Adafruit_GFX.h> 12#include <Adafruit_SSD1306.h> 13 14#define SCREEN_WIDTH 128 // OLED display width, in pixels 15#define SCREEN_HEIGHT 32 // OLED display height 16 17// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 18// The pins for I2C are defined by the Wire-library. 19 20#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 21#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 22Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 23 24 25const static char* WeekDays[] = 26{ 27 "Monday ", 28 "Tuesday ", 29 "Wednesday ", 30 "Thursday ", 31 "Friday ", 32 "Saturday ", 33 "Sunday " 34}; 35const static char* MonthText[] = 36{ 37 "Jan", 38 "Feb", 39 "Mar", 40 "Apr", 41 "May", 42 "Jun", 43 "Jul", 44 "Aug", 45 "Sep", 46 "Oct", 47 "Nov", 48 "Dec" 49}; 50 51 52void setup() { 53 if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { 54 Serial.println(F("SSD1306 allocation failed")); 55 for(;;); // Don't proceed, loop forever 56 } 57 58 rtc.init(); // initialize the RTC 59 display.clearDisplay(); // clears the screen and buffer 60 display.drawLine(12,18,112,18,WHITE); 61 62} 63 64 65void loop() 66{ 67 68 // get the current time 69 Ds1302::DateTime now; 70 rtc.getDateTime(&now); 71 static uint8_t last_second = 0; 72 if (last_second != now.second) 73 { 74 last_second = now.second; 75 display.setTextColor(SSD1306_WHITE); 76 display.setTextSize(2); 77 display.setCursor(15,2); 78 if (now.hour <= 9) { //If Hour is single figures, put a 0 in front 79 display.print("0"); 80 } 81 display.print(now.hour); 82 display.print(":"); 83 if (now.minute <= 9) { //If Minute is single figures, put a 0 in front 84 display.print("0"); 85 } 86 display.print(now.minute); 87 display.print(":"); 88 if (now.second <= 9) { //If Seconds is single figures, put a 0 in front 89 display.print("0"); 90 } 91 display.print(now.second); 92 93 display.setTextSize(1); 94 display.setCursor(17,22); 95 display.print(WeekDays[now.dow -1]); 96 display.print(now.day); 97 display.print(" "); 98 display.print(MonthText[now.month -1]); 99 100 display.display(); 101 102 103 display.setTextColor(SSD1306_BLACK); 104 display.setCursor(17,22); 105 display.print(WeekDays[now.dow -1]); 106 display.print(now.day); 107 display.print(" "); 108 display.print(MonthText[now.month -1]); 109 110 111 112 display.setTextSize(2); 113 display.setCursor(15,2); 114 if (now.hour <= 9) { 115 display.print("0"); 116 } 117 display.print(now.hour); 118 display.print(":"); 119 if (now.minute <= 9) { 120 display.print("0"); 121 } 122 display.print(now.minute); 123 display.print(":"); 124 if (now.second <= 9) { 125 display.print("0"); 126 } 127 display.print(now.second); 128 129// No need to display the screen again 130 131 } 132 LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF); 133} 134
Oled Clock on Nano
arduino
1 2#include <Arduino.h> 3#include <Ds1302.h> 4#include <LowPower.h> 5 6 // DS1302 RTC instance 7Ds1302 rtc(9,7,8); // RST , CLK , DAT 8 9#include <SPI.h> 10#include <Wire.h> 11#include <Adafruit_GFX.h> 12#include <Adafruit_SSD1306.h> 13 14#define SCREEN_WIDTH 128 // OLED display width, in pixels 15#define SCREEN_HEIGHT 32 // OLED display height, in pixels 16 17// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 18// The pins for I2C are defined by the Wire-library. 19 20#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 21#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 22Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 23 24 25const static char* WeekDays[] = 26{ 27 "Monday ", 28 "Tuesday ", 29 "Wednesday ", 30 "Thursday ", 31 "Friday ", 32 "Saturday ", 33 "Sunday " 34}; 35 36 37void setup() { 38 if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { 39 Serial.println(F("SSD1306 allocation failed")); 40 for(;;); // Don't proceed, loop forever 41 } 42 43 rtc.init(); // initialize the RTC 44 display.clearDisplay(); // clears the screen and buffer 45// display.drawRect(0, 0, 84, 30, BLACK); 46// display.drawRect(0, 29, 84, 12, BLACK); 47 48} 49 50 51void loop() 52{ 53 54 // get the current time 55 Ds1302::DateTime now; 56 rtc.getDateTime(&now); 57 static uint8_t last_second = 0; 58 if (last_second != now.second) 59 { 60 last_second = now.second; 61 display.setTextColor(SSD1306_WHITE); 62 display.setTextSize(2); 63 display.setCursor(18,0); 64 if (now.hour <= 9) { //If Hour is single figures, put a 0 in front 65 display.print("0"); 66 } 67 display.print(now.hour); 68 display.print(":"); 69 if (now.minute <= 9) { //If Minute is single figures, put a 0 in front 70 display.print("0"); 71 } 72 display.print(now.minute); 73 display.print(":"); 74 if (now.second <= 9) { //If Seconds is single figures, put a 0 in front 75 display.print("0"); 76 } 77 display.print(now.second); 78 79 display.setTextSize(1); 80 display.setCursor(27,20); 81 display.print(WeekDays[now.dow -1]); 82 display.print(now.day); 83 display.print("/"); 84 display.print(now.month); 85 86 display.display(); 87 88 89 display.setTextColor(SSD1306_BLACK); 90 display.setCursor(27,20); 91 display.print(WeekDays[now.dow -1]); 92 display.print(now.day); 93 display.print("/"); 94 display.print(now.month); 95 96 97 98 display.setTextSize(2); 99 display.setCursor(18,0); 100 if (now.hour <= 9) { 101 display.print("0"); 102 } 103 display.print(now.hour); 104 display.print(":"); 105 if (now.minute <= 9) { 106 display.print("0"); 107 } 108 display.print(now.minute); 109 display.print(":"); 110 if (now.second <= 9) { 111 display.print("0"); 112 } 113 display.print(now.second); 114 115// No need to display the screen again 116 117 } 118 LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF); 119} 120
Tweaked Updated Display
arduino
1 2#include <Arduino.h> 3#include <Ds1302.h> 4#include <LowPower.h> 5 6 7 // DS1302 RTC instance 8Ds1302 rtc(9,7,8); // RST , CLK , DAT 9 10#include 11 <SPI.h> 12#include <Wire.h> 13#include <Adafruit_GFX.h> 14#include <Adafruit_SSD1306.h> 15 16#define 17 SCREEN_WIDTH 128 // OLED display width, in pixels 18#define SCREEN_HEIGHT 32 // 19 OLED display height 20 21// Declaration for an SSD1306 display connected to I2C 22 (SDA, SCL pins) 23// The pins for I2C are defined by the Wire-library. 24 25#define 26 OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 27#define 28 SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 29Adafruit_SSD1306 30 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 31 32 33const static 34 char* WeekDays[] = 35{ 36 "Monday ", 37 "Tuesday ", 38 "Wednesday 39 ", 40 "Thursday ", 41 "Friday ", 42 "Saturday ", 43 "Sunday 44 " 45}; 46const static char* MonthText[] = 47{ 48 "Jan", 49 "Feb", 50 51 "Mar", 52 "Apr", 53 "May", 54 "Jun", 55 "Jul", 56 57 "Aug", 58 "Sep", 59 "Oct", 60 "Nov", 61 "Dec" 62}; 63 64 65void 66 setup() { 67 if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { 68 Serial.println(F("SSD1306 69 allocation failed")); 70 for(;;); // Don't proceed, loop forever 71 } 72 73 74 rtc.init(); // initialize the RTC 75 display.clearDisplay(); // 76 clears the screen and buffer 77 display.drawLine(12,18,112,18,WHITE); 78 79} 80 81 82void 83 loop() 84{ 85 86 // get the current time 87 Ds1302::DateTime now; 88 89 rtc.getDateTime(&now); 90 static uint8_t last_second = 0; 91 if (last_second 92 != now.second) 93 { 94 last_second = now.second; 95 display.setTextColor(SSD1306_WHITE); 96 97 display.setTextSize(2); 98 display.setCursor(15,2); 99 if 100 (now.hour <= 9) { //If Hour is single figures, put a 0 in front 101 display.print("0"); 102 103 } 104 display.print(now.hour); 105 display.print(":"); 106 107 if (now.minute <= 9) { //If Minute is single figures, put a 0 in front 108 109 display.print("0"); 110 } 111 display.print(now.minute); 112 113 display.print(":"); 114 if (now.second <= 9) { //If Seconds is 115 single figures, put a 0 in front 116 display.print("0"); 117 } 118 119 display.print(now.second); 120 121 display.setTextSize(1); 122 display.setCursor(17,22); 123 124 display.print(WeekDays[now.dow -1]); 125 display.print(now.day); 126 127 display.print(" "); 128 display.print(MonthText[now.month -1]); 129 130 131 display.display(); 132 133 134 display.setTextColor(SSD1306_BLACK); 135 136 display.setCursor(17,22); 137 display.print(WeekDays[now.dow -1]); 138 139 display.print(now.day); 140 display.print(" "); 141 display.print(MonthText[now.month 142 -1]); 143 144 145 146 display.setTextSize(2); 147 148 display.setCursor(15,2); 149 if (now.hour <= 9) { 150 display.print("0"); 151 152 } 153 display.print(now.hour); 154 display.print(":"); 155 if 156 (now.minute <= 9) { 157 display.print("0"); 158 } 159 display.print(now.minute); 160 161 display.print(":"); 162 if (now.second <= 9) { 163 display.print("0"); 164 165 } 166 display.print(now.second); 167 168// No need to display the screen 169 again 170 171 } 172 LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF); 173} 174
Downloadable files
Oled Clock
Oled Clock
Oled Clock
Oled Clock
Comments
Only logged in users can leave comments