Every digital circuit, from the Arduino to the phone, runs on a clock: regular pulses that tell the processor "execute the next step". And those pulses usually come from a small part called the crystal.
How does it work?
A crystal is a piece of quartz cut to a very precise size. Quartz has a property called piezoelectricity: if you apply a voltage to it, it moves, and if it moves, it produces a voltage. With a suitable circuit, it keeps vibrating at a very stable frequency set by its size.
A crystal marked 16.000 means 16 MHz, that is 16 million pulses a second. And that's exactly the crystal on the Arduino Uno.
The circuit that drives it is inside the microcontroller itself: a small amplifier connected to two pins (on the Uno they're called XTAL1 and XTAL2). The crystal sits between those two pins, and the amplifier gives it a push each time so the vibration doesn't stop.
Why not just any oscillator?
- Accuracy: an ordinary crystal is accurate to around ±20 to ±50 parts per million (ppm). An RC oscillator can be off by 1% or more.
- Stability: it isn't much affected by temperature and voltage.
That accuracy matters in:
- Serial communication (UART): if the speed differs too much between the two ends, the data comes out as strange characters.
- USB: it needs very precise timing.
- Time and date: a clock that's off by 1% is wrong by about 15 minutes a day!
ppm in numbers
| Accuracy | Error per day | Error per month |
|---|---|---|
| 1% (internal RC) | About 14 minutes | Hours |
| 50 ppm | About 4 seconds | About two minutes |
| 20 ppm | Under two seconds | About a minute |
| 2 ppm (TCXO) | Under a quarter of a second | About 5 seconds |
That's why, if you're building a clock, millis() on its own isn't enough. Use an RTC chip such as the DS3231, which contains a temperature-compensated crystal (TCXO), or fetch the time from the internet.
Load capacitors
An ordinary (two-pin) crystal needs two small capacitors (usually 10–22 pF) from each pin to ground. Their value is calculated from the load capacitance stated in the crystal's datasheet. The wrong value makes the frequency drift a little, or stops the crystal from starting at all.
The calculation with an example
C1 = C2 = 2 × (CL − Cstray)
- CL: from the crystal's datasheet, for example 18 pF.
- Cstray: the capacitance of the traces and the chip pins, roughly 3–5 pF.
- So: 2 × (18 − 4) = 28 pF → the nearest value is 27 pF.
Why times 2? Because, as far as the crystal is concerned, the two capacitors are in series, so the capacitance it sees is half the value of one.
On the PCB: place the crystal right next to the chip's pins, with its capacitors beside it, ground underneath, and away from fast signals.
The types
- Crystal: two pins, and needs the circuit inside the chip plus the two capacitors.
- Ready-made oscillator: 4 pins; give it power and it outputs pulses straight away. Easier, but more expensive.
- Ceramic resonator: cheaper and less accurate (about 0.5%), and sometimes the capacitors are inside it.
- 32.768 kHz: the clock crystal. That number = 2 to the power 15, so if you divide it by 2 fifteen times you get exactly one pulse per second. That's why it's the one in watches and in RTC chips.
| Type | Pins | Approximate accuracy | Needs capacitors? |
|---|---|---|---|
| Crystal | 2 (or 4 in SMD) | ±20–50 ppm | Yes |
| Ready-made oscillator | 4 | ±25–50 ppm | No, just a supply capacitor |
| Resonator | 3 | About 0.5% | Usually built in |
| Internal RC | – | 1–10% | No |
A note on the Uno: the original Uno has a 16 MHz crystal for the ATmega16U2 (the USB chip), while the ATmega328P itself usually runs on a 16 MHz resonator. That's why the Uno isn't the best thing to use as an accurate clock.
The internal RC
Many microcontrollers have an internal oscillator. If your project doesn't need accurate timing, you can do without the crystal and save parts and space. And if you need USB or accurate time, use a crystal.
On the ATmega328P, for example, you can run it on its internal 8 MHz with no crystal at all, which is useful in small battery projects. But you must change the fuses with a programmer, and choose the right board in the IDE, or all the timing will be wrong.
Well-known faults
| Symptom | Usual cause |
|---|---|
| The chip doesn't run at all after you fit it | The crystal isn't starting: wrong capacitors or poor soldering |
| Serial outputs strange characters | The actual frequency differs from the one in the settings |
delay(1000) takes two seconds | The board is set to 16 and the chip is running at 8 |
| The clock loses minutes a day | Internal RC instead of a crystal, or you need an RTC |
| It stops when you bring your hand close | The crystal traces are long or there's no ground under them |
Don't measure the crystal with an ordinary probe: the probe's own capacitance (about 10–15 pF) can stop the oscillation or change the frequency. If you need to check, measure on a clock output pin if the chip provides one.
If you're still getting to know the microcontroller, start with the microcontroller article.
FAQ
Does a crystal have a direction? A two-pin crystal has no direction. A four-pin SMD one usually has only two pins for the crystal and the other two are ground, so check the datasheet.
Can I fit a faster crystal to make the Arduino faster? The ATmega328P is designed for up to 20 MHz. Above that it may run and then hang. And any change in frequency requires changing the settings and the bootloader, or all the timing will be wrong.