How to Hit Sub‑Microsecond Timing on a Cheap RTC for IoT

You’re building a sensor node that needs to know exactly when a pulse happened – not just to the nearest millisecond, but to a few hundred nanoseconds. In a world where cloud services brag about “real‑time” analytics, your tiny device can feel left out if its clock drifts or jitters. The good news? You don’t need a $200 crystal oscillator board to get there. With a little care, a low‑cost real‑time clock (RTC) can give you sub‑microsecond accuracy even in a battery‑run IoT gadget.

Why the Clock Matters More Than You Think

Most hobby projects settle for “good enough” timing – a few seconds off is fine for a weather logger. But when you start mixing data from many nodes, or when you need to timestamp events for a control loop, those seconds add up. A drift of 1 ppm (part per million) sounds tiny, but over an hour that’s 3.6 ms – enough to throw off a motor control sequence or make a GPS trace look jittery. Sub‑microsecond accuracy means the difference between a smooth data set and a noisy mess.

Pick the Right Low‑Cost RTC

Look for a “temperature‑compensated” chip

A plain 32 kHz crystal can wander a few ppm per degree Celsius. A TCXO (temperature‑compensated crystal oscillator) inside the RTC keeps the drift down to 0.5 ppm or less across a typical indoor range. Chips like the DS3231 or the newer MCP7940M are cheap (under $2) and already include a TCXO.

Check the “frequency stability” spec

Manufacturers list a stability figure in ppm. For sub‑microsecond work you want ≤ 1 ppm. Anything higher will need extra software tricks to correct.

Make sure it has a “digital output” option

Some RTCs give you a square‑wave output that you can feed directly to a timer peripheral. That edge is what you’ll use as a reference for sub‑microsecond counting.

Wiring It Up Without Adding Jitter

Keep the trace short and clean

Long wires act like antennas and pick up noise. Keep the connection between the RTC and your MCU under a few centimeters, and route it away from high‑speed signals.

Use proper pull‑up resistors

The I²C bus (if you use it for configuration) needs 4.7 kΩ pull‑ups on SDA and SCL. Too weak and the edges become slow; too strong and you waste power. For a low‑power node, 10 kΩ works fine.

Add a small decoupling capacitor

A 0.1 µF capacitor right next to the RTC’s VCC pin filters out supply spikes that could shift the crystal frequency.

Turning the RTC Edge into Sub‑Microsecond Ticks

Use the MCU’s hardware timer in capture mode

Most microcontrollers have a timer that can latch its count on a rising edge of an external pin. Connect the RTC’s 1 Hz or 32 kHz square‑wave output to that pin. When the edge arrives, the timer value gives you the exact moment within the timer’s resolution.

Choose a fast timer clock

If your MCU runs at 48 MHz, set the timer prescaler to 1 so each tick is about 20.8 ns. That means a single timer count gives you sub‑microsecond granularity. Even a 16 MHz clock still yields 62.5 ns per tick – still well under a microsecond.

Combine the RTC date with the timer count

The RTC keeps track of seconds, minutes, hours, and date. When the timer captures an edge, read the RTC registers (they’re usually buffered, so you get a consistent snapshot). Then compute the full timestamp:

timestamp = rtc_seconds * 1_000_000 + (timer_count * timer_tick_ns) / 1_000

All the math can be done with 32‑bit integers on most MCUs, keeping the code fast.

Software Tricks to Clean Up the Last Bits

Apply a moving‑average filter to the timer offset

Even with a perfect crystal, the MCU’s timer can slip a few nanoseconds due to temperature or voltage changes. Record the offset between the timer count and the expected 1 s interval over several cycles, then smooth it with a simple average. Use the result to adjust the final timestamp.

Periodic calibration against a network time source

If your device has occasional Wi‑Fi or LoRa connectivity, pull the current UTC time from an NTP server once a day. Compare it to the RTC’s count and compute a drift correction factor. Apply that factor in software to keep the long‑term error below a few hundred nanoseconds per day.

Power Budget – Staying Low‑Cost and Low‑Power

A cheap RTC draws about 200 nA in battery‑backed mode. The MCU’s timer, when idle, can be clock‑gated to near zero. The biggest consumer is the radio, but that only runs when you send data. By keeping the RTC running continuously and sleeping the rest of the system, you can run a coin cell for months while still getting sub‑microsecond timestamps.

A Quick Test You Can Do at Home

  1. Connect the RTC’s square‑wave output to a logic analyzer.
  2. Set the analyzer’s time base to 10 ns/div.
  3. Capture a few seconds of data and look at the edge jitter – it should be under 200 ps for a good TCXO.
  4. Compare the captured timestamps with a laptop’s clock (via USB) to verify the drift.

If the jitter looks larger, double‑check your wiring and decoupling. Most of the time the problem is a noisy power rail or a long trace.

Bottom Line

You don’t need an expensive lab‑grade oscillator to get sub‑microsecond timing in an IoT node. Pick a temperature‑compensated RTC, feed its clean edge into a fast hardware timer, and polish the result with a bit of software smoothing and occasional network calibration. The result is a tiny, cheap, and power‑friendly clock that can keep your sensor data in perfect sync with the rest of the world.

#rtc #embedded #iot

#real time clock insights #rtcinsights #logzly

Reactions
Do you have any feedback or ideas on how we can improve this page?