When micros()
just isn't enough.
The microsecond time resolution on 16MHz ATmega-based Arduino boards (Uno, Mega, Nano, etc.) is 4 microseconds, but if you want finer precision, you can use this little timer instead.
Notes:
- Precision: 1μs
- Based on Timer/Counter2
- Respects and restores the current interrupts enabled status
Installation:
- Drop the
microsecond_time.h
andmicrosecond_time.cpp
files into your Arduino project'ssrc
directory:
Project/
Sketch.ino
src/
microsecond-time/
microsecond_time.h
microsecond_time.cpp
Usage:
// Include the header
#include "src/microsecond-time/microsecond_time.h"
void setup() {
// Enable the microsecond timer:
enableMicrosecondTime();
}
void loop() {
// Retrieve a 1μs precise time snapshot:
unsigned long currentMicrosecondTime = microsecondTime();
}
// If you want to disable the microsecond timer at any point:
disableMicrosecondTime();