Components and supplies
Standard LCD - 16x2 White on Blue
Real Time Clock (RTC)
Arduino UNO
Buzzer
Project description
Code
RTC_alarm_clock_LCD.ino
arduino
Works with UNO + RTC DS1302 + LCD i2c + 4x4 keypad (Majors changes made 12/04/18 to solve some issues)
1//This code is to use with DS1302 RTC module + 4*4 Keypad + LCD i2c + Arduino + Buzzer 2//After wiring the modules, the LCD will show the default date and time or the one set before 3//The objective of this project is that you can set the RTC module from the keypad, and sure it will stay stored 4//Then show it on the screen and after you can set your alarm. 5//Refer to Surtrtech.com or SurtrTech youtube channel for more information 6 7#include <Keypad.h> //Libraries needed 8#include <Wire.h> 9#include <virtuabotixRTC.h> 10#include <LiquidCrystal_I2C.h> 11 12#define I2C_ADDR 0x27 //LCD i2c stuff 13#define BACKLIGHT_PIN 3 14#define En_pin 2 15#define Rw_pin 1 16#define Rs_pin 0 17#define D4_pin 4 18#define D5_pin 5 19#define D6_pin 6 20#define D7_pin 7 21 22LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 23 24 25virtuabotixRTC myRTC(2, 3, 4); //Wiring of the RTC (CLK,DAT,RST) 26 //If you change the wiring change the pins here also 27 28 29const byte numRows= 4; //number of rows on the keypad 30const byte numCols= 4; //number of columns on the keypad 31 32//keymap defines the key pressed according to the row and columns just as appears on the keypad 33char keymap[numRows][numCols]= 34{ 35{'1', '2', '3', 'A'}, 36{'4', '5', '6', 'B'}, 37{'7', '8', '9', 'C'}, 38{'*', '0', '#', 'D'} 39}; 40 41 42byte rowPins[numRows] = {12,11,10,9}; //Rows 0 to 3 //if you modify your pins you should modify this too 43byte colPins[numCols]= {8,7,6,5}; //Columns 0 to 3 44 45int i1,i2,i3,i4; 46char c1,c2,c3,c4; 47char keypressed,keypressedx; 48 49int A_hour=NULL; 50int A_minute=NULL; 51int AlarmIsActive=NULL; 52 53int buzzer = 13; 54 55 56Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); 57 58 59void setup() { 60 Serial.begin(9600); 61 lcd.begin (16,2); //Initialize the LCD 62 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 63 lcd.setBacklight(HIGH); 64 lcd.home (); 65 66} 67 68void loop() { 69 70 while(keypressed == NO_KEY){ //As long as no key is pressed we keep showing the date and time, I'm obliged to clear the screen everytime so the numbers don't get confused 71 //And I should add that little delay so the screen shows correctly otherwise it didn't work for me 72 keypressed = myKeypad.getKey(); 73 lcd.clear(); //Here after clearing the LCD we take the time from the module and print it on the screen with usual LCD functions 74 myRTC.updateTime(); 75 76 if(myRTC.hours==A_hour && myRTC.minutes==A_minute && AlarmIsActive==1 && myRTC.seconds >= 0 && myRTC.seconds <= 2){ 77 while(keypressedx == NO_KEY){ 78 tone(buzzer, 1000); //You can modify the tone or make your own sound 79 delay(100); 80 tone(buzzer, 2000); 81 delay(100); 82 lcd.clear(); 83 lcd.print("Get up !!!"); //Message to show when the alarm is ringing 84 keypressedx = myKeypad.getKey(); 85 } 86 } 87 keypressedx = NO_KEY; 88 noTone(buzzer); 89 lcd.setCursor(0,0); 90 lcd.print(myRTC.dayofmonth); 91 lcd.print("/"); 92 lcd.print(myRTC.month); 93 lcd.print("/"); 94 lcd.print(myRTC.year); 95 lcd.setCursor(0,1); 96 lcd.print(myRTC.hours); 97 lcd.print(":"); 98 lcd.print(myRTC.minutes); 99 lcd.print(":"); 100 lcd.print(myRTC.seconds); 101 delay(100); 102 } 103 104 105 if (keypressed == '*') //As we everytime check the key pressed we only proceed to setup if we press "*" 106 { 107 lcd.clear(); 108 lcd.print(" Setup"); 109 delay(1000); 110 lcd.clear(); 111 lcd.print("Setup year"); 112 //So you can understand how this works, first it shows us "setup" then it prints "setup year" and now you can write your year normally (2-0-1-8) 113 //It automatically passes to setting up the month...until it's finished 114 //The keys from keypad are all considered chars (c) so we should convert them to int that's what I did then we store them (i) 115 //We do some math and we get the year, month... as int so we can inject them to the RTC otherwise it will not be compiled 116 //Months like April you should write 04, 03 for March... otherwise it will not pass to the next parameter 117 //The RTC virtuabotix library is already set to not accept strange time and dates (45/17/1990) (58:90:70), and yes old dates are considered as errors 118 char keypressed2 = myKeypad.waitForKey(); 119 if (keypressed2 != NO_KEY && keypressed2 !='*' && keypressed2 !='#' && keypressed2 !='A' && keypressed2 !='B' && keypressed2 !='C' && keypressed2 !='D' ) 120 { 121 c1 = keypressed2; 122 lcd.setCursor(0, 1); 123 lcd.print(c1); 124 } 125 char keypressed3 = myKeypad.waitForKey(); 126 if (keypressed3 != NO_KEY && keypressed3 !='*' && keypressed3 !='#' && keypressed3 !='A' && keypressed3 !='B' && keypressed3 !='C' && keypressed3 !='D' ) 127 { 128 c2 = keypressed3; 129 lcd.setCursor(1, 1); 130 lcd.print(c2); 131 } 132 char keypressed4 = myKeypad.waitForKey(); 133 if (keypressed4 != NO_KEY && keypressed4 !='*' && keypressed4 !='#' && keypressed4 !='A' && keypressed4 !='B' && keypressed4 !='C' && keypressed4 !='D' ) 134 { 135 c3 = keypressed4; 136 lcd.setCursor(2, 1); 137 lcd.print(c3); 138 } 139 char keypressed5 = myKeypad.waitForKey(); 140 if (keypressed5 != NO_KEY && keypressed5 !='*' && keypressed5 !='#' && keypressed5 !='A' && keypressed5 !='B' && keypressed5 !='C' && keypressed5 !='D' ) 141 { 142 c4 = keypressed5; 143 lcd.setCursor(3, 1); 144 lcd.print(c4); 145 } 146 147 i1=(c1-48)*1000; //the keys pressed are stored into chars I convert them to int then i did some multiplication to get the code as an int of xxxx 148 i2=(c2-48)*100; 149 i3=(c3-48)*10; 150 i4=c4-48; 151 int N_year=i1+i2+i3+i4; 152 delay(500); 153 lcd.clear(); 154 lcd.print("Setup month"); 155 156//////////////////////////////////////////////////////////////// 157 char keypressed6 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above 158 if (keypressed6 != NO_KEY && keypressed6 !='*' && keypressed6 !='#' && keypressed6 !='A' && keypressed6 !='B' && keypressed6 !='C' && keypressed6 !='D' ) 159 { 160 c1 = keypressed6; 161 lcd.setCursor(0, 1); 162 lcd.print(c1); 163 } 164 char keypressed7 = myKeypad.waitForKey(); 165 if (keypressed7 != NO_KEY && keypressed7 !='*' && keypressed7 !='#' && keypressed7 !='A' && keypressed7 !='B' && keypressed7 !='C' && keypressed7 !='D' ) 166 { 167 c2 = keypressed7; 168 lcd.setCursor(1, 1); 169 lcd.print(c2); 170 } 171 172 173 i1=(c1-48)*10; 174 i2=c2-48; 175 int N_month=i1+i2; 176 delay(500); 177 178 lcd.clear(); 179 lcd.print("Setup Day"); 180 181 //////////////////////////////////////////////////////////////// 182 char keypressed8 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above 183 if (keypressed8 != NO_KEY && keypressed8 !='*' && keypressed8 !='#' && keypressed8 !='A' && keypressed8 !='B' && keypressed8 !='C' && keypressed8 !='D' ) 184 { 185 c1 = keypressed8; 186 lcd.setCursor(0, 1); 187 lcd.print(c1); 188 } 189 char keypressed9 = myKeypad.waitForKey(); 190 if (keypressed9 != NO_KEY && keypressed9 !='*' && keypressed9 !='#' && keypressed9 !='A' && keypressed9 !='B' && keypressed9 !='C' && keypressed9 !='D' ) 191 { 192 c2 = keypressed9; 193 lcd.setCursor(1, 1); 194 lcd.print(c2); 195 } 196 197 198 i1=(c1-48)*10; 199 i2=c2-48; 200 int N_day=i1+i2; 201 delay(500); 202 lcd.clear(); 203 lcd.print("Setup hour"); 204////////////////////////////////////////////////////////////////////////////////////: 205 char keypressed10 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above 206 if (keypressed10 != NO_KEY && keypressed10 !='*' && keypressed10 !='#' && keypressed10 !='A' && keypressed10 !='B' && keypressed10 !='C' && keypressed10 !='D' ) 207 { 208 c1 = keypressed10; 209 lcd.setCursor(0, 1); 210 lcd.print(c1); 211 } 212 char keypressed11 = myKeypad.waitForKey(); 213 if (keypressed11 != NO_KEY && keypressed11 !='*' && keypressed11 !='#' && keypressed11 !='A' && keypressed11 !='B' && keypressed11 !='C' && keypressed11 !='D' ) 214 { 215 c2 = keypressed11; 216 lcd.setCursor(1, 1); 217 lcd.print(c2); 218 } 219 220 221 i1=(c1-48)*10; 222 i2=c2-48; 223 int N_hour=i1+i2; 224 delay(500); 225 lcd.clear(); 226 lcd.print("Setup minutes"); 227////////////////////////////////////////////////////////////////////////////////////: 228 char keypressed12 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above 229 if (keypressed12 != NO_KEY && keypressed12 !='*' && keypressed12 !='#' && keypressed12 !='A' && keypressed12 !='B' && keypressed12 !='C' && keypressed12 !='D' ) 230 { 231 c1 = keypressed12; 232 lcd.setCursor(0, 1); 233 lcd.print(c1); 234 } 235 char keypressed13 = myKeypad.waitForKey(); 236 if (keypressed13 != NO_KEY && keypressed13 !='*' && keypressed13 !='#' && keypressed13 !='A' && keypressed13 !='B' && keypressed13 !='C' && keypressed13 !='D' ) 237 { 238 c2 = keypressed13; 239 lcd.setCursor(1, 1); 240 lcd.print(c2); 241 } 242 243 244 i1=(c1-48)*10; 245 i2=c2-48; 246 int N_minutes=i1+i2; 247 delay(500); 248 lcd.clear(); 249 250 myRTC.setDS1302Time(22, N_minutes, N_hour, 1, N_day, N_month, N_year); //once we're done setting the date and time we transfer to values to the RTC module 251 //the 22 stands for seconds you can add a setup for it too if you want 252 //the 1 stands for day of the week, as long I don't show it on the screen I don't change it 253 keypressed=NO_KEY; //the "*" key is stored in "keypressed" so I remove that value from it otherwise it will get me in the setup again 254 } 255/////////////////////////////////////////Alarme setup///////////////////////////////// 256 257 if (keypressed == 'A'){ 258 lcd.clear(); 259 lcd.print(" Alarm setup "); 260 delay(1000); 261 lcd.clear(); 262 lcd.print("Set alarm hour"); 263 264 char keypressed14 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above 265 if (keypressed14 != NO_KEY && keypressed14 !='*' && keypressed14 !='#' && keypressed14 !='A' && keypressed14 !='B' && keypressed14 !='C' && keypressed14 !='D' ) 266 { 267 c1 = keypressed14; 268 lcd.setCursor(0, 1); 269 lcd.print(c1); 270 } 271 char keypressed15 = myKeypad.waitForKey(); 272 if (keypressed15 != NO_KEY && keypressed15 !='*' && keypressed15 !='#' && keypressed15 !='A' && keypressed15 !='B' && keypressed15 !='C' && keypressed15 !='D' ) 273 { 274 c2 = keypressed15; 275 lcd.setCursor(1, 1); 276 lcd.print(c2); 277 } 278 279 280 i1=(c1-48)*10; 281 i2=c2-48; 282 A_hour=i1+i2; 283 delay(500); 284 lcd.clear(); 285 lcd.print("Set alarm minutes"); 286 char keypressed16 = myKeypad.waitForKey(); // here all programs are stopped until you enter the four digits then it gets compared to the code above 287 if (keypressed16 != NO_KEY && keypressed16 !='*' && keypressed16 !='#' && keypressed16 !='A' && keypressed16 !='B' && keypressed16 !='C' && keypressed16 !='D' ) 288 { 289 c1 = keypressed16; 290 lcd.setCursor(0, 1); 291 lcd.print(c1); 292 } 293 char keypressed17 = myKeypad.waitForKey(); 294 if (keypressed17 != NO_KEY && keypressed17 !='*' && keypressed17 !='#' && keypressed17 !='A' && keypressed17 !='B' && keypressed17 !='C' && keypressed17 !='D' ) 295 { 296 c2 = keypressed17; 297 lcd.setCursor(1, 1); 298 lcd.print(c2); 299 } 300 301 302 i1=(c1-48)*10; 303 i2=c2-48; 304 A_minute=i1+i2; 305 delay(500); 306 lcd.clear(); 307 AlarmIsActive=1; 308 keypressed=NO_KEY; 309 } 310 if (keypressed == 'B') 311 { 312 lcd.clear(); 313 lcd.print("Alarm deactivated"); 314 AlarmIsActive=0; 315 keypressed=NO_KEY; 316 delay(500); 317 } 318 else { 319 myRTC.updateTime(); 320 keypressed=NO_KEY; 321 } 322 323 324} 325
Downloadable files
ds1302 alarm clock schematic
ds1302 alarm clock schematic
ds1302 alarm clock schematic
ds1302 alarm clock schematic
Comments
Only logged in users can leave comments