
After reading this post the reader will be able to learn about the basics of the rtc ds1307 clock, working of the real time clock module interface with arduino, the principle of the rtc clock, circuit for alarm clock, interfacing of the Arduino microcontroller development board with ds1307 rtc module. So sit back, keep reading and enjoy learning.
Arduino Alarm Clock using Rtc ds1307
arduino rtc ds1307 library download
Arduino Alarm Clock using Rtc ds1307 Circuit Diagram
Components Required
- Arduino Nano OR Arduino UNO
- rtc ds1307
- lcd 2×16 or lcd 4x 20
- 5 Voltage Buzzer
- Push Buttons
- Bread Bord
- Resistor,capacitor and leds
- Jumper Wires
Arduino Lcd Clock with Rtc Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | int sw1=8; int sw2=9; int sw3=10; int sw4=12; #include<EEPROM.h> #include <LiquidCrystal.h> LiquidCrystal lcd(7, 6, 5, 4, 3, 2); const char* AMPM = 0; int cnt4=0; #include <Wire.h> const int DS1307 = 0x68; // Address of DS1307 see data sheets const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; const char* months[] = //{"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"}; {"Jan", "Feb", "March", "April", "May", "June", "July", "August","Septem", "Octobe", "Novem", "Decemb"}; // Initializes all values: byte second = 0; byte minute = 0; byte hour = 0; byte weekday = 0; byte monthday = 0; byte month = 0; byte year = 0; byte fminute = 0; byte fhour = 0; byte ffminute = 0; byte ffhour = 0; byte zminute = 0; byte zhour = 0; byte aminute = 0; byte ahour = 0; byte mminute = 0; byte mhour = 0; byte iminute = 0; byte ihour = 0; int addr = 0; byte aaminute = 0; byte aahour = 0; int rly1 = A0; void setup() { delay(10); Wire.begin(); delay(10); lcd.begin(16, 2);Serial.begin(9600);lcd.clear(); delay(300); pinMode(rly1, OUTPUT); digitalWrite(rly1, HIGH); pinMode(sw1, INPUT_PULLUP); pinMode(sw2, INPUT_PULLUP); pinMode(sw3, INPUT_PULLUP); pinMode(sw4, INPUT_PULLUP); addr=1;fhour = EEPROM.read(addr); addr=2;fminute = EEPROM.read(addr); addr=3;zhour = EEPROM.read(addr); addr=4;zminute = EEPROM.read(addr); addr=5;ahour = EEPROM.read(addr); addr=6;aminute = EEPROM.read(addr); addr=7;mhour = EEPROM.read(addr); addr=8;mminute = EEPROM.read(addr); addr=9;ihour = EEPROM.read(addr); addr=10;iminute = EEPROM.read(addr); addr=11;aahour = EEPROM.read(addr); addr=12;aaminute = EEPROM.read(addr); addr=13;ffhour = EEPROM.read(addr); addr=14;ffminute = EEPROM.read(addr); addr=15;cnt4 = EEPROM.read(addr); // Continuous function for converting bytes to decimals and vice versa void loop() { printTime(); delay(500); chk(); chk2(); chkalm(); } void chkalm(){ if(fhour==hour){ if(fminute==minute){ lcd.clear(); lcd.setCursor(0, 0); lcd.print(" alarm ");delay(1500); digitalWrite(rly1, LOW); delay(1500); //command ( 0x03 , 1 ); alarmd();//delay(60000); digitalWrite(rly1, HIGH); } } } void alarmd(){ delay(2000); } void chk2(){ if(digitalRead(sw4) == LOW){ lcd.clear(); lcd.setCursor(0, 0); lcd.print("enter hour");delay(500); while(digitalRead(sw4) == LOW){delay(500); } while(digitalRead(sw4) == HIGH){delay(300); if(digitalRead(sw2) == LOW){ lcd.setCursor(6, 1); lcd.print(" "); fhour++; lcd.setCursor(6, 1); lcd.print(fhour);delay(100); } if(digitalRead(sw3) == LOW){ fhour--; lcd.setCursor(6, 1); lcd.print(" "); lcd.setCursor(6, 1); lcd.print(fhour);delay(100); }} lcd.clear(); lcd.setCursor(0, 0); lcd.print("enter minute");delay(500); while(digitalRead(sw4) == LOW){delay(500); } while(digitalRead(sw4) == HIGH){delay(300); if(digitalRead(sw2) == LOW){ lcd.setCursor(6, 1); lcd.print(" "); fminute++; lcd.setCursor(6, 1); lcd.print(fminute);delay(100); } if(digitalRead(sw3) == LOW){ fminute--; lcd.setCursor(6, 1); lcd.print(" "); lcd.setCursor(6, 1); lcd.print(fminute);delay(100); }} addr=1; EEPROM.write(addr,fhour); addr=2; EEPROM.write(addr,fminute); } } void chk(){ if(digitalRead(sw1) == LOW){ lcd.clear(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("enter hour");delay(500); while(digitalRead(sw1) == LOW){delay(500); } while(digitalRead(sw1) == HIGH){delay(300); if(digitalRead(sw2) == LOW){ lcd.setCursor(6, 1); lcd.print(" "); hour++; lcd.setCursor(6, 1); lcd.print(hour);delay(100); } if(digitalRead(sw3) == LOW){ hour--; lcd.setCursor(6, 1); lcd.print(" "); lcd.setCursor(6, 1); lcd.print(hour);delay(100); }} lcd.clear(); lcd.setCursor(0, 0); lcd.print("enter minute");delay(500); while(digitalRead(sw1) == LOW){delay(500); } while(digitalRead(sw1) == HIGH){delay(300); if(digitalRead(sw2) == LOW){ lcd.setCursor(6, 1); lcd.print(" "); minute++; lcd.setCursor(6, 1); lcd.print(minute);delay(100); } if(digitalRead(sw3) == LOW){ minute--; lcd.setCursor(6, 1); lcd.print(" "); lcd.setCursor(6, 1); lcd.print(minute);delay(100); }} second=0; //*/ // The following codes transmits the data to the RTC Wire.beginTransmission(DS1307); Wire.write(byte(0)); Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(weekday)); Wire.write(decToBcd(monthday)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.write(byte(0)); Wire.endTransmission(); // Ends transmission of data } } byte decToBcd(byte val) { return ((val/10*16) + (val%10)); } byte bcdToDec(byte val) { return ((val/16*10) + (val%16)); } // This set of codes is allows input of data void setTime() { Serial.print("Please enter the current year, 00-99. - "); year = readByte(); Serial.println(year); Serial.print("Please enter the current month, 1-12. - "); month = readByte(); Serial.println(months[month-1]); Serial.print("Please enter the current day of the month, 1-31. - "); monthday = readByte(); Serial.println(monthday); Serial.println("Please enter the current day of the week, 1-7."); Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - "); weekday = readByte(); Serial.println(days[weekday-1]); Serial.print("Please enter the current hour in 24hr format, 0-23. - "); hour = readByte(); Serial.println(hour); Serial.print("Please enter the current minute, 0-59. - "); minute = readByte(); Serial.println(minute); second = 0; Serial.println("The data has been entered."); // The following codes transmits the data to the RTC Wire.beginTransmission(DS1307); Wire.write(byte(0)); Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(weekday)); Wire.write(decToBcd(monthday)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.write(byte(0)); Wire.endTransmission(); // Ends transmission of data } byte readByte() { while (!Serial.available()) delay(10); byte reading = 0; byte incomingByte = Serial.read(); while (incomingByte != '\n') { if (incomingByte >= '0' && incomingByte <= '9') reading = reading * 10 + (incomingByte - '0'); else; incomingByte = Serial.read(); } Serial.flush(); return reading; } void printTime() { char buffer[3]; readTime(); /* Serial.print(days[weekday-1]); Serial.print(" "); Serial.print(months[month-1]); Serial.print(" "); Serial.print(monthday); Serial.print(", 20"); Serial.print(year); Serial.print(" "); */ if (hour > 12) { hour -= 12; AMPM = " PM"; } else AMPM = " AM"; /* Serial.print(hour); Serial.print("y:"); sprintf(buffer, "%02d", minute); Serial.print(buffer); Serial.println(AMPM); */ sprintf(buffer, "%02d", minute); lcd.setCursor(0, 0); lcd.print(hour); lcd.print(":"); lcd.print(buffer); lcd.print(" "); lcd.print(second); lcd.print(AMPM); lcd.print(" "); ///* lcd.print(days[weekday-1]); lcd.setCursor(0, 1); lcd.print(monthday); lcd.print(" "); lcd.print(months[month-1]); lcd.print(" "); lcd.print("20"); lcd.print(year); //*/ } void readTime() { Wire.beginTransmission(DS1307); Wire.write(byte(0)); Wire.endTransmission(); Wire.requestFrom(DS1307, 7); second = bcdToDec(Wire.read()); minute = bcdToDec(Wire.read()); hour = bcdToDec(Wire.read()); weekday = bcdToDec(Wire.read()); monthday = bcdToDec(Wire.read()); month = bcdToDec(Wire.read()); year = bcdToDec(Wire.read()); } |
Step by Step Tutorial Arduino Alarm Clock using Rtc ds1307.we make arduino nano projects our next project is water level indicator using arduino with buzzer