1933

This project helps you to build cheap yet precisely synchronised millisecond clocks using just an ESP32 and a GPS receiver.

Millisecond GPS clock

I'm working on a pipeline project where by precision measurement of pressure shock waves by two pressure transducers at both ends and then measuring the precise time differences we will try to find out the location of the leakages and the time of leakage etc. The length of the pipeline is about 12 kilometers.

The first hurdle in the line was to build two precision clocks playing the same time. We deployed two GPS sensors for uniform time sensing but to build a millisecond precision clock out of that signal we developed an ESP32 and a trick!

In fact it’s not one trick, It’s two tricks one after another to couple the timer clock & the GPS clock coupled together to give the same time.

Now since the GPS clock is same therefore, in two setups, all four clocks (2 Timer clocks and 2 GPS clocks) are synchronized with each other.

Using two sets of Arduino it works but using two ESP8266, it improves in performances like fast synchronization and less gap etc., but using a pair of ESP32 and the result is fabulous! The clock synchronizes fast and the gap reduces to zero pretty quickly.

Tid-bits of the project

Hardware Serial: The softwareserial that is used in Arduino or in ESP8266 is pretty slow compared to the inbuilt hardware serial of ESP32 and that’s why I get better result in ESP32. ESP32 has 3 hardware serials and any pins can be set for the task and for that no extra header files required.
Serial1.begin(9600,SERIAL_8N1, RXD1,TXD1); //16,15,13,34,
Serial2.begin(9600,SERIAL_8N1,RXD2,TXD2); //17,12,4,35
In fact you can set any pins for the TXD or RXD. The Baudrate is taken as 9600 because the GPS receiver that I got works on 9600 baud rate.

Once the the GPS starts receiving the NMEA signals, the time is set and the millis() function of the internal timer is deployed for millisecond counting but the GPS clock and the timer clock are not synchronized at this moment.

First Synchronization

When the millisecond gap of the GPS clock and the timer clock approaches to 0 to 1, the timer clock is set by the GPS clock time:
if (yy>2000 and year(t)<2000 and ss%5==0 and millis()%1000>=0 and millis()%1000<=1)
{ 
  setTime(hh,mm,ss,dd,mn,yy);
}

Second Synchronization:

Now the both the timer clock and the GPS clocks are synchronized but their milliseconds are not synchronized. Therefore, we attempt a second synchronization by adjusting the timer clock again.
x = ss*1000+millis();
y = second(t)*1000+millis();
int gap = x-y;

if (gap>=1000 or gap<=-1000) 
{
  setTime(hh,mm,ss,dd,mn,yy);
  digitalWrite(BUTTON_PIN,HIGH);
}
We create a new variable called ‘gap’ which measures the gap in milliseconds between the two clocks – timer clock and the GPS clock. When the gap is just 1000 or -1000 (this is the point when the second count changes value and the gap is just one second) we set time again! And both the clocks are synchronized to milliseconds value.

Terminal output is the proof

Within a gap of 100 milliseconds all four clocks are synchronized precisely to the GPS values.

1st col = 1st ESP32 GPS clock,
2nd col = 1st ESP32 internal timer clock
3rd col = 2nd ESP32 GPS clock,
4th col = 2nd ESP32 internal timer clock

LCD/OLED/TFT display fiasco

The display stalls on any of these display screens. The second digit does not move further. On old 16 char x 2 col, LCD screens also the digit hangs. However, the clocks keep on running relentlessly!

Philosophy of the project

Once there is a pressure signal generated by any leakages whatsoever, the GPS clock will record the time precisely on both the pressure transducers. The time gap (t = t1 - t2) will indicate the location of the leakage but that’s not our topic here.

Schematic

Attached below.

Right now the whole thing is under testing.

I hope your readers would love the prospect of this small experiment someday.

Here is an interview with the author of this post.