Quick tip: Decoupling Capacitors
Decoupling capacitors are a critical component for circuits that switch at a high speed. Electricity has a limited speed of propagation and, when a large transient current draw is placed on a power supply (as in a bunch of transistors changing state when you print "Hello, world!"), voltage within a microcontroller can drop before adequate current can be supplied.
There are two ways of considering how a capacitor works--in the time domain and in the frequency domain. Let's look at the time domain model first.
\[ I = C \frac{dV}{dt} \]
Where I is current (charge per second), C is capacitance, V is voltage, and t is time. This implies that any change in voltage will necessarily "push" or "pull" current out of or into a capacitor. Let's visualize this with the Falstad Circuit Simulator.
This RLC (resistor, inductor, capacitor) oscillator swings up and down +/-10V. The capacitor here works with the inductor to boost the voltage from ~.3V to 10V. The capacitor sources and sinks current as the oscillations continue--in a sense it is acting as a very very tiny battery.
How does this relate to a circuit using a microcontroller? When a particularly strenuous bit of calculation happens, a ton of gates on the microcontroller change state all at once. MOSFET, the technology used in modern microcontrollers, is used because of its miniscule on current. The switching part of the MOSFET (the gate) simply needs to be charged or discharged. Some discrete MOSFETs have ~18nC gate charge. Microcontroller MOSFETs will be even lower. Not a lot per device but it quickly scales with many thousands of transistors on even simple microcontrollers. Let's take a look at what that might look like in Falstad.
For this toy example, it can be seen that as soon as the transistor is closed a large amount of current is sourced. This causes the voltage to dip close to zero. This effect is greatly exaggerated for demonstration. In reality, the dip in voltage will be significantly shorter and smaller in magnitude.
These variations are important! If a power supply is too noisy, the "high" voltage level of a transistor may not be properly supplied. This may cause the transistor to fall below the threshold of a logic high level and cause a myriad of errors in the microcontroller.
Let's simulate a slightly different scenario with a decoupling capacitor!
You can see as the transistor is switched on and off that the voltage rail becomes quite noisy! +/- 1V around the "logic level" of 5V. Let's add in a decoupling capacitor to source and sink current near the transistor.
Now the voltage ripple is reduced to almost nothing. Thanks decoupling cap.
Another way to consider capacitors is in the "frequency domain." Let's apply a Laplace transform to the above equation describing a capacitor.
\[ I = C \frac{dV}{dt} \]
\[ \mathscr{L} \{ \frac{dV}{dt} \} = sV(s) -V(0) \]
Let's neglect V(0) and assume that it is 0.
\[ I(s) = CsV(s) \]
\[ Z(s) = \frac{V(s)}{I(s)} \]
Substituting the expressions for V(s) and I(s) we derived earlier and canceling out the I(s) terms:
\[ Z(s) = \frac{1}{Cs} \]
Since s represents the complex frequency, we can replace it with jω, where j is the imaginary unit and ω is the angular frequency. This gives us:
\[ Z(jω) = \frac{1}{jωC} \]
Where ω is the angular frequency in radians per second, j is the imaginary unit, and Z is the complex impedance or resistance at a certain frequency.
What does this all mean? Let's take the limiting case where the frequency, ω, is infinite. This would mean that Z approaches zero. It becomes a short. Conversely, let's take the limiting case where the frequency approaches zero. It becomes infinite in impedance. In a decoupling circuit, the capacitor sits across the power rail to ground. Any noise in the circuit sees the capacitor as a very low resistance path to ground! This prevents noise from propagating to the rest of the circuit (i.e. the microcontroller) and ensures that the logic voltage for every transistor is always well defined.
There are a few more rules of thumb about decoupling capacitors. Here's a quick list:
- Place the decoupling capacitor as close to the microcontroller power supply pins as is practical. It may not make a difference for low performance systems but it is a best practice.
- All capacitors should have a via to ground as close to them as possible. Always always always!
- The rule of thumb is one decoupling capacitor of 0.1u-1u per power supply pin. If you're working with a small QFN, absolutely do this. If you're working with a 1000 ball FPGA, include bulk decoupling and evaluate your performance later.
- Tantalum capacitors are polarized! If you put it in backwards it will explode.
- Manufacturer datasheets are the way, the truth, and the light. They are also written by sophomore undergrad interns. Keep both in mind!
I leave you with an excellent presentation by David Jones of EEvblog.