syncing RTC to a PC via USB Serial, accuracy ?

Status
Not open for further replies.

vladn

Well-known member
I have trouble getting a reasonably accurate time sync of the Teensy RTC to a PC host. Teensy is always behind the PC by the 0.5-5 minute range. I am using TimeTeensy3 example with no library mods and no board connections (except USB) at several compile clock settings. Am I missing something ? Is there a way to sync RTC to a PC time via USB with a ~1s accuracy ?
 
What is even more confusing is that I can not get anything back from the USB Serial, no matter how fast (or slow) I am sending the Serial.write(TIME_REQUEST).
Tried 1.8.5/1.41 and 1.42b3 with both serial monitors using 3.2, 3.5 and 3.6 Teensy boards.
I think I miss something important here, looked at the libraries and got even more confused :(. How the time information is transferred from PC host to a Teensy RTC over USB ???
 
Ok, figured it out after a lot of pain ... One needs a separate tool called Processing, it has an IDE similar to Arduino (sketches). You need to run SyncArduinoClock.pde Processing sketch provided in the .../Time/ExamplesSyncArduinClock/ directory, while at the same time running TimeTeensy3 on the Teensy via Arduino. Close the serial monitor in Arduino, then run the Processing sketch. Change portIndex in the Processing sketch to the Teensy COMx, but that index is only counts existing ports (in my case there is COM1 and COM6(Teensy), so the index is 1 for Teensy). The Processing sketch emulates the serial connection for the Teensy and pushes the correct time to RTC.
 
Yes, the time set by the code is compile time, not download time. I think it is also possible to manually type the time into the terminal window but the processing solution sorts all the formatting side out.

If chasing better time sync normal solution is a GPS, possibly with a 1Pulse per Second output to keep everything really locked in.
 
Once I installed "Processing" and ran SyncArduinoClock.pde I've got the sync error down to about -1sec, which is fully acceptable for my project, so the problem is solved.

Nevertheless I killed few hours in deep frustration trying to figure out what is going on, being fully convinced that the Arduino serial monitor should respond to time requests - wrong ! For those on the same path - download Processing (it is free) and don't forget to edit SyncArduinoClock.pde to set the right COM port index (not number) and matching serial speed.
 
Any suggestion of how the comments or documentation could have helped you? While I want to keep the info brief, I'd like to edit it to save others from this frustration.

Currently TimeSerial.ino has this:

Code:
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 T1357041600  
 *
 * A Processing example sketch to automatically send the messages is included in the download
 * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 */

and SyncArduinoClock.pde has this:

Code:
/**
 * SyncArduinoClock. 
 *
 * portIndex must be set to the port connected to the Arduino
 * 
 * The current time is sent in response to request message from Arduino 
 * or by clicking the display window 
 *
 * The time message is 11 ASCII text characters; a header (the letter 'T')
 * followed by the ten digit system time (unix time)
 */
 
Paul, here is my take on it. Please note that TimeTeensy3 does not have any comments at all, and that was what I was trying to use most of the time.

/*
To sync Teensy RTC to the PC host time with ~1s accuracy it is necessary to install a 3rd party "Processing" IDE (from processing.org) and run Processing sketch provided with the Arduino Time library .../Time/Examples/SyncArduinClock/SyncArduinoClock.pde . Arduino USB Serial monitor does not respond to Serial.write(TIME_REQUEST) and must be closed. Instead the Processing sketch running on the host emulates the USB serial monitor and sends PC time updates to the Teensy. Both Arduino and Processing IDEs can be open at the same time as long as only one accesses the Teensy serial port.

Two changes are required to the SyncArduinoClock.pde :
- set the portIndex to the Teensy COMx, but the index counts only existing ports, in my case there is COM1 and COM6(Teensy), so the index is 1 for Teensy;
- set the Serial bit rate to the one used in the Arduino sketch.

TimeTeensy3 has an additional approximate method for host->RTC sync. (Paul, please explain in 1-2 sentences).
*/
 
Status
Not open for further replies.
Back
Top