How To Make Digital Clock Using Sun Board version 2
Hello friends, this is my new project. This is a 24 hours digital clock using arduino and DS3231 module. I used 20×4 I2C lcd module to show big font of digit and also used sun board to make a good looking box. Its very simple to make. Just follow all the step and make your own clock.
Components:
- Sun board
- Arduino nano : https://amzn.to/3tPRBB9
- 20×4 LCD Display :https://amzn.to/2RVZIPx
- DS3231 Module : https://amzn.to/3ePcRTb
- Push Button
- TP-4056 Module https://amzn.to/2S2kugs
- MT-3608 Module https://amzn.to/3u0lwqB
Diagram:
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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
// Build 1 // r1 150214 - initial build with glyphs and big font character table in program memory // r2 150227 - added RTC support //************************************************************ #define build 1 #define revision 2 //************************************************************ #include "avr/pgmspace.h" // for memory storage in program space #include "Wire.h" #include "LiquidCrystal_I2C.h" // library for I@C interface LiquidCrystal_I2C lcd(0x27, 20, 4); #define DS3231_I2C_ADDRESS 0x68 byte decToBcd(byte val){ return( (val/10*16) + (val%10) ); } byte bcdToDec(byte val){ return( (val/16*10) + (val%16) ); } void readDS3231time(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year){ Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set DS3231 register pointer to 00h Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes of data from DS3231 starting from register 00h *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); } void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) { // sets time and date data to DS3231 Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set next input to start at the seconds register Wire.write(decToBcd(second)); // set seconds Wire.write(decToBcd(minute)); // set minutes Wire.write(decToBcd(hour)); // set hours Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) Wire.write(decToBcd(month)); // set month Wire.write(decToBcd(year)); // set year (0 to 99) Wire.endTransmission(); } const char custom[][8] PROGMEM = { {0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0f, 0x1f}, // char 1: top left triangle {0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x1f}, // char 2: upper block {0x00, 0x00, 0x00, 0x00, 0x10, 0x1c, 0x1e, 0x1f}, // char 3: top right triangle {0x1f, 0x0f, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00}, // char 4: bottom left triangle {0x1f, 0x1e, 0x1c, 0x10, 0x00, 0x00, 0x00, 0x00}, // char 5: bottom right triangle {0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00}, // char 6: bottom block {0x1f, 0x1f, 0x1e, 0x1c, 0x18, 0x18, 0x10, 0x10} // char 7: full top right triangle // room for another one! }; const char bn[][30] PROGMEM = { // organized by row // 0 1 2 3 4 5 6 7 8 9 {0x01,0x02,0x03, 0x01,0x02,0xFE, 0x01,0x02,0x03, 0x01,0x02,0x03, 0x02,0xFE,0x02, 0x02,0x02,0x02, 0x01,0x02,0x03, 0x02,0x02,0x02, 0x01,0x02,0x03, 0x01,0x02,0x03}, {0xff,0xfe,0xff, 0xFE,0xFF,0xFE, 0x01,0x02,0xFF, 0xFE,0x02,0xFF, 0xFF,0x02,0xFF, 0xFF,0x02,0x02, 0xFF,0x02,0x03, 0xFE,0x01,0x07, 0xFF,0x02,0xFF, 0xFF,0xFE,0xFF}, {0xff,0xfe,0xff, 0xFE,0xFF,0xFE, 0xFF,0xFE,0xFE, 0xFE,0xFE,0xFF, 0xFE,0xFE,0xFF, 0xFE,0xFE,0xFF, 0xFF,0xFE,0xFF, 0xFE,0xFF,0xFE, 0xFF,0xFE,0xFF, 0x04,0x06,0xFF}, {0x04,0x06,0x05, 0xFE,0x06,0xFE, 0x06,0x06,0x06, 0x04,0x06,0x05, 0xFE,0xFE,0x06, 0x04,0x06,0x05, 0x04,0x06,0x05, 0xFE,0x06,0xFE, 0x04,0x06,0x05, 0xFE,0xFE,0x06} }; byte col, row, nb=0, bc=0; // general byte bb[8]; // byte buffer for reading from PROGMEM int Mode = 0; #include "RTClib.h" RTC_Millis RTC; byte hr, mn, se, osec; byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; volatile boolean buttonA = false; volatile boolean buttonB = false; volatile boolean buttonC = false; int StateOfbuttonA = 0; int StateOfbuttonB = 0; int StateOfbuttonC = 0; int NewStateOfbuttonA = 0; int NewStateOfbuttonB = 0; int NewStateOfbuttonC = 0; uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C char *mon2str(uint8_t mon, char *psz, uint8_t len) // Get a label from PROGMEM into a char array { const __FlashStringHelper* const Jan = F("Jan"); const __FlashStringHelper* const Feb = F("Feb"); const __FlashStringHelper* const Mar = F("Mar"); const __FlashStringHelper* const Apr = F("Apr"); const __FlashStringHelper* const May = F("May"); const __FlashStringHelper* const Jun = F("Jun"); const __FlashStringHelper* const Jul = F("Jul"); const __FlashStringHelper* const Aug = F("Aug"); const __FlashStringHelper* const Sep = F("Sep"); const __FlashStringHelper* const Oct = F("Oct"); const __FlashStringHelper* const Nov = F("Nov"); const __FlashStringHelper* const Dec = F("Dec"); const __FlashStringHelper* str[] = { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; } char *dow2str(uint8_t code, char *psz, uint8_t len) { const __FlashStringHelper* const Sunday = F("Sunday"); const __FlashStringHelper* const Monday = F("Monday"); const __FlashStringHelper* const Tuesday = F("Tuesday"); const __FlashStringHelper* const Wednesday = F("Wednesday"); const __FlashStringHelper* const Thursday = F("Thursday"); const __FlashStringHelper* const Friday = F("Friday"); const __FlashStringHelper* const Saturday = F("Saturday"); const __FlashStringHelper* str[] = { Sunday, Monday, Tuesday, Wednesday, Thursday,Friday, Saturday ,Sunday }; } //*********************** INITIAL SETUP *********************** void setup() { randomSeed(analogRead(0)); RTC.begin(DateTime(__DATE__, __TIME__)); Serial.begin(9600); lcd.init(); // initialize the LCD lcd.backlight(); lcd.begin(20, 4); for (nb=0; nb<7; nb++ ) { // create 8 custom characters for (bc=0; bc<8; bc++) bb[bc]= pgm_read_byte( &custom[nb][bc] ); lcd.createChar( nb+1, bb ); } lcd.clear(); lcd.setCursor(4, 0); lcd.print(F("4-Line LARGE")); lcd.setCursor(4, 1); lcd.print(F("TIME DISPLAY")); lcd.setCursor(5, 3); lcd.print(F("V")); lcd.print(build); lcd.print(F(".")); lcd.print(revision); lcd.print(F(" ")); //lcd.print(freeRam()); lcd.print(F("B")); // printNum(random(0,10),0); // printNum(random(0,10),17); delay(500); lcd.clear(); readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //hour=hour+1; //setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); //setDS3231time(30,42,21,4,26,11,14); } //************************* MAIN LOOP ************************ void loop() { datetinme_print(); timesetup(); } /* // ******************* OPERATION ROUTINES ******************** // FREERAM: Returns the number of bytes currently free in RAM int freeRam(void) { extern int __bss_end, *__brkval; int free_memory; if((int)__brkval == 0) { free_memory = ((int)&free_memory) - ((int)&__bss_end); } else { free_memory = ((int)&free_memory) - ((int)__brkval); } return free_memory; }*/ void datetinme_print(){ readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); // DateTime now = RTC.now(); hr =hour; mn = minute; se = second; if(se != osec) { printNum(hr/10,0); printNum(hr%10,3); printColon(6); printNum(mn/10,7); printNum(mn%10,10); printColon(13); printNum(se/10,14); printNum(se%10,17); osec = se; } delay(50); // not strictly necessary } // ********************** SUBROUTINES ************************ void printNum(byte digit, byte leftAdjust) { for(row=0; row<4; row++) { lcd.setCursor(leftAdjust,row); for(col=digit*3; col <digit*3+3; col++) { lcd.write(pgm_read_byte( &bn[row][col]) ); } } } void printColon(byte leftAdjust) { for(row=0; row<4; row++) { lcd.setCursor(leftAdjust,row); if(row == 1 || row == 2) lcd.print(F(".")); else lcd.print(F(" ")); } } void timesetup(){ Serial.println(Mode); NewStateOfbuttonA = digitalRead(3); NewStateOfbuttonB = digitalRead(4); NewStateOfbuttonC = digitalRead(5); buttonAisPressed(); buttonBisPressed(); buttonCisPressed(); if (buttonB) { buttonB = false; Mode++; if (Mode > 4 ) { Mode = 0; } } if (buttonA) { if (Mode == 0 ) { buttonA = false; /* contrast++; if (contrast >= 51 ) { contrast = 50;*/ } else if (Mode == 1 ) { buttonA = false; // Mode = 0; } else if (Mode == 2 ) { buttonA = false; hour++; if (hour >= 24 ) { hour = 0; } Serial.println("up"); setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 3 ) { buttonA = false; minute++; if (minute >= 60 ) { minute = 0; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 4 ) { buttonA = false; second = 0; setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 5 ) { buttonA = false; dayOfWeek++; if (dayOfWeek >= 8 ) { dayOfWeek = 1; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); //P.displayReset(2); } else if (Mode == 6 ) { buttonA = false; dayOfMonth++; if (dayOfMonth >= 32 ) { dayOfMonth = 1; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 7 ) { buttonA = false; month++; if (month >= 13 ) { month = 1; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 8 ) { buttonA = false; year++; if (year >= 2035 ) { year = 2015; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } } if (buttonC) { if (Mode == 0 ) { buttonC = false; // contrast--; // if (contrast <= 0 ) { // contrast = 0; //} } else if (Mode == 1 ) { buttonC = false; // Mode = 0; } else if (Mode == 2 ) { buttonC = false; hour--; if (hour <= 0 ) { hour = 23; } Serial.println("Down"); setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 3 ) { buttonC = false; minute--; if (minute <= 0 ) { minute = 59; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 4 ) { buttonC = false; second = 0; setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 5 ) { buttonC = false; dayOfWeek--; if (dayOfWeek <= 0 ) { dayOfWeek = 7; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); //P.displayReset(2); } else if (Mode == 6 ) { buttonC = false; dayOfMonth--; if (dayOfMonth <= 0 ) { dayOfMonth = 31; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 7 ) { buttonC = false; month--; if (month <= 0 ) { month = 12; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year); } else if (Mode == 8 ) { buttonC = false; year--; if (year <= 2010 ) { year = 2025; } setDS3231time(second,minute, hour, dayOfWeek, dayOfMonth, month,year);; } } if (Mode == 0) { static uint32_t lastTime = 0; // millis() memory static bool flasher = false; // seconds passing flasher if (millis() - lastTime >= 1000) { lastTime = millis(); flasher = !flasher; } } if (Mode == 1) { static uint8_t display = 0; // current display mode { switch (display) { case 0: // Time datetinme_print(); break; case 1: // Day break; case 2: // Calendar break; } } } if (Mode == 2) { static uint32_t lastTime = 0; // millis() memory static bool flasher = false; // seconds passing flasher if (millis() - lastTime >= 200) { lastTime = millis(); // lcd.setCursor(4,1); //lcd.setCursor(6,4); lcd.cursor(); printNum(hr/10,0); printNum(hr%10,3); flasher = !flasher; } } if (Mode == 3) { static uint32_t lastTime = 0; // millis() memory static bool flasher = false; // seconds passing flasher if (millis() - lastTime >= 200) { lastTime = millis(); printColon(6); printNum(mn/10,7); printNum(mn%10,10); flasher = !flasher; } } if (Mode == 4) { static uint32_t lastTime = 0; // millis() memory static bool flasher = false; // seconds passing flasher if (millis() - lastTime >= 200) { lastTime = millis(); printColon(13); printNum(se/10,14); printNum(se%10,17); } } if (Mode == 5) { static uint8_t display = 0; // current display mode } if (Mode == 6) { /* P.displayAnimate(); P.getZoneStatus(2); P.setTextEffect(2, PA_PRINT, PA_NO_EFFECT); getdyy(szMesg); P.displayReset(2);*/ } if (Mode == 7) { /*P.displayAnimate(); P.getZoneStatus(2); P.setTextEffect(2, PA_PRINT, PA_NO_EFFECT); getmon(szMesg); P.displayReset(2);*/ } if (Mode == 8) { /* P.displayAnimate(); P.getZoneStatus(2); P.setTextEffect(2, PA_PRINT, PA_NO_EFFECT); getyyyy(szMesg); P.displayReset(2);*/ } } void buttonAisPressed() { if (NewStateOfbuttonA != StateOfbuttonA) { if (NewStateOfbuttonA == 0) { buttonA=true; } delay(50); } StateOfbuttonA = NewStateOfbuttonA; } void buttonBisPressed() { if (NewStateOfbuttonB != StateOfbuttonB) { if (NewStateOfbuttonB == 0) { buttonB=true; } delay(50); } StateOfbuttonB = NewStateOfbuttonB; } void buttonCisPressed() { if (NewStateOfbuttonC != StateOfbuttonC) { if (NewStateOfbuttonC == 0) { buttonC=true; } delay(50); } StateOfbuttonC = NewStateOfbuttonC; } |
Hi, I’m Mahmud. I would like to introduce you to one of my websites where you will find all kinds of WordPress & WooCommerce related Premium Tools absolutely free. That means you don’t have to spend money to buy your necessary WordPress Tools. Usually, all of our tools are bought from official developers with unlimited licenses and then our professional developer’s team creates an activated version of the product carefully for multiple domain usages.
.
So It’s guaranteed that the Plugins or Themes weren’t contained any kind of viruses or hidden malware. Our files are completely clean and secured. So you can use these files without any hesitation. Also, you can be sure to scan any file with VirusTotal.Com before using it.
.
Website Link: https://nulled4all.com
.
Thank You
Mahmud Ghazni