Go Down

Topic: Getting millisecond and microsecond time (Read 249 times) previous topic - next topic

goldworkswell

Hi all,
I need to time some data from some sensors right now I am getting data time resolution down to 1 second, but i need it down to millisecond possibly microsecond resolution. I know that in the time library you can get resolution down to 1 second. I have the adafruit sd card shield with an rtc. I think you would have to use the micros and millis but i don't know how i could get it at the start of the second.

Tech:
adafruit sd card shield with an rtc
Arduino Due
various sensors

jremington

#1
Apr 21, 2019, 01:44 am Last Edit: Apr 21, 2019, 01:46 am by jremington
That is certainly not easy and perhaps not even possible with an external RTC. You don't know when the "start of the second" happens inside the RTC, and it takes some time to read the RTC data into the Arduino.

A better solution is to turn the Arduino itself into an RTC. Get one with an accurate crystal rather than the cheap ceramic resonators, then use a combination of the time library and millis() to form time stamps.

Or, use the 1 Hz or 32 kHz output from an RTC module as an interrupt input to the Arduino, and keep time using that as a time base.

wvmarle

I was also thinking of the 1 Hz output.

Trigger an interrupt, that records the current value of the micros() timer. Then to record your sensor time use the current time (from now()) plus micros() - recordedMicros. That should give you a pretty accurate timestamp.

Of course that are still not absolute times as it depends also on how well the RTC is set, it does give you the time between the time you recorded an event. Which in turn is not necessarily when it happened: all sensors have a reaction time, and it takes some time for your sketch to read each individual sensor. Even if you have the sensors fire interrupts on events you have to keep reaction time/conversion time in mind.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

6v6gt

As the previous posters have stated, you can use the 1Hz output of say a DS3231 as the basis for your timing. The *falling edge of the 1Hz signal is synchronized with the seconds rollover in the RTC.
To benefit from this, you set the RTC accurately on at the beginning of a new second. You can use NTP to get a **timestamp in fractions of a second. With such a timestamp, you can periodically schedule an update to the RTC for the next full second. At the time of update, you can also record the value of say millis()%1000 to get an offset (which you can also periodically correct when handling the 1Hz pulse) to use for time resolutions in fractions of a second.

I used some of the techniques above in a speaking clock project which I published here.

*from the DS1307 datasheet ( The edge does not appear to be specified on the DS3231 data sheet)
** ideally, compensate this for network latency.

Go Up