Go Down

Topic: current date and time (Read 20811 times) previous topic - next topic


i'm able to interface and lcd and rtc to arduino severino
now i just want to display current date and time on lcd nd save it on rtc
i just don't know how to get the current date and time from pc


Depending on the language you use, you can get the current time by calling a function like CTime::getCurrentTime (C++), or Now (vbscript).


Apr 30, 2009, 12:15 pm Last Edit: Apr 30, 2009, 12:15 pm by alokgvc Reason: 1
i'm using standard c in the arduino IDE
i tried downloading the datetime library but there is no responce.
so can u please tell me the complete code for date/time, coz i'm bit weak at my programming side  :-[


Apr 30, 2009, 01:22 pm Last Edit: Apr 30, 2009, 01:23 pm by mem Reason: 1
The DateTime library example sketch will only display the time after its internal clock is set. You can test that it is working by setting an arbitrary time - add this line at the end of setup :
   DateTime.sync(1241094085); // set the time to 04/30/2009 @ 7:21am.


The code you write in Arduino IDE is meant to be uploaded to the Arduino board, so it won't run on your PC. It does not make sense to get the current time from PC when you don't run that code on the PC, does it?
The time synchronization between your PC and Arduino should happen as a result of a program running on PC (which gets the current time) sending (on serial port) the current time to the sketch running on Arduino.
Isn't this what you want to do?


sir, that's precisely what i want to do, synchronise time between PC and Arduino
i know how to enter time in rtc, and do stuff with it
but for now, entering current date everytime calls for changing values, which i want to skip


> entering current date everytime calls for changing values, which i want to skip

I don't understand what you are saying. Did you try the example Processing sketch that gets the time from the PC and sends a message to arduino that sets the clock to that time?


ok sire here is my exact problem
i'm using arduino severino and interfaced rtc PCF8563 to it.
i've not attached a battery to it which means the time is reset every time there is power failure. so i have to manully write new values in code corresponding to current time to set the time of rtc to current time.

i tried the code of datetime library of arduino but neither was there any display/serial output nor was the led blinking.

so i just want a piece of code(as i'm not perfect at programming) for my arduino that sets the rtc time to current time and saves me from manually editing the value.

the code i wrote is available at open-embedded.blogspot.com


The question still remains: where do you want to get the time from? From the PC (thus connecting the arduino to the PC), from the user inputting (via buttons, IR etc) some numbers?


florinc's question is valid, but if you want to set the time using the example Processing sketch posted in the playground, you can use this arduino code to get the time component you would need to set a RTC.

Code: [Select]

#include <DateTime.h>

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

void setup(){

void  loop(){
 getPCtime(); // try to get time from the PC

boolean getPCtime() {
 // if time sync available from serial port, update time and return true
 while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
   if( Serial.read() == TIME_HEADER ) {        
     time_t pctime = 0;
     for(int i=0; i < TIME_MSG_LEN -1; i++){  
       char c= Serial.read();          
       if( c >= '0' && c <= '9'){  
         pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
     setTime(pctime);   // Sync Arduino clock to the time received on the serial port
     return true;   // return true if time message received on the serial port
 return false;  //if no message return false

void setTime( time_t time)
 byte Second,Minute,Hour,Day,DayofWeek,Month, Year; // note year is actual year minus 2000 (ie 9 is 2009)
 DateTime.localTime(&time,&Second,&Minute,&Hour,&Day,&DayofWeek,&Month,&Year)  ;
 // do something with the values to set the RTC here;


ok sir that point missed out of my mind about input. i'm taking the input from a pc using arduino interface. and as there is no usb interface the baud rate for serial communication between pc and arduino is 9600

thanks sir for the code, but i'm stuck at a small point
the pcf8563 have a predefined input format which cannot be altered(atleast not by me till now). i'm writing down the code for the storing values to rtc
 Wire.send(0x00);  // 00H      Test mode
 Wire.send(0x00);  // 01H      Alarm mode
 Wire.send(0x00);  // 02H      Seconds
 Wire.send(0x00);  // 03H      Minutes
 Wire.send(0x00);  // 04H      Hours
 Wire.send(0x1b);  // 05H      Date
 Wire.send(0x01);  // 06H      Day of the week
 Wire.send(0x04);  // 07H      Century bit 19XX = 1 | 20XX = 0
 Wire.send(0x09);  // 08H      year
 Wire.send(0x80);  // 09H      Minute alarm
 Wire.send(0x80);  // 0AH      Hour alarm
 Wire.send(0x80);  // 0BH      Day alarm
 Wire.send(0x80);  // 0CH      Weekday alarm
 Wire.send(0x00);  // 0DH      CLKOUT control (00: 32.768Khz 11: 1Hz)
 Wire.send(0x00);  // 0EH      Timer control
 Wire.send(0x80);  // 0FH      Timer

the input values of sec, min hour, date is in BCD format.
so sir can you please give me a code for this
mean while i'll be trying to modify your code.
thanks sir.


You can find some code to convert to and from bcd on this page: http://www.glacialwanderer.com/hobbyrobotics/?p=12

Go Up