DEV Community

Hedy
Hedy

Posted on

How a quartz crystal works in a microcontroller?

This is a fundamental concept in microcontroller systems. Let's break down how a quartz crystal works in a microcontroller, from the basic physics to the practical circuit.

The Core Concept: The Piezoelectric Effect
At the heart of a quartz crystal is a physical phenomenon called the Piezoelectric Effect.

  • Mechanical → Electrical: When you apply a physical stress (squeeze or bend) to a quartz crystal, it generates a small electrical voltage across its surfaces.
  • Electrical → Mechanical: Conversely, when you apply an electrical voltage to a quartz crystal, it physically deforms (vibrates) slightly.

This two-way street is the magic key.

How It Creates a Clock Signal: Resonance

  1. Natural Frequency: Every piece of quartz crystal has a natural resonant frequency—a specific frequency at which it "likes" to vibrate most efficiently. This frequency is determined primarily by its physical size, shape, and how it's cut. A thinner, smaller crystal vibrates at a higher frequency. This is why crystals for common microcontrollers (like 16 MHz, 20 MHz) are small, while crystals for real-time clocks (32.768 kHz) are larger and often cylindrical.

  2. The Oscillator Circuit: Inside the microcontroller, there is a specialized circuit called a Crystal Oscillator (often denoted as OSC1 and OSC2 pins). This circuit is designed to:

  • Apply a constantly alternating voltage to the crystal.
  • Listen to the tiny voltage generated by the crystal's vibration.
  • Amplify that signal and feed it back to the crystal.
  1. The Self-Sustaining Loop (The "How It Works"):
  • When you power on the circuit, electrical noise provides a tiny, random voltage kick to the crystal.
  • The crystal vibrates in response, generating a tiny voltage at its natural resonant frequency.
  • The oscillator circuit in the microcontroller picks up this tiny signal, amplifies it, and feeds it back to the crystal.
  • This reinforced signal causes the crystal to vibrate more strongly.
  • Within a few milliseconds, this feedback loop builds up into a strong, continuous, and perfectly stable oscillation at the crystal's precise natural frequency.

The result is a clean, square-wave clock signal that drives the entire microcontroller.

The Complete Picture: The Practical Circuit
On your schematic and PCB, you don't just connect the crystal directly to the MCU. A typical crystal circuit looks like this:

text

         ---||---       ---||---
OSC1 |---|     |---X---|     |---| OSC2
     |    C1        |        C2   |
     |              |             |
     |-------------/ \------------|
                   Rf (Internal)
Enter fullscreen mode Exit fullscreen mode

Illustration of a typical crystal oscillator circuit

Components and Their Roles:

  1. Quartz Crystal (X): The resonant element that sets the frequency.

  2. Load Capacitors (C1 and C2): These are absolutely critical.

  • Purpose: They, along with the crystal's own internal capacitance, form a capacitive load that helps the crystal oscillate at its specified frequency.
  • Value: The values (typically 10-22 pF for most MCUs) are chosen to match the crystal's specified load capacitance. Using the wrong values can make the oscillator unstable or cause the frequency to be slightly off.
  1. Internal Oscillator Circuit (inside MCU): Contains the amplifier and a feedback resistor (Rf), which biases the amplifier into its linear region so it can start and maintain oscillation.

Why is the Clock Signal So Important?
The clock signal is the "heartbeat" of the microcontroller. It synchronizes every operation:

  • Core Execution: The CPU executes instructions in time with the clock ticks (each tick is one clock cycle).
  • Peripheral Timing: Timers, PWM (Pulse Width Modulation), and communication interfaces like UART, SPI, and I2C all rely on the clock to generate precise baud rates and time intervals.
  • Bus Operations: Reading from Flash memory, writing to RAM, and accessing peripherals are all coordinated by the clock.

A simple analogy: Imagine an orchestra without a conductor. It would be chaotic. The crystal-generated clock signal is the conductor, ensuring every part of the microcontroller works in perfect synchrony.

The Special 32.768 kHz "Watch Crystal"
You often see a second, smaller crystal on boards (shaped like a tiny can). This is almost always a 32.768 kHz crystal.

  • Why that number? 32,768 = 2¹⁵.
  • A simple 15-stage binary divider circuit can easily divide this frequency down to exactly 1 Hz (one pulse per second), which is perfect for driving a Real-Time Clock (RTC) to keep track of time, even when the main microcontroller is in sleep mode.

Summary: Key Takeaways

  • It's a resonator: The quartz crystal doesn't create a frequency out of nothing; it filters and stabilizes the oscillation to its precise natural frequency.
  • Piezoelectric Effect is key: The mechanical-electrical feedback loop is what makes it work.
  • It's a system: The crystal alone won't work. It needs the microcontroller's internal oscillator circuit and the external load capacitors to form a complete, functioning oscillator.
  • Precision and Stability: Compared to internal RC oscillators, crystals are vastly more accurate and stable over temperature and voltage changes, which is essential for timing-critical applications and communication protocols.

Top comments (0)