Pre-synchronizing two arduinos

Hi!
I am wondering if I can somehow pre-sync two arduinos. Here is what I am trying to accomplish: I am using two arduinos as daq for two sensors (microphones). The two sensors are a few miles apart and there is no easy solution to sync them using wi-fi. But I want the measurements from these two sensors to be sync. The data will be written either to a computer using the serial port or to an SD card. If I can get time stamps against each data point from each sensor, then I can later compare the data from these two sensors. The time stamps have to be synced with ideally millisecond accuracy (but I can live with 10 ms accuracy too). 1 second accuracy will not work for my application, otherwise using an inexpensive gps receiver might have worked. Any thoughts?

Price of parts is not a concern right now (although it very well might be later :slight_smile: Feasibility of the experiment with arduino is the important factor.

Thanks :slight_smile:

I presume they initially start near each other? You could use Timer 1 or Timer 2, and have that start counting the moment an external event happens (like, when you push a switch). If they are both connected in a way that they both get the same switch pulse then they would start counting at the same time.

Now, of course, you can't afford to have them power down after that, so they would have to run from batteries until the time of the experiment. This should be achievable either with reasonably large batteries, or using power-saving techniques (however not saving so much power that the timer stops).

You might need to experiment to see how much one drifts with respect to the other (due to a slightly different clock speed).

The two sensors are a few miles apart

I would assume they would not start in close physical proximity.

They will drift over time as well, especially if one is hot and the other cold.

I think you have to periodically send a sync command of some kind from the "computer", whatever that is. However if they are stand-alone and logging to their local SD cards or local computers you are out of luck unless the computers have access to a global time standard of some kind (are there any with 1mS resolution, and if so how would you get a sub-mS deterministic sync through an operating system).

How about an RTC with a TCXO on each Arduino, these should be good to 2-3ppm accuracy and if you have a battery backup for the RTC you can sync them in a single location then take them to the field.

Over what period of time do you need to log the data? That makes a difference as well.


Rob

These are exactly the solutions I was looking for. Thanks Nick and Grayno! I can make them start near each other. And yes, I can afford to keep both units on until the end of the experiment. Gotta learn about the time library how it works. My experiments will be done within half an hour. So, I have to check how much it drifts within that time. So, I guess, I will start with Nick's solution and improve upon it based on Grayno's suggestion if required.

There are a few things you could do to improve accuracy.

  • Make up a custom board (itself not particularly hard) with a crystal rather than a resonator for higher accuracy (and perhaps get a crystal with good specs)
  • Include a temperature sensor
  • If measurement shows that time drifts depending on temperature, then if you know the temperature (from the sensor) you could compensate to some degree (pun not intended)

You could consider something like the ChronoDot:

According to the documentation:

This makes the ChronoDot very well suited for time critical applications that cannot be regularly synchronized to an external clock.

I don't know if you can get microsecond accuracy from it, but the SQW signal might achieve that.

It might be worth experimenting with. However I honestly don't know how accurate that would be compared to maintaining your own count of clock ticks using one of the timers.

My experiments will be done within half an hour. So, I have to check how much it drifts within that time.

If they both start at the same temperature, and are synced together, and the test is conducted 30 minutes later, the drift due to temperature would be negligible I would have thought. Especially if you transport (or store) them in a thermal insulating box to minimize changes (eg. from the car trip).

As for inherent inaccuracies in the clocks, a quick test over an hour where it reports the timer counter periodically should show if one is drifting with respect to the other.

Note, IIRC, when looking at RTC units, I recall the cheaper ones just drift, but the more expensive ones have temperature sensors in them, because the surrounding temperature will affect the timing.

I would suggest starting to look at NTP, the network time protocol (http://www.ntp.org/) and delve into all of the issues. I suspect most of the people in this group probably don't have the depth of knowledge that you will need (I certainly don't) to get millisecond accuracy with two computers in different locations. This is a fairly complex problem.

I imagine a solution might be keep a clock using either internal time or a high accuracy RTC (real time clock), and sync every so often with either GPS or via NTP if there is a network connecting the two Arduinos, and figure out how many clock ticks your local chip ticked. Because such calculations are usually involve floating point which is simulated on the AVR and expensive, I would suggest instead when you log to SD, log the local ticks, and every so often when you synchronize, log that. Then when you are processing the data on a bigger machine, use those timestaps to correlate with the local time ticks to provide the actual tick.

You can use the PPS pin of GPS to get within nanoseconds absolute accuracy. Older GPS to within milliseconds. If the temperature is the same at both units, an Arduino Uno can be calibrated in software to drift 1 PPM. No extra hardware is needed. I did this with PPS pin of GPS, but then you don't need GPS anymore.

Hey sbright, I saw your postings: http://forum.arduino.cc/index.php/topic,120288
Looks interesting. I will give it a try.

sbright33:
You can use the PPS pin of GPS to get within nanoseconds absolute accuracy. Older GPS to within milliseconds. If the temperature is the same at both units, an Arduino Uno can be calibrated in software to drift 1 PPM. No extra hardware is needed. I did this with PPS pin of GPS, but then you don't need GPS anymore.

Do you an have an example of how that is done? The ceramic resonator as used on the Uno has no internal drift correction means that I recall, only the internal R/C oscillator which I would think would be very hard to maintain +/- 1 PPM even in the short term?

Lefty

I recall crystals have 20ppm even at constant temperature. Can you point lasers at the sites? Just curious.

Graynomad:
if so how would you get a sub-mS deterministic sync through an operating system).

If you could shoe-horn a full NTP daemon implementation onto an Arduino, and can afford to leave it to adapt for an extended period, then I think you could (eventually) get down to that sort of resolution. The main limiting factor would be how much jitter there was on the network and how consistent that jitter was.

See my link above for a short code example. Lefty it is done in software only. No parts needed. You can see how long experimentally it stays within 1ms always for hours in my tests. You can also measure how long it takes to drift 10us. From this data you can calculate the PPM result. Anyone try it with PPS pin?