Crystals and oscillators: the source of the MCU clock
The internal RC oscillator has an error of 1 to 2%, which is not enough for UART, USB or CAN. Calculating crystal load capacitors and placing the parts.
Why an external crystal
The MCU's internal RC oscillator (HSI) varies by more than 1% with temperature and voltage. UART needs an error within 2 to 3%, CAN within 0.5%, and USB within 0.25%. So boards where communication matters use an external crystal (HSE).
Crystal and oscillator
- Crystal (passive): works together with the MCU's internal oscillator circuit. Needs two load capacitors
- Oscillator (active): outputs a clock by itself once powered. More expensive, but easier to design in
Load capacitor calculation
Match the load capacitance CL in the crystal's datasheet (for example 12 pF).
C1 = C2 = 2 × (CL - Cstray)
Taking the stray capacitance Cstray of traces and pins as 3 to 5 pF, 2 × (12 - 4) gives about 16 pF. With the wrong value the frequency is off by tens of ppm, or oscillation starts late.
Placement
- Crystal right next to the MCU pins, traces as short as possible
- Surround it with ground on the layer below and around it, and do not route other signals nearby
- Keep the capacitors' ground connections short too
Failure to oscillate at low temperature happens from time to time, so check prototypes with freeze spray or a chamber.