Computing crystal load capacitance using Python & UliEngineering
When you implement a crystal oscillator circuit, one task that is both essential and often overlooked is how to compute the load capacitors of the crystal.
Tip: In many cases it is easier to use an integrated crystal oscillator for which you don’t have to care about load capacitors. Crystals are usually recommendable for low-power applications and high-volume manufacturing where oscillators are too expensive.
In this example, we’ll compute the load capacitors for a Abracon ABLS-16.000MHZ-B4-T (specified with a load capacitance of 18 pF
according to the datasheet).
It is paramount to understand that 18 pF load capacitance specification does not mean that you can use 18 pF
capacitors or 2x 9 pF
capacitors. You need to actually calculate the correct values.
You need the following information:
- Load capacitance of the crystal, specified in the crystal’s datasheet.
- An estimate of the stray capacitance, i.e. the capacitance of the PCB traces from the crystal to the microcontroller. If you don’t know this, I recommend to use
2 pF
- The pin capacitance of the microcontroller (or whatever device you want to connect your device to) as specified in the microcontroller’s datasheet. Often the capacitance of oscillator pins is not the same as for other pins and is hence specified separately.
3 pF
is a good first estimate for most modern ICs. The value must be specified per pin!
In order to install UliEngineering (a Python 3 library) run:
sudo pip3 install -U UliEngineering
Now we can compute the correct load capacitors using:
from UliEngineering.EngineerIO import auto_print
from UliEngineering.Electronics.Crystal import *
# Print load capacitors: prints "29.0 pF"
auto_print(load_capacitors, cload="18 pF", cpin="3 pF", cstray="2 pF")
In this case, you need to have two load capacitors of 29.0 pF
each.
You should use the closest available value of load capacitor, but always check the resulting frequency if you have high clock accuracy requirements.
Tip: You can pass both numbers (like 18e-12
) or strings (like 18 pF
or 0.018 nF
) to most UliEngineering functions. SI prefixes like p
and n
are automatically decoded
If you want to use the value programmatically, call load_capacitors()
directly:
from UliEngineering.EngineerIO import auto_print
from UliEngineering.Electronics.Crystal import *
# Coompute the load capacitor value (for both of the two load caps):
load_caps = load_capacitors(cload="18 pF", cpin="3 pF", cstray="2 pF")
# load_caps = 2.9e-11