How to get PC clock by Arduino?

Hi there,

I have a simple question.

I am writing a small program on Arduino. On this program, I need to get current PC clock (Hour/Minute/Second, Year/Month/Date), and I will use each element of PC clock (ex.Hour, Minute,...) for my own purpose in my Arduino program.

I read many topics on Arduino forum, but they do not match with my intention. All of examples are PC clock is input from a Terminal, and Arduino will get the input value as the current PC clock value.

My expectation is: I do not input any PC clock from Terminal. PC clock value is sent to Arduino automatically. The PC clock can be sent to Arduino only one time (at the time I power on Arduino).

If you have any idea for my question, please reply this topic. I appreciate for any answer.

Thanks.

What do you have running on the PC that knows to send time to the Arduino? Might be easier to have a Real Time Clock (RTC) module like DS1307 or DS3231 connected to the Arduino, have it read the time during setup( ) or as needed.

Dear Mr.CrossRoads,

I just have a board which has only ATmega328, it is Arduino base. I connect the board to PC via Micro USB. There is no RTC module connected to Arduino.

I need to get the current PC clock value, and use each element of PC clock value for my own purpose (ex. display Hour, Minute on Serial Monitor).

Thanks.

Ok, so the question remains: What do you have running on the PC that knows to send time to the Arduino?

I don't know what that is. I have only sent time manually from the Serial Monitor to the Arduino.

I'd get one of these. Connect to A4, A5, Gnd, and you've got battery-backed time running with no reliance on a PC. https://www.sparkfun.com/products/12708 or https://www.adafruit.com/products/3013

http://www.dipmicro.com/store/DS1307BAT-MOD http://www.tinyosshop.com/index.php?route=product/product&filter_name=clock&filter_description=true&filter_sub_category=true&product_id=229 (you can use the 32K I2C EEPROM memory for some other project later on)

And then there is eBay

http://www.ebay.ca/itm/Arduino-DS3231-AT24C32-IIC-Module-Precision-RTC-Real-Time-Clock-Quare-Memory-/262123918224?hash=item3d07cd9390:g:FssAAOSwHoFXrTHl

.

Dear Mr.CrossRoads,

What do you have running on the PC that knows to send time to the Arduino?

Sorry, I do not understand your question well. I guess that you asked about a tool (Serial Monitor, a software,...) that sends the time to Arduino?

If I understand your question well, the answer is no tool (I do not run anything on my PC to send time to the Arduino). I just connect my PC to Arduino (of course, my Arduino has been programmed my expected code, and I am asking the code in this topic). At the time of connection, the Arduino is powered on, and it will get the PC time.

** For your information, I already tried "Gobetwino" software. Gobetwino will output a PC clock value to "Command Output" when Arduino sends "T" command to it. The sent time is at the time I invoke Gobetwino. Even though Gobetwino can output the PC time at "Command Output", but I can not retrieve the PC clock at Serial Port by Serial.read(). If you already used Gobetwino, please give me your advice about retrieving value of PC clock by Arduino ( for ex, use Serial.read() ).

Thank you for your links. Actually, I intended to connect an RTC module to the Arduino, but my purpose is: I get PC clock by Arduino, retrieve the PC clock value and configure that value to RTC module. It make sure that: RTC module is configured automatically by the PC clock. I do not need to do configuration manually.

Thanks.

I do not need to do configuration manually.

How often do you think you have to reset an RTC?

I have several DS3231 RTCs running for 6 months, both are within 7 seconds of actually time.

.

Dear Mr.LarryD,

How often do you think you have to reset an RTC?

Thank you for your question. At present, I am finding a way to configure my RTC first (configure by Arduino as I raise question in this topic), I do not consider how often I need to reset RTC, so I do not know exact answer right now.

If you have any suggestion for my question, please give me your idea.

Thank you in advance.

My projects that include an RTC have two push button switches to set time (you could use one).

If you have the battery installed in the RTC, the RTC will keep time through power fails.

The DS3231 is a very good choice for an accurate RTC.

Edit: You could easily have a separate sketch that sets the RTC. .

I did a sketch recently where I read the seconds register during setup(). If the MSB is high, then the RTC is not running, I treat it like a new part: clear the upper bit so time starts running, and give it seconds/minutes/hours (including setting 12 hour or 24 hour mode) and date info also if that is used.

Then push buttons to allow user update of the various settings as needed.

Dear Mr.CrossRoads,

CrossRoads:
I did a sketch recently where I read the seconds register during setup(). If the MSB is high, then the RTC is not running, I treat it like a new part: clear the upper bit so time starts running, and give it seconds/minutes/hours (including setting 12 hour or 24 hour mode) and date info also if that is used.

Then push buttons to allow user update of the various settings as needed.

At present, I do not figure out the way to do this, especially, “Then push buttons to allow user update of the various settings as needed.”. It is same as my intention.

Basically, I need a program for Arduino that can configure RTC module which has same value as PC clock. The PC clock time is at the time user start a software, or start a script, push a button,… Previously, I mentioned that I do not need input PC clock to Terminal manually. It means that I do not intend to copy a PC clock value, then paste that value into the Terminal. The updated value to RTC should be done when user trigger something such as start a script, a software, or push a button.

So, could you send me your sketch? I really need it for proceed my task.

Thanks.

There are examples in the RTC library. Example: Follow the format below, change values to match the actual time. //************************************** //start up the RTC communications Wire.begin(); //To set the RTC time: //uncomment the third line down, as needed make changes to: SS,MM,HH,DD,DT,MM,YY //upload this sketch, comment the line again, upload this sketch again!!! //DS3231 SS,MM,HH,DD,DT,MM,YY setDS3231time ( 0,57,16, 4,20, 4,16); //uncomment to set the RTC time

Dear Mr.LarryD,

LarryD: My projects that include an RTC have two push button switches to set time (you could use one).

If you have the battery installed in the RTC, the RTC will keep time through power fails.

The DS3231 is a very good choice for an accurate RTC.

Edit: You could easily have a separate sketch that sets the RTC. .

I am using DS3232M, and I need to configure DS3232M through Arduino. But the configuration is done at the specific time when user trigger a script, push a button. The configured value to RTC is same as PC clock. I also supply DS3232M by a battery.

I read some topics related to this matter. Regarding to those topics, I simply think that we need to copy then paste PC clock as Unix time format (EPOCH?) to Serial Monitor (or a Terminal application). I think we can input to Serial Monitor (or a Terminal) automatically when we trigger a script, or push a button. We do not need to copy/paste as I mentioned.

I get an example: http://stackoverflow.com/questions/13335402/unable-to-sync-computer-time-to-arduino-via-usb

In this example, I think when user type "echo "T$(($(date +%s)+60*60*$TZ_adjust))" >/dev/tty.usbmodemfa131", this will send Unix time to COM port automatically, and Arduino will display the current time on Serial Monitor. I am using Windows OS, I tried this but nothing has changed since I type the command on MobaXterm. So I do not know whether this way can be applied or not.

Thanks.

Dear Mr.LarryD,

LarryD: There are examples in the RTC library. Example: Follow the format below, change values to match the actual time. //************************************** //start up the RTC communications Wire.begin(); //To set the RTC time: //uncomment the third line down, as needed make changes to: SS,MM,HH,DD,DT,MM,YY //upload this sketch, comment the line again, upload this sketch again!!! //DS3231 SS,MM,HH,DD,DT,MM,YY setDS3231time ( 0,57,16, 4,20, 4,16); //uncomment to set the RTC time

Thank you so much for your help. But do we have any way to get the actual time automatically, not by hand? If we set time by hand, the RTC can go behind the PC clock some seconds. And we must read the value of PC clock, then we set that value to RTC.

Thanks.

You can get date and time when the sketch was complied. Not sure of the format, but it is available if you search for it. This will only work once as it is the values at compile time when uploaded.

Here: DATE and TIME

Edit: rtc.adjust(DateTime(F(DATE), F(TIME))); Or maybe: // following line sets the RTC to the date & time this sketch was compiled rtc.begin(DateTime(F(DATE), F(TIME)));

Dear Mr.LarryD,

LarryD:
You can get date and time when the sketch was complied.
Not sure of the format, but it is available if you search for it.
This will only work once as it is the values at compile time when uploaded.

Here:
DATE
and
TIME

Edit:
rtc.adjust(DateTime(F(DATE), F(TIME)));
Or maybe:
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(DATE), F(TIME)));

Thank you so much for your kind support. Your suggestion is good for me.

I use your suggestion, refer to some topics, and this is my sketch for configuration RTC DS3232M to PC clock only one time. The time may be at the sketch was compiled.

By the way, I also think one more solution about configuration DS3232M to PC clock. I will write down it in another post.


#include <SoftI2C.h> // initialise required libraries
#include <DS3232RTC.h>
#include <Time.h>
SoftI2C i2c(A4, A5); // assign pins to SDA and SCL
DS3232RTC rtc(i2c);

RTCTime time;
RTCDate date;
const char *monthName[12] = {
“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”,
“Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”
};

bool getTime(const char *str)
{
int Hour, Min, Sec;

if (sscanf(str, “%d:%d:%d”, &Hour, &Min, &Sec) != 3) return false;
time.hour = Hour;
time.minute = Min;
time.second = Sec;
return true;
}

bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;

if (sscanf(str, “%s %d %d”, Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
date.day = Day;
date.month = monthIndex + 1;
date.year = Year;
return true;
}

void setup() {
Serial.begin(9600);

// get the time when the compiler was run
if (getTime(TIME)) {
// and configure the RTC with this info
rtc.writeTime(&time);
}

// get the date when the compiler was run
if (getDate(DATE)) {
// and configure the RTC with this info
rtc.writeDate(&date);
}

// Read time
rtc.readTime(&time);

Serial.print("Time: ");
Serial.print(time.hour, DEC);
Serial.print(’:’);
Serial.print(time.minute, DEC);
Serial.print(’:’);
Serial.print(time.second, DEC);

// Read date
rtc.readDate(&date);

Serial.println("");
Serial.print("Date: ");
Serial.print(date.day, DEC);
Serial.print(’:’);
Serial.print(date.month, DEC);
Serial.print(’:’);
Serial.print(date.year, DEC);
}

void loop() {
// put your main code here, to run repeatedly:

}

I am thinking one more solution to configure RTC to PC clock automatically (it means that we do not need to type PC clock input manually. We run one script, or push a button, or run a software. One of those actions will send the PC clock information to Arduino, then Arduino will use the information to configure to RTC).

There is a link to do this idea. http://www.instructables.com/id/Synchronise-DS3221-RTC-with-PC-via-Arduino/?ALLSTEPS

At my solution, I will create a sketch that RTC is configured to PC clock via input from UART. We can input the clock information via Serial Monitor or any Terminal (ex. Teraterm), but I will not input by hand.

I will create a script that get the PC clock information, then send that information to UART. This information as input of Arduino, and it will be used to configure to RTC.

Thank you Mr.LarryD and Mr.CrossRoads for your support in this topic.

Hi,

see https://github.com/kachok/arduino-libraries/tree/master/Time/Examples/Processing/SyncArduinoClock.

Make yourself familar with processing. I guess you will find more details on Github. But you need your board connected to a PC via usb. But that is your business case I would guess.

I found that approach very useful.

Dear Mr.f41_ardu,

Thank you for your information. I will try your suggestion about SyncArduinoClock.

I am not familiar with processing, but I think will be useful for me in any Arduino tasks.

Best regards,