I need to synchronize the clocks of three Arduino clones down to microseconds.

Thread Starter

FractalFibs

Joined Jan 11, 2022
17
Hello AAC!

I am formulating a plan for a project that requires perfect clock synchronization between three microcontrollers. Now, when I say "perfect", I mean as close to perfect with the lowest error as is technically possible given the devices being used. Please know from the start that I am brand new to microcontrollers. I have some familiarity with electronics and programing but at a novice to intermediate level.

The goal of the project is to experiment with detecting the location of a gunshot, either through triangulation, trilateration, or some geometrical combination thereof and to accomplish this by using the light and sound of the shots fired.

Every project needs a name suitable to its intention, so I've dubbed a tidy little acronym for it. DOSAL or Direction Of Shockwave And Location. Think of it as a line-of-sight sniper detector. It may also, hypothetically, be used to detect the distance to a lightning strike, an incoming meteor, an explosion or any event that has a significant light and sonic signature.

For this project, I'll be using the Redboard Artemis microcontrollers from Sparkfun. Each microcontroller will have a connected sensor. One MC (the Master) will have a light sensor (transistor type) and two MC's (the Slaves) are equipped with digital microphones to detect the instant of a sound. A temperature sensor(s) may also be included for calculating the speed of sound. For the experiments, the MC's will be hardwired as a compact device located at a particular location. However, ideally, they would be individually powered and modularized then distributed at arbitrary locations, tracked with GPS and networked wirelessly for data communications. But for now, let's keep it simple.

Here is the algorithm. Once a shot is fired, the MC with the light sensor will log the time of initial detection and send that data to the slaves, starting a detection sequence. When the sonic wave of the shot reaches the microphone sensors of the slaved MC's; those detection times (and temperatures) are logged respectively. The slave MC's will combine the master's detection time and their own to solve for the distance to the origin of the shot with a TDOA formula. The master MC will constantly ping the slaves during the detection sequence for a solution acknowledgement and once confirmed, will request the calculated distance solutions. Each slave in turn will send their solution to the master MC ending the detection sequence. The master itself then calculates the approximate coordinates of the would-be sniper through triangulation or trilateration etc., then sends the data and displays it for user action.

From my perspective, it seems that clock synchronization is the most important aspect of the concept. Without it the slaves cannot accurately calculate the TDOA formulas, and therefore, the master cannot accurately calculate the location of the shooter.

Can synchronization be achieved and maintained during each detection sequence and without affecting timely communications, while using the master slave configuration? I have read this thread which at least partially addresses my concerns: (1) Problem: Syncing two Arduinos Master/Slave | All About Circuits. It uses a non-standard I2C configuration to create a synchronization event. Is this a concept that would work in my project? Or could I combine I2C and SPI for a synchronization solution? Or some other synchronization concept while still using the master slave relationship?

I would greatly appreciate any guidance or outside the box suggestions and or recommendations.

Thanks.
 
Last edited:

MrChips

Joined Oct 2, 2009
25,928
I have done this with synchronization down to nanoseconds or one CPU clock cycle.

The solution is to run all slave processors off the same crystal clock.
On power up, all slave processors are placed in HALT or SLEEP mode.
A master MCU powers up and after a short time delay, sends an interrupt (wake up) signal to all slave MCUs.
All slave MCUs will be in sync within one CPU clock cycle.
 

Thread Starter

FractalFibs

Joined Jan 11, 2022
17
I have done this with synchronization down to nanoseconds or one CPU clock cycle.

The solution is to run all slave processors off the same crystal clock.
On power up, all slave processors are placed in HALT or SLEEP mode.
A master MCU powers up and after a short time delay, sends an interrupt (wake up) signal to all slave MCUs.
All slave MCUs will be in sync within one CPU clock cycle.
Wow, I didn't expect such quick responses. Thank you both. @MrChips Thats amazing! Where can I learn more about this process you have used? What protocol did you implement it with? Would you have any generic code examples available on how you initiated the synchronization method and functions?
 

ericgibbs

Joined Jan 29, 2010
15,347
hi FF,
I have designed and manufactured a number of products for Land and Marine surveying, and I would say what you are trying to do cannot be realized to that timed Sync accuracy.

E
 

MrChips

Joined Oct 2, 2009
25,928
Wow, I didn't expect such quick responses. Thank you both. @MrChips Thats amazing! Where can I learn more about this process you have used? What protocol did you implement it with? Would you have any generic code examples available on how you initiated the synchronization method and functions?
You need to read up on the actual MCU datasheet (Atmel ATmega328) particularly on interrupts and low power modes.

I had 9 MCUs recording similar signals and all events had to be time-stamped within sub-microseconds.
Each MCU recorded the signal waveform and reported back to a master MCU. ADC sampling rate on each unit was 40Msps.
The master MCU then compiled the composite information from all 9 reporting MCUs.
This is very much the same as what you are trying to do.
 

Thread Starter

FractalFibs

Joined Jan 11, 2022
17
hi FF,
I have designed and manufactured a number of products for Land and Marine surveying, and I would say what you are trying to do cannot be realized to that timed Sync accuracy.

E
What would be, in your estimation, a reasonable expectation for time sync accuracy given the devices involved?
 

drjohsmith

Joined Dec 13, 2021
382
What time resolution have you decided on,

A quick calculation
speed of sound is around 340 m/s

Assuming your sensors are say 1m apart,
thats 3 ms, when source is at 90 degrees to your position,

If your measuring "time"
then I'm assuming you are using a comparator to say when the "shot sound" reaches you

Will the sound at both sensors be the same, or is one distorted ?
if distorted from each other then how to you decide the same place to sample.

Wondering,
would a correlation be a way forward ?
 

Papabravo

Joined Feb 24, 2006
18,097
Mr. Chips said:
The solution is to run all slave processors off the same crystal clock.

this would seem to require that all processors be on the same board or on boards in close proximity to one another. running a high speed crystal clock all over the place is fraught with problems you cannot even conceive of.
 

Thread Starter

FractalFibs

Joined Jan 11, 2022
17
You need to read up on the actual MCU datasheet (Atmel ATmega328) particularly on interrupts and low power modes.

I had 9 MCUs recording similar signals and all events had to be time-stamped within sub-microseconds.
Each MCU recorded the signal waveform and reported back to a master MCU. ADC sampling rate on each unit was 40Msps.
The master MCU then compiled the composite information from all 9 reporting MCUs.
This is very much the same as what you are trying to do.
Yes, very similar indeed! Do you suppose that the Redboard MCU will afford extra accuracy given its superior speed and hardware?
 

ericgibbs

Joined Jan 29, 2010
15,347
hi FF,
I will answer your question this way.
Let's assume that you could initially Clock sync the Master and all the Slave MCU's, this would be a challenge in its self to achieve.
The MCU program in the Slaves I assume would be the same in order to stay in sync, as soon as the first Slave detects the Event, its program execution would change in order to service the event code, and it would no longer be program sync.

I think you are confusing the clock sync with program sync.?

E
 

MrChips

Joined Oct 2, 2009
25,928
Mr. Chips said:
The solution is to run all slave processors off the same crystal clock.

this would seem to require that all processors be on the same board or on boards in close proximity to one another. running a high speed crystal clock all over the place is fraught with problems you cannot even conceive of.
My crystal clock frequency is 8MHz. This is transmitted to all MCUs.
The MCU internal clock frequency is 168MHz.
 

Ya’akov

Joined Jan 27, 2019
5,508
You don't need to know what time the optical detection occurred, you need to have a very low latency way to tell the slaves to start counting ticks when that happens, then report the number of ticks to the master.

I am not sure why you have decided to offload the processing to the slaves and to constantly poll them. I would think simply reporting the raw data to the master and cooking it there would be preferable. You don't have to poll, the slaves can push when they have the data.
 

Thread Starter

FractalFibs

Joined Jan 11, 2022
17
What time resolution have you decided on,

A quick calculation
speed of sound is around 340 m/s

Assuming your sensors are say 1m apart,
thats 3 ms, when source is at 90 degrees to your position,

If your measuring "time"
then I'm assuming you are using a comparator to say when the "shot sound" reaches you

Will the sound at both sensors be the same, or is one distorted ?
if distorted from each other then how to you decide the same place to sample.

Wondering,
would a correlation be a way forward ?
The sensors and the MCU's are all within 1/2 square meter of each other. The comparator, if I am understanding you correctly, is the time stamp of the initial detection of light from the master MCU. That minus the sound detection timestamp gives us the time of flight of the sound. The sound would be nearly adjacent to the sensors and would be line of sight. So the sound should be the same for both sensors. I am vaguely familiar with cross correlation. As far as time resolution, well I suppose the highest resolution I can get with the material at hand.
 

Ya’akov

Joined Jan 27, 2019
5,508
In fact, you could even use an optical signal to trigger the slaves. When the master detects, it could flash the slaves with a distinctive optical signal. It would make all three boards very similar if not the same.

There are other engineering issues I perceive aside from the synchronization problem, though.
 

Thread Starter

FractalFibs

Joined Jan 11, 2022
17
You don't need to know what time the optical detection occurred, you need to have a very low latency way to tell the slaves to start counting ticks when that happens, then report the number of ticks to the master.

I am not sure why you have decided to offload the processing to the slaves and to constantly poll them. I would think simply reporting the raw data to the master and cooking it there would be preferable. You don't have to poll, the slaves can push when they have the data.
Your thought was my initial reasoning, but I came under the impression that using I2C, the slaves could only send data upon request by the master. So I decided, based upon that, to have the slaves do some calculations while they were waiting for the master's request. I was trying to be efficient. What Protocol would be the lowest latency for this?
 

MrChips

Joined Oct 2, 2009
25,928
That is approximately 8", and that doesn't seem to cover an area required for sound detection and processing. I could be wrong but I think the TS will need something more useable.
I was referring to CPU clock synchronisation.
If the TS needs to sync a time-base to x-microseconds over a wider area then we can look at other options.

I would look at sending a common interrupt signal to all MCU in order to reset an internal timer clock. You only need to do this once on system reset.
 
Top