how to acheive 2 arduino time syncronization?

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
Hi,


Please excuse my possible lack of knowledge and experience as I’m trying to give some steps on this (new) world. And accept my apologies if I’m on the wrong forum…

I think I’ve hit on a problem and need advice for what might be the best path/approach to solve it.

I’m trying to synchronize the clocks for 2 Arduinos (Mega 2560 compatible) that connect though WiFi (ESP8266) to a common AccessPoint and to a PC that receives the data from both. The sync I’m trying to achieve should be quite accurate as I need to collect time and sensor values on both during no more than 1 minute, but the time on the readings on both much match as close as possible up to millisecond. Any skew between them must be recorded.

The application on the PC will measure the time synchronization (or lack of it) of sensor readings by both Arduinos, so readings on Arduino1 can be compare with readings on Arduino2. I’m thinking on timer/clock sync but I think if I can know the skew between both Arduino clocks, I can make the proper calculations outside both Arduinos (on the PC).

I had 2 initial ideias and did some research. But have doubts about both: not sure which is best or even if any will allow me to reach the goal.

Option1) Sync both clocks using RTC
Problems Option1: RTC can only give up to second, and I need millisecond
need to sync it using one NTP server and looking at libraries/examples think this will sync up to minute … I need milliseconds.​

Option2) GPS clock sync – seams a technical viable solution –
Problems Option2) this will be mainly indoor and is a somehow expensive solution – a GPS module to sync time, seams like a cannon to kill a fly but may be the only solution…​

Any ideas, feedback, suggestions or directions will be greatly appreciated on what might be the best approach to know the time skew between 2 Arduinos or sync their clocks up to millisecond precision?

Regards and ty for any help,
Luis
 

MrChips

Joined Oct 2, 2009
30,821
I do this everyday with ten MCUs synchronized to 10ns.
The solution is straight forward. All MCUs are driven from the same MCU clock. All MCUs are started simultaneously using one interrupt following sleep mode.
 

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
I do this everyday with ten MCUs synchronized to 10ns.
The solution is straight forward. All MCUs are driven from the same MCU clock. All MCUs are started simultaneously using one interrupt following sleep mode.
Hi MrChips,
Thanks for you very quick reply!

So if understand this correctly, as long as every Arduino are from "same manufacture/build" aplying reset (?RESET PIN?) simultaneously to all they will start all at the same exact time and with this all in sync right? The sleep mode is needed for this purpose or just to enter energy saving?

cheers,
Luis
 

NorthGuy

Joined Jun 28, 2014
611
Unless your WiFi module gives you some sort of access to frame data, I don't think 1 ms is achievable over WiFi. However, you can get close - NTP can give you few ms. GPS will definitely work. You may also sync to Atomic clock signal or some home made RF beacon. This will give you much better than 1 ms. Of course, any sort of wired connection will do.

Also, the MCU clock is not very precise. If you have a crystal oscillator, you can keep 1 ms synchronization for a while, say for 10 sec or may be even longer. With standard internal RC oscillator, you probably need to re-sync every 100 ms or so. You can increase the re-sync time requirement if you use your sync points for calibration.
 

MrChips

Joined Oct 2, 2009
30,821
Hi MrChips,
Thanks for you very quick reply!

So if understand this correctly, as long as every Arduino are from "same manufacture/build" aplying reset (?RESET PIN?) simultaneously to all they will start all at the same exact time and with this all in sync right? The sleep mode is needed for this purpose or just to enter energy saving?

cheers,
Luis
No. All MCUs are not guaranteed to come out of RESET in exactly the same number of clock cycles. The purpose of SLEEP mode in this situation is not to save energy. The purpose is to go into IDLE mode so that all MCUs are in the same state. They will all respond to an external interrupt in the same number of clock cycles.
 

John P

Joined Oct 14, 2008
2,026
I am skeptical about doing this via WiFi, but maybe there are other options to calibrate the processors. If they can't be wired together, what could you do to connect them, or to connect them to some third signal source? Optical, radio? 1 millisecond is actually a lot of processor cycles.
 

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
Unless your WiFi module gives you some sort of access to frame data, I don't think 1 ms is achievable over WiFi. However, you can get close - NTP can give you few ms. GPS will definitely work. You may also sync to Atomic clock signal or some home made RF beacon. This will give you much better than 1 ms. Of course, any sort of wired connection will do.

Also, the MCU clock is not very precise. If you have a crystal oscillator, you can keep 1 ms synchronization for a while, say for 10 sec or may be even longer. With standard internal RC oscillator, you probably need to re-sync every 100 ms or so. You can increase the re-sync time requirement if you use your sync points for calibration.
Hi
I'm not sure if it's feasible also via wifi... As I use wifi to send data to PC I was considering also this possibility...
Seams GPS might be a good solution have to investigate that further tks :) But being indoor may not be a problem?

about MCU precision... I'm getting worried on that one... as I will need several measurements along the day, but last for no more than 1 minute, I'm suspecting that I'll need to resync several times.... I'm considering having a cable connecting both but need also to ensure low time deviation during those minutes of readings.. any recommendation? as I'm quite new on this arduino stuff, some "trivial" stuff are not that trivial to me :)

found this link, do you think might be usefull for my goal? Seams almost "to easy" to work... but this is for making a clock... I need to clock to make calculations but also read from sensors...
http://www.instructables.com/id/Make-an-accurate-Arduino-clock-using-only-one-wire/
I'm using elapsedMicros as timer counter, so I guess I might have to change some code to read time from other functions?

ty very much for all this precious help.
LG
 

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
I am skeptical about doing this via WiFi, but maybe there are other options to calibrate the processors. If they can't be wired together, what could you do to connect them, or to connect them to some third signal source? Optical, radio? 1 millisecond is actually a lot of processor cycles.
Hi John

I can wire them together. Was not my initial idea but let's face it, if it simplifies things why not considering it? I also think that might be interesting an initial wiring (some removable cable to connect both) for initial sync, and then they stay disconnected as long as I can keep clocks synced during the day - another challenge that I have to solve... found this trick do you think this might help?
http://www.instructables.com/id/Make-an-accurate-Arduino-clock-using-only-one-wire/
 

MrChips

Joined Oct 2, 2009
30,821
Put an IR sensor on each of the two Arduinos connected to interrupt inputs. An external IR pulse will trigger the interrupt and sync the two MCUs. You should be able to sync to less than 1μs.
 

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
Hi Danny,

how can I achieve this? I have little to no idea... :)

I'm trying to have a wire connect both for triggering reset to timer... 1st impression I get from Arduino Mega (the cheep ones I have for testing) is that even having a builtin crystal the have (on my 1st tests) something like 0,05% skew, that represents ~28ms on 60 sec ... too much for my needs... :(

LG
 

NorthGuy

Joined Jun 28, 2014
611
Do you need the consistent time base (e.g. the time elapsed between samples is accurate to 1 ms) or do you want synchronisation (e.g. both MCU sample within 1 ms of each other)?
 

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
Depends on what you meant by 'this'.
:) sorry...
the 1st I think I'm on my way to do it (wiring both wire and using interrupts)

those 2 I have no idea at all:
"Or you can run one MCU as master and another as a slave.
Or you can run both mcus as slaves to the PC master."
 

Thread Starter

Luis Guido

Joined Nov 16, 2016
12
Do you need the consistent time base (e.g. the time elapsed between samples is accurate to 1 ms) or do you want synchronisation (e.g. both MCU sample within 1 ms of each other)?
I would need an accurante sync between clocks for about 1/2 day. Recurrent measures being taken for 60 seconds long every now and then using ultrasonic sensors.
Preferebly the clocks would be in sync with each other so that the time stamp taken on both arduinos would be usable for comparison:
eg: a reading on timestamp(in ms) 12345 on arduino1 has +5ms than the same reading on timestamp 12340 on arduino2. It's no problem to reset counters/clocks for each 60sec Reading.

So aswering to your question I would say the second as synchronisation is what I am trying to read/detect. measuring 2 distinct events and register their time diference on 2 arduinos.

I was thinking about something that might work. Have to try it and see if is feasable, but let me know if it makes sense or if I'm overcomplicating things :)

1s run upon Arduino restart

1) Wire between both Arduinos(might also try other sugestions like RF, IR, … if this if cable works)
2) PC (via WiFi) send command to both Arduinos to sleep
a. Arduinos enter in sleep mode​
3) Trigger them to wake from sleep and reset the counter using wire connection
a. Both Arduinos wake from sleep at the same time​

4) Measure for a while (?60 seconds? or more to “pair” the devices)
5) Produce another interrupt to read the actual timecounter for both Arduinos
a. This interrupt is also wire triggered but without arduinos being in sleep mode ?so we’ll have little skew on reading between both Arduinos?
b. Each Arduino read the value for own timecounter and sent it to PC​
6) PC will know the skew for the Arduino clocks
a. Will allow to compensate the values received by both Arduinos
b. Will allow to determine the synchronization between them​

all the best
LG
 

MrChips

Joined Oct 2, 2009
30,821
Firstly, let's understand what you are trying to do.
You are using ultrasonic sensors. Hence you must be doing some kind of distance or time-of-flight measurement.
Are you using one Arduino to transmit an ultrasonic pulse and a second Arduino to record the received echo?
 

NorthGuy

Joined Jun 28, 2014
611
Events? Such as one arduino detects a bullet out of the barrel, the other one detects the bullet hitting the target and you want to know the time difference? If that's the goal, simply send a pulse from the first arduino to the second once the bullet left and let the second arduino measure the time difference.
 
Top