PC Time to Arduino - How to sync

Dear all,

I'm trying to sync the time of my arduino with the time from pc but it didn't work out. I've tried the library Time and its examples but all of them couldn't take the time from pc automatically. As an example, in the TimeSerial file, the program runs well but it waits some input from serial line to start counting. I need some way to get the time from pc automatically, without any external aid. The function now() don't get it for me.

Please help me here!!

In advance, I thank you all!!

This may not be as simple to solve as you expect.

How accurately do you want to sync the two systems?

You will need a program on the PC that can send the time to the Arduino. It is just not possible for the Arduino to get anything from the PC unless there is a PC program to communicate with the Arduino.

Even with a program on the PC there will be a short interval between that program getting the data from the PC's clock and the data arriving in the Arduino.

A simple option may be to add a Real Time Clock (RTC) module to the Arduino so that it has its own time source.

...R

Tkanks Robin2.

I thought something like that. But even using a RTC, we have to set the time properly. I think there's no way to get the pc time automatically unless a program send it to the Arduino as you said but I've seen something like that in the net.

Do you know any macro or program capable to do it?

Regards,

You can get NTP time with an ethernet shield. The ethernet library has an NTP example sketch. Then you can set the PC to NTP time with the lame software that Microsoft provides. With the thought that this might be an "XY" problem, why do you need to set the time to the PC, as opposed to just setting the time? GPS is an excellent time provider, and there are others.

fisihelder:
Do you know any macro or program capable to do it?

It would not be difficult to write a Python program to send the time to an Arduino. This Python - Arduino demo could be a starting point. I think it will work on Windows if you give it the correct serial port name.

You have not said how closely (minutes, seconds, milliseconds) you want the PC and the Arduino to be in sync.

...R

@Robin, I had similar thoughts, except I wanted to send time from the Arduino to the PC. :slight_smile: But I now have other fish to fry.

The basic library that comes with most RTC chips already sets the RTC time from the time that the program is compiled. It will be a few seconds (maybe 30 seconds) behind the PC but for most clock purposes, that should be OK.

There's a macro in the compiler that shows the current date and time. Then it's uploaded to the Arduino a few seconds later and it sets the time, if the chip has not already been set.

After running the example program once (and with a battery in the RTC) then you don't need to run it again until daylight savings changes. Load your own program and enjoy!

If you need accuracy to the second, then the solutions proposed above will work. GPS is also a neat way to synchronise things. It's how the modern phone system works - the phone company doesn't send a time sync signal to the exchanges, they each get their own time from an expensive GPS. This is more accurate than the rotation of the Earth itself.

Thanks all for the attention and the tips.

I would try to do a Python program. In case of failure, I'll try using a RTC module.

I thought it would be easy to set the time of the chip but I was wrong! :frowning:

I'm trying to test a primitive model of data logger using one computer, arduino and the sensors. The accuracy is not a issue (30 seconds delay is fine) but I need to update the time of the data collection with the computer all the time I run it.

Again, thanks all folks for the tips and attention!!

I found this to work OK:

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

So if you compile and push it to your board, then run it immediately, your clock will become fairly accurate with the current time. Maybe 10 to 20 seconds off. Good enough.

Then unplug your clock, comment our that line of code, and then start it again. Once the latest code is updated on the arduino, you can then plug your card back in.

As Robin2 says, this is far from simple. Further, you might question if you really need to do this. You may have good reason, but humour me, because I can't imagine a situation where you would. By needing sync, you imply Arduino is connected to PC but, typically, the only time to need an RTC is when it isn't. So what are you really trying to do?