Clock ,RTC DS1307 SET AND SHOW THE TIME ON YOUR LCD.

108K7534

Intro: Clock ,RTC DS1307 SET AND SHOW THE TIME ON YOUR LCD.

Based on my first Instructable.
Based on WWC SKETCH

Add Rtc(realtimeclock) to your Instructable .

Items
1 Arduino
1 Usb conector wire
1 RTC DS1307
1 Wires
1 Device to upload scetch.
1 time / DS1307RTC  libraries http://playground.arduino.cc/Code/time

STEP 1:

conect rtc to arduino.

conect  SDA  to Arduino A4.
conect  SCL  to Arduino A5.
conect vcc to  +5v
conect gnd to gnd

STEP 2: Conect Lcd

LCD to Arduino
1 GND
2 +
3 center of potentiometer
4 pin 12 Arduino.
5 GND
6 Pin 11 Arduino
11 Pin 5 Arduino
12 Pin 4 Arduino
13 Pin 3 Arduino
14 Pin 2 Arduino
15 +
16 Gnd

STEP 3: The Set Rtc Sketch From Member WWC. Don't Use This Example Where U Change the Time.

the serial input didn 't work for me so i ad the time to the sketch.

dont use this one . only for EXAMPLE where u have to enter the time .

// This set of codes is allows input of data
void setTime() {
  Serial.print("Please enter the current year, 00-99. - ");
  year = 13;
  Serial.println(year);
  Serial.print("Please enter the current month, 1-12. - ");
  month = 9;
  Serial.println(months[month-1]);
  Serial.print("Please enter the current day of the month, 1-31. - ");
  monthday = 20;
  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 = 6;
  Serial.println(days[weekday-1]);
  Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
  hour = 13;
  Serial.println(hour);
  Serial.print("Please enter the current minute, 0-59. - ");
  minute = 51;
  Serial.println(minute);
  second = 15;
  Serial.println("The data has been entered.");

STEP 4: The Set Rtc Sketch From Member WWC. Take This.

you have to enter your time ,upload and then open serial and hit y and send.

//////////////////////////////////////////
// RTC data and time setter              //
//                                       //
// This sample program allows the user   //
// to set the date and time of an RTC    //
// using I2C.                            //
//                                       //
// Codes by:                             //
// eGizmo Mechatronix Central            //
// Taft, Manila, Philippines             //
// http://www.egizmo.com                 //
// April 15, 2013                        //
///////////////////////////////////////////

#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"};

// Initializes all values:
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  delay(2000); // This delay allows the MCU to read the current date and time.

  Serial.print("The current date and time is: ");
  printTime();
  Serial.println("Please change to newline ending the settings on the lower right of the Serial Monitor");
  Serial.println("Would you like to set the date and time now? Y/N");

  while (!Serial.available()) delay(10);
  if (Serial.read() == 'y' || Serial.read() == 'Y')

  // This set of functions allows the user to change the date and time
  {
    Serial.read();
    setTime();
    Serial.print("The current date and time is now: ");
    printTime();
  }


  Serial.println("Thank you.");
}

// Continuous function for converting bytes to decimals and vice versa
void loop() {
}
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 = 13;
  Serial.println(year);
  Serial.print("Please enter the current month, 1-12. - ");
  month = 9;
  Serial.println(months[month-1]);
  Serial.print("Please enter the current day of the month, 1-31. - ");
  monthday = 20;
  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 = 6;
  Serial.println(days[weekday-1]);
  Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
  hour = 13;
  Serial.println(hour);
  Serial.print("Please enter the current minute, 0-59. - ");
  minute = 51;
  Serial.println(minute);
  second = 15;
  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];
  const char* AMPM = 0;
  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(":");
  sprintf(buffer, "%02d", minute);
  Serial.print(buffer);
  Serial.println(AMPM);
}


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 5: Now Upload This to See the Time on Your Lcd.

/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()  {
  lcd.begin(16, 2);



  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");

}

void loop()
{
   digitalClockDisplay(); 
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();

lcd.setCursor(2, 0);                                            // Set LCD cursor position (column, row)
lcd.print(hour());
lcd.print(":");
lcd.print (minute());
lcd.print(" ");
lcd.print(second());
lcd.print(" ");
lcd.print("Uhr");
lcd.setCursor(3, 1);                                           // Set LCD cursor position (column,row) 
lcd.print(day());
lcd.print(".");
lcd.print(month());
lcd.print(".");
lcd.print(year());

// Print text to LCD
                                     // Delay to read text
                               // Clear the display

}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

25 Comments

Finally a code that works with my setup. Thank you sooooo verrrrry much!!!
Cool . have fun.

nothing is showing om my lcd screen

Try turning the potentiometer until something shows up.
I thought this was cool, so reworked the sketch to make it more relevant for my area:

/*
* This works great. Altered to keep spacing, print the month.
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
* RTC: SDA - A4 / SCL - A5
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
lcd.clear();
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){ // digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
switch(month()){
case 1:
Serial.print("JAN");
break;
case 2:
Serial.print("FEB");
break;
case 3:
Serial.print("MAR");
break;
case 4:
Serial.print("APR");
break;
case 5:
Serial.print("MAY");
break;
case 6:
Serial.print("JUN");
break;
case 7:
Serial.print("JUN");
break;
case 8:
Serial.print("JUL");
break;
case 9:
Serial.print("SEP");
break;
case 10:
Serial.print("OCT");
break;
case 11:
Serial.print("NOV");
break;
case 12:
Serial.print("DEC");
break;
}
Serial.print(" ");
if (day() , 10)
Serial.print("0");
Serial.print(day());
Serial.print(",");
Serial.print(year());
Serial.println();
lcd.setCursor(2, 0); // Set LCD cursor position (column, row)
if (hour() < 10)
lcd.print("0");
lcd.print(hour());
lcd.print(":");
if (minute() < 10)
lcd.print("0");
lcd.print (minute());
lcd.print(":");
if (second() < 10)
lcd.print("0");
lcd.print(second());
lcd.print(" ");
lcd.print("MST");
lcd.setCursor(2, 1); // Set LCD cursor position (column,row)
switch(month()){
case 1:
lcd.print("JAN");
break;
case 2:
lcd.print("FEB");
break;
case 3:
lcd.print("MAR");
break;
case 4:
lcd.print("APR");
break;
case 5:
lcd.print("MAY");
break;
case 6:
lcd.print("JUN");
break;
case 7:
lcd.print("JUN");
break;
case 8:
lcd.print("JUL");
break;
case 9:
lcd.print("SEP");
break;
case 10:
lcd.print("OCT");
break;
case 11:
lcd.print("NOV");
break;
case 12:
lcd.print("DEC");
break;
}
lcd.print(" ");
if (day() < 10)
lcd.print("0");
lcd.print(day());
lcd.print(",");
lcd.print(year());
// Print text to LCD
// Delay to read text
// Clear the display
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
This was a bit confusing, but in the end, works fine. What was confusing was the sketch to set the time in the RTC, because the LCD stayed blank. Also it didn't let me set the time in the serial monitor. I ended up setting the time in the sketch and uploaded it. I'm guessing the UHR is Universal hour? End result: very nice! Altered mine: Mon-day-year and AZSTD for Arizona standard time. Suggestion: the fill for digit less than 10 works great in the serial monitor, but not for the LCD Display, so I edited the sketch like this:
lcd.setCursor(2, 0); // Set LCD cursor position (column, row)
if (hour() < 10)
lcd.print("0");
lcd.print(hour());
lcd.print(":");
if (minute() < 10)
lcd.print("0");
lcd.print (minute());
lcd.print(":");
if (second() < 10)
lcd.print("0");
lcd.print(second());
lcd.print(" ");
lcd.print("MST");
lcd.setCursor(3, 1); // Set LCD cursor position (column,row)
if (month() < 10)
lcd.print("0");
lcd.print(month());
lcd.print(".");
if (day() < 10)
lcd.print("0");
lcd.print(day());
lcd.print(".");
lcd.print(year());

That way all the spacing stays the same in the display.
Also using a DS3231 instead of the DS1307 and it works the same with this code.
ITS because Off the serial transfer didn't work for me. So you have to load the time by the descripted part (step3)where U Set the time. I used the workaround. Uhr means clock ,we say in Germany ITS 5 a clock = es ist jetzt 5 Uhr. If your edited Script Part Work for direkt serial Input, i will try IT .
Greatings
I edited the script for the correct time/date. I didn't get it to work over the serial monitor.

I have been trying for months to show the time on my LCD using Mega 2560,RTC 1307 and a 16x2 LCD with no luck(can't even find any sketches)well with urs I get 'setSyncProvider' was not declared in this scope on line setSyncProvider(RTC.get); // the function to get the time from the RTC is the problems don't know what this means!

Load the scetch and search the marked numbers from step 3.
Go to step 3 .
Enter the time in the Sketch.

the serial input didn 't work for me so i ad the time to the sketch.

In step 3 you will see here u have to enter the time .

how i can make time on display get am-pm?

Hello When I try to upload the sketch it gives the error

'setSyncProvider' was not declared in this scope

Can you please help

Very nice! What would the code be if I wanted to reduce lcd wiring to 4 wires with an I2C?
? don t know.

What happen when RTC module ran out of battery? Do I have to connect the Arduino and reload the sketch?

how to set alarm using same library on rtc arduino??or timed event?

plz helppp

More Comments