how to get exact timestamp of a sensor reading and compare it with other sensor values in arduino?

Ex: Get the time of low reading of LDR and compare it

  • 4
    You see that word in the tag that you chose? That's the name of the function you want.
    – Majenko
    Commented Mar 16, 2018 at 10:04
  • 1
    You should clarify 1) what do you mean by exact (you need to specify the resolution needed), 2) what kind of sensors you are using (particularly how long does it take them to make a measurement) and 3) if you have a target, the accuracy you need (accuracy != resolution)
    – frarugi87
    Commented Mar 16, 2018 at 11:26
  • Are you after a human readable timestamp (eg. 17:53:43) for logging etc. or are you after a machine timestamp (eg. 42750 ms after powerup)? If the machine timestamp is fine then use millis() as others have suggested.
    – sa_leinad
    Commented Mar 16, 2018 at 12:57
  • @nuwankarunarathna Really more details are needed to answer this question.
    – sa_leinad
    Commented Mar 16, 2018 at 12:58
  • i want to get two readings from two ldr sensors and need to have the exact time that ldrs are gone low.(low light condition). this has to be done over multiple times and i should have the capability to compare the values Commented Mar 16, 2018 at 13:07

1 Answer 1


As Majenko said you can use the millis() keyword.

Unless millis is not accurate enough. In that case you can use micros(), but this overflows every 70s if I'm correct, but you can offset it against millis().

If you need to compare two values, what you can do is:

  • get millis()
  • get micros()
  • read sensor data (optimize if needed)
  • get micros()

Average the difference of the micros, and add it to millis, and you have the average time for the sensor data readings.

Of course if it doesn't matter that all sensor data is read at approximately the same time, you can use millis() for each sensor data reading and work with those values.

  • Why should you take the aveage timestamp of the measure and not the beginning or the end? I think that the beginning is more consistent and more appropriate (usually you acquire and then elaborate, so the actual measurement is closer to the beginning rather than the end)
    – frarugi87
    Commented Mar 16, 2018 at 11:23
  • @frarugi87 I also would use the start, but if you need the one time stamp for multiple measurements, the 'average' might be more applicable. Commented Mar 16, 2018 at 11:30
  • since arduino code is in a loop how can i exactly know the time Commented Mar 16, 2018 at 11:47
  • You first get the time with millis(). Than immediately afterwards you do the measurement. Assumign the measurement takes very little time, you can use the millis() value as 'timestamp'. Commented Mar 16, 2018 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.