What makes a microcontroller tick?
June 21, 2019 • article
In the past projects and articles we have kind of glossed over this point: what makes a microcontroller tick? In this article we will fill that gap! We will focus on crystals, and crystal oscillators, and how they are used with microcontrollers to provide a stable timebase.
The short answer: in most cases, we use a crystal oscillator built of a crystal and two capacitors, and connect it to the microcontroller. It then looks something like this (pay attention to the part circled in orange):
Can you see it? The silver piece is the so-called crystal, and the two orange components are the capacitors. Crystals come in different forms and sizes, the most common of which you can see here:
But before we get to all these details, let's better start at the beginning :)
What is a clock? What is clock speed?
Any computer has a heart beat, which is usually called the clock speed or just clock for short. Clock speed is measured in Hertz, that is, in beats per second. When you buy a computer nowadays (2019), typical clock speeds are in the range of 3-4GHz (gigahertz) which corresponds to 3-4 billion beats per second. Yes, that's fast.
On older computers from the nineties you could sometimes see a small (often green or yellow) LED display that would show the current clock speed in megahertz (MHz):
And yes, that is a proud clock speed of 66MHz. Pressing the turbo button would allow you to temporarily switch to a faster clock speed (in this case: around 130MHz), which would offer higher computing power at a higher power consumption. In some sense, this can be considered as the original historical over-clocking of PCs.
Alright, but what does that have to do with microcontrollers? you might ask. For all intents and purposes, microcontrollers are basically tiny computers. And just as computers, microcontrollers need a clock.
If you are wondering how we could avoid using a crystal in our previous projects (the blink LED and the electronic dice): we didn't! We simply told the microcontroller to use its internal oscillator. This goes a bit beyond the scope of this article, but let me know if you are interested and I will cover it in a future article :)
What is a crystal?
Okay, so we have seen the silvery thing that supposedly is a “crystal.” But why is it called crystal? And what is a crystal? Without going too much into detail, crystals are a special kind of material that has a funny property when it is compressed or stretched by some external force: it creates an electric potential! See the following diagram:
On the left you see a lump of crystal. When it is compressed (middle), the positive charges and negative charges split and accumulate at the top and bottom, respectively. When the crystal is stretched, however, the opposite happens. This is called the piezoelectric effect, and it occurs in crystalline structures such as quartz. If you want to learn more about it, check out the website of the National Science Teacher's Association in the resources box.
Now it is easy to imagine that if you instead apply a voltage to the crystal that the crystal then compresses or stretches:
In the picture above you see the lump of crystal on the left side, just like before. In the middle, we apply a voltage by connecting a small battery. The positive voltage is at the bottom, and the negative voltage is at the top. The crystal wants to stay neutral, so inside of it the positive charge moves to the top and the negative charge to the bottom. As a result, the crystal compresses! This is called the reverse piezoelectric effect :)
How can we use the piezoelectric effect to create an oscillator?
Okay, so what does that have to do with the clock of a microcontrollers? We are almost there!
The clock of a microcontroller is a periodic tick and is created by an oscillator. And we can use a crystal to make such an oscillator! Here is how it works. It can be broken down into five steps:
In the above you can see the animation. The two funny looking things attached to the crystal are capacitors, and we will soon understand why we need them. Let's go through the five steps:
- The crystal is at rest. We have to kick start the oscillator by inserting a voltage into the circuit. We only need to do that once, after that the oscillator will keep oscillating on its own (step 1).
- As an immediate response to the external voltage we put in, the crystal stretches. Charges appear due to the reverse piezoelectric effect, and they get absorbed by the capacitors (step 2).
- When the crystal is stretched to the maximum position, it stops moving. The charges flow away to ground and then the crystal is uncharged again. What does it do? It wants to relax to its original position (step 3).
- The crystal reaches its original size (unstretched and uncompressed), but because it is a fluent motion, it overshoots a little bit. It's like letting go off the throttle: the car will eventually stop, but the inertia keeps it going. Same with the crystal! It overshoots and becomes now slightly compressed (step 4).
- Then, just as before, the charges that appear get absorbed by the capacitors and the crystal relaxes to its original size (step 5).
Then, everything starts over and over again. But remember that we only need to start the oscillator once, after that it keeps oscillating on its own. In praxis, of course, there are resistance losses in the circuit, so we have to periodically re-start the oscillator occasionally, but these are small details.
So why do we need the capacitors? you might ask. Well, they store the charges that are generated by the reverse piezoelectric effect. Without them the charges would disappear immediately to ground and there would be no oscillation.
It is interesting to see what happens with the energy of the total system during an oscillation. When the crystal is at its normal position, all the energy is in the kinetic energy of the crystal (because it is just coming back from being stretched or compressed, like a pendulum). When the crystal is maximally stretched or compressed, on the other hand, all the energy is in electrocstatic energy that is stored in the capacitors.
In an oscillator, the energy is usually always transferred from one kind to another kind during an oscillation :)
From the oscillator to a clock signal
Now we understand how a simple crystal oscillator works. Any oscillator has a typical frequency with which it, well, oscillates. What kind of parameters determine this frequency for a crystal oscillator?
The crystal does! The shape and size of the crystal determine, mechanically, the most convenient frequency for it to stretch and compress. The material is always the same, but the shape and size of the crystal determines its frequency.
Just like a musical tuning fork, the crystal is cut into the same sort of shape. And, naturally, it has a typical oscillating frequency, most commonly in the range of a few kilohertz up to many megahertz.
Once we know the frequency of the crystal, we can pick the appropriate capacitor. Larger capacitors discharge slower (because they can store more charges) and therefore the value of the capacitor depends on the value of the frequency of the circuit. If you want to learn more about the technical details of crystal oscillators, check out the excellent Wikipedia article on the topic :)
Now we need to connect the crystal oscillator to a microcontroller, and this is very simple! Here you go:
Each controller typically has two pins called “OSC” for oscillator. Between these two pins you put the crystal of the desired frequency (around 4MHz in many of our cases). Then, you add an additional capacitor on each crystal leg and connect it to ground. For our purposes we can use capacitors of 18-22pF (picofarad) capacity, they work quite well.
Internally, the controller reads out these oscillations, and whenever the electric signal has completed a full oscillation it is registered as a tick. And that tick makes the controller jump to the next command to execute, just like a computer :)
Why haven't we talked about that before? The internal oscillator!
The heartbeat of a microcontroller circuit seems to be an important topic. So why didn't we talk about it earlier?
As already alluded to in the introduction: most microcontrollers can be configured to use an internal oscillator (that is, if they have one). This oscillator is usually not very precise, meaning that its frequency is not very stable and can vary a bit from controller to controller.
For simple tasks (the blink LED and the electronic dice) this is completely sufficient, and it helps us to cut down the number of externally required devices. The internal oscillator can be turned on using a so-called configuration bit, and for the PIC16F627A it looks like this:
#pragma config FOSC = INTOSCIO
INTOSCIO stands for Internal Oscillator, where the ports RA6 and RA7 (see the schematic above) can be used as regular IO ports. This can be a useful trick to keep in mind :)
Sneak peek: creating a 1Hz clock signal on a PIC16F627A
Alright, this is basically what I wanted to talk about in this week's article. But why don't we always use the internal oscillator if it is so much more convenient? Well, sometimes it is not precise enough!
Say we want to build a clock. And yes, we will soon build a clock! Then we need to have a very precise oscillator circuit so that we can make sure our clock runs accurately. We will use a crystal with the tuned frequency of 4.194304MHz. Why this strange number? Because 4194304 = 2^22, that is, 2 to the twenty-second power! This is nice because this way it is very easy for a computer, whose calculations are based on the binary system (so, basically, the number 2) to divide this frequency to arrive at a frequency of one tick per second (1 Hertz).
If you want to learn more about this idea, check out Steve Mould's YouTube video on the same idea based on a 32768kHz crystal (which is 2^15). Same idea!
This week's article was a bit more on the theoretical side, I know. Let me know if you like this kind of article, and please also share any open questions you may still have with me on social media! I will do my best to improve my explanations :)