Components and supplies
Resistor 1k ohm
Resistor 10k ohm
Arduino Nano R3
Jumper wires (generic)
Solderless Breadboard Full Size
Pi RTC (DS1307)
Alphanumeric LCD, 16 x 2
Rotary potentiometer (generic)
Apps and platforms
Arduino IDE
Project description
Code
Arduino DS1307 RTC code
c_cpp
copy and paste on your editor
1// Real time clock and calendar with set buttons using DS1307 and Arduino 2 3// include LCD library code 4#include <LiquidCrystal.h> 5// include Wire library code (needed for I2C protocol devices) 6#include <Wire.h> 7 8// LCD module connections (RS, E, D4, D5, D6, D7) 9LiquidCrystal lcd(5,6,7,8,9,10); 10 11void setup() { 12 pinMode(3, INPUT_PULLUP); // button1 is connected to pin 8 13 pinMode(4, INPUT_PULLUP); // button2 is connected to pin 9 14 // set up the LCD's number of columns and rows 15 lcd.begin(16, 2); 16 Wire.begin(); // Join i2c bus 17} 18 19char Time[] = "TIME: : : "; 20char Calendar[] = "DATE: / /20 "; 21byte i, second, minute, hour, date, month, year; 22 23void DS1307_display(){ 24 // Convert BCD to decimal 25 second = (second >> 4) * 10 + (second & 0x0F); 26 minute = (minute >> 4) * 10 + (minute & 0x0F); 27 hour = (hour >> 4) * 10 + (hour & 0x0F); 28 date = (date >> 4) * 10 + (date & 0x0F); 29 month = (month >> 4) * 10 + (month & 0x0F); 30 year = (year >> 4) * 10 + (year & 0x0F); 31 // End conversion 32 Time[12] = second % 10 + 48; 33 Time[11] = second / 10 + 48; 34 Time[9] = minute % 10 + 48; 35 Time[8] = minute / 10 + 48; 36 Time[6] = hour % 10 + 48; 37 Time[5] = hour / 10 + 48; 38 Calendar[14] = year % 10 + 48; 39 Calendar[13] = year / 10 + 48; 40 Calendar[9] = month % 10 + 48; 41 Calendar[8] = month / 10 + 48; 42 Calendar[6] = date % 10 + 48; 43 Calendar[5] = date / 10 + 48; 44 lcd.setCursor(0, 0); 45 lcd.print(Time); // Display time 46 lcd.setCursor(0, 1); 47 lcd.print(Calendar); // Display calendar 48} 49void blink_parameter(){ 50 byte j = 0; 51 while(j < 10 && digitalRead(3) && digitalRead(4)){ 52 j++; 53 delay(25); 54 } 55} 56byte edit(byte x, byte y, byte parameter){ 57 char text[3]; 58 while(!digitalRead(3)); // Wait until button (pin #8) released 59 while(true){ 60 while(!digitalRead(4)){ // If button (pin #9) is pressed 61 parameter++; 62 if(i == 0 && parameter > 23) // If hours > 23 ==> hours = 0 63 parameter = 0; 64 if(i == 1 && parameter > 59) // If minutes > 59 ==> minutes = 0 65 parameter = 0; 66 if(i == 2 && parameter > 31) // If date > 31 ==> date = 1 67 parameter = 1; 68 if(i == 3 && parameter > 12) // If month > 12 ==> month = 1 69 parameter = 1; 70 if(i == 4 && parameter > 99) // If year > 99 ==> year = 0 71 parameter = 0; 72 sprintf(text,"%02u", parameter); 73 lcd.setCursor(x, y); 74 lcd.print(text); 75 delay(200); // Wait 200ms 76 } 77 lcd.setCursor(x, y); 78 lcd.print(" "); // Display two spaces 79 blink_parameter(); 80 sprintf(text,"%02u", parameter); 81 lcd.setCursor(x, y); 82 lcd.print(text); 83 blink_parameter(); 84 if(!digitalRead(3)){ // If button (pin #8) is pressed 85 i++; // Increament 'i' for the next parameter 86 return parameter; // Return parameter value and exit 87 } 88 } 89} 90 91void loop() { 92 if(!digitalRead(3)){ // If button (pin #8) is pressed 93 i = 0; 94 hour = edit(5, 0, hour); 95 minute = edit(8, 0, minute); 96 date = edit(5, 1, date); 97 month = edit(8, 1, month); 98 year = edit(13, 1, year); 99 // Convert decimal to BCD 100 minute = ((minute / 10) << 4) + (minute % 10); 101 hour = ((hour / 10) << 4) + (hour % 10); 102 date = ((date / 10) << 4) + (date % 10); 103 month = ((month / 10) << 4) + (month % 10); 104 year = ((year / 10) << 4) + (year % 10); 105 // End conversion 106 // Write data to DS1307 RTC 107 Wire.beginTransmission(0x68); // Start I2C protocol with DS1307 address 108 Wire.write(0); // Send register address 109 Wire.write(0); // Reset sesonds and start oscillator 110 Wire.write(minute); // Write minute 111 Wire.write(hour); // Write hour 112 Wire.write(1); // Write day (not used) 113 Wire.write(date); // Write date 114 Wire.write(month); // Write month 115 Wire.write(year); // Write year 116 Wire.endTransmission(); // Stop transmission and release the I2C bus 117 delay(200); // Wait 200ms 118 } 119 Wire.beginTransmission(0x68); // Start I2C protocol with DS1307 address 120 Wire.write(0); // Send register address 121 Wire.endTransmission(false); // I2C restart 122 Wire.requestFrom(0x68, 7); // Request 7 bytes from DS1307 and release I2C bus at end of reading 123 second = Wire.read(); // Read seconds from register 0 124 minute = Wire.read(); // Read minuts from register 1 125 hour = Wire.read(); // Read hour from register 2 126 Wire.read(); // Read day from register 3 (not used) 127 date = Wire.read(); // Read date from register 4 128 month = Wire.read(); // Read month from register 5 129 year = Wire.read(); // Read year from register 6 130 DS1307_display(); // Diaplay time & calendar 131 delay(50); // Wait 50ms 132}
Downloadable files
Arduino RTC clock schematic
connect all the hardware according to it
Arduino RTC clock schematic
Arduino RTC clock schematic
connect all the hardware according to it
Arduino RTC clock schematic
Comments
Only logged in users can leave comments
ramjipatel376
4 years ago
excellent project