gwordal

Lesson 4 of 5 · 22 min

Capacitors and RC circuits

A resistor responds instantly: change the voltage and the current changes at once. A capacitor does not. It stores charge, and storing takes time. That delay is useful: it lets you smooth noise, filter out button bounce, and keep a chip alive during the nanoseconds when its supply wobbles. All it takes is a resistor, a capacitor and one number, the time constant.

What a capacitor is

A capacitor is two conductive plates separated by a thin insulator. No current flows through the insulator, but when you apply a voltage, charge piles up on one plate and an equal opposite charge on the other. The more charge it holds per volt, the bigger its capacitance:

C = Q / V

The unit is the farad (F). One farad is one coulomb per volt, a huge value, so real parts are measured in microfarads (1 µF = 10^-6 F), nanofarads (1 nF = 10^-9 F) and picofarads (1 pF = 10^-12 F). A common decoupling capacitor is 100 nF, which is 0.1 µF.

Two facts follow from the definition.

Current needs a changing voltage. Differentiating Q = C * V with respect to time gives I = C * dV/dt. If you push a steady 1 mA into a 100 µF capacitor, its voltage ramps at dV/dt = I / C = 0.001 A / 100 x 10^-6 F = 10 V/s. A capacitor fully charged to a steady voltage passes no current, and a sudden voltage change would need infinite current, so the voltage on a capacitor cannot jump.

It stores energy. E = 1/2 * C * V^2. A 100 µF capacitor at 5 V holds 0.5 * 100 x 10^-6 * 5^2 = 1.25 mJ. That is tiny compared with a battery, but plenty to feed a chip for a few microseconds.

Charging through a resistor

Connect a capacitor to a supply Vs through a resistor R. At the start the capacitor is empty and has 0 V across it, so the full Vs appears across R and the current is Vs / R. As charge accumulates, the capacitor voltage rises, the voltage left across R shrinks, and the current falls. The charging slows down as it fills, which is why the curve is exponential rather than a straight line:

Vc(t) = Vs * (1 - e^(-t / (R * C)))

The product R * C has the unit of time and is the time constant:

tau = R * C

Take R = 10 kΩ and C = 100 µF:

tau = 10 000 Ω * 100 x 10^-6 F = 1 s

Charging toward Vs = 5 V:

TimeFraction chargedVc
1 tau = 1 s63.2 percent3.16 V
2 tau = 2 s86.5 percent4.32 V
3 tau = 3 s95.0 percent4.75 V
5 tau = 5 s99.3 percent4.97 V

The rule of thumb is that after about 5 time constants the capacitor is, for practical purposes, fully charged.

Discharging

Remove the supply and let the capacitor discharge through the same resistor. The voltage decays by the same pattern in reverse:

Vc(t) = V0 * e^(-t / (R * C))

After one tau it has fallen to 36.8 percent of where it started: from 5 V to 1.84 V. After 2 tau it is 0.68 V and after 3 tau only 0.25 V.

You can also solve for the time to reach a threshold. How long does the 10 kΩ and 100 µF circuit above take to charge from 0 V to 3 V, roughly where a 5 V microcontroller input begins to read HIGH?

  1. Rearrange: t = -tau * ln(1 - Vth / Vs)
  2. Substitute: t = -1 s * ln(1 - 3 / 5) = -1 s * ln(0.4)
  3. Evaluate: ln(0.4) = -0.916, so t = 0.916 s

Because tau sets the speed, you can pick R and C to give almost any delay: bigger R or bigger C means slower.

Decoupling capacitors

Why does every datasheet say to place a 100 nF capacitor right next to each chip's supply pin?

A digital chip draws current in sharp spikes: each time internal gates switch, a pulse of current is pulled for a few nanoseconds. The power supply is at the far end of a wire, and every wire has inductance, roughly 1 µH per metre. A 20 cm wire is about 200 nH. An inductor resists changes in current, producing a voltage V = L * dI/dt. A spike of 20 mA rising in 10 ns means dI/dt = 0.02 A / 10 x 10^-9 s = 2 x 10^6 A/s, so:

V = 200 x 10^-9 H * 2 x 10^6 A/s = 0.4 V

The chip's supply would dip by 0.4 V for a moment, enough to cause resets or glitches. A 100 nF capacitor right beside the pin acts as a tiny local reservoir. It supplies the spike itself, and the wire only has to replenish it slowly. The charge in a 20 mA, 10 ns spike is Q = I * t = 0.02 A * 10 x 10^-9 s = 2 x 10^-10 C, so the voltage dip on the capacitor is:

dV = Q / C = 2 x 10^-10 C / 100 x 10^-9 F = 2 mV

Two millivolts instead of 400. That is the whole idea of decoupling. The capacitor has to be physically close, with short leads or traces, because the wire between them is exactly the inductance you are trying to avoid. For motors and servos, add a larger bulk capacitor (47 to 470 µF) near the driver to cover the slower, bigger current swings.

Debouncing a button with RC

A mechanical switch does not close cleanly. The contacts bounce, making and breaking several times over a few milliseconds before settling. A fast microcontroller sees each bounce as a separate press.

An RC filter slows the edges down so that brief bounces never cross the logic threshold. Wire the button from the input to ground, with a pull-up resistor R = 10 kΩ to 5 V and a capacitor C = 1 µF from the input to ground:

tau = 10 000 Ω * 1 x 10^-6 F = 10 ms

When the button opens, the input rises through the pull-up like the charging curve. It needs t = -tau * ln(1 - 3/5) = 0.916 * 10 ms = 9.2 ms to reach the 3 V HIGH threshold. A bounce shorter than that never lets the voltage get there before the contact closes again and drains the capacitor, so the noise is filtered out. When the button closes, the capacitor dumps through the contacts almost instantly. To smooth that edge too, add a 1 kΩ resistor in series with the button: the fall time constant becomes 1 kΩ * 1 µF = 1 ms, and the peak discharge current is limited to 5 V / 1 kΩ = 5 mA.

Hardware debouncing costs a couple of parts and removes the need for software delays, but it also delays a real press by several milliseconds. Many projects use a simple software debounce (ignore changes for about 20 ms) instead, which is free. Use whichever suits the design.

Check yourself

A circuit has R = 47 kΩ and C = 10 µF. What is the time constant?

Check yourself

Where should a 100 nF decoupling capacitor be placed?