Measuring Capacitor Charge and Discharge

Pico W with a 10 kΩ resistor from GP15 to a 100 µF electrolytic capacitor, GP26 reading the capacitor node, capacitor negative leg to the ground rail

Measuring Capacitor Charge and Discharge

What You'll Learn

  • What a capacitor does, and why its voltage can't jump instantly.
  • Build a simple resistor-and-capacitor (RC) circuit on the breadboard.
  • Measure the capacitor's voltage with the Pico W ADC and plot it over time.
  • Recognise the charging curve and the discharging curve, and the timescale they share.

What You'll Need

  • Raspberry Pi Pico W on a half breadboard
  • One 100 µF electrolytic capacitor — watch its polarity
  • One 10 kΩ resistor
  • A few jumper wires
  • Comfortable reading a voltage from an analog pin (the Reading Analog Values lesson) and reading a plot in the editor (the Using the Plotter lesson)

Background

Here's a question to hold in your head before you build anything: if you connect an empty capacitor to 3.3 V through a resistor, what does its voltage do? Does it snap straight to 3.3 V, or does it take a moment to get there?

A capacitor is a small part that stores electric charge, a bit like a tiny bucket for electricity. A resistor limits how fast charge can flow. Put them together and the bucket can't fill instantly, it has to fill through the narrow neck the resistor allows. Make your prediction now, then build the circuit and measure what actually happens.

Build the Circuit

You'll drive the circuit from a GPIO pin, let the capacitor charge through the resistor, and read the voltage on the capacitor with an analog pin.

  1. Place the 10 kΩ resistor so one leg is in hole f24 and the other in f23.
  2. Connect the Pico's GP15 to the resistor leg in row 24. This end drives the circuit.
  3. Place the 100 µF capacitor with its positive leg in row 23 (the same net as the resistor's other leg) and its negative leg in row 22. This shared point in row 23 is the node you'll measure.
  4. Connect the Pico's GP26 to the capacitor's positive leg. GP26 is analog input ADC0.
  5. Connect the capacitor's negative leg to the negative power rail.
  6. Connect a Pico GND pin to that same negative rail.

Warning: An electrolytic capacitor is polarised, it has a positive and a negative leg, and it must go in the right way round. The negative leg is usually marked with a stripe and is the shorter leg. The negative leg connects to ground here. Reversing it can damage the part.

The measured node charges from 3.3 V through the resistor, so its voltage always stays between 0 V and 3.3 V. That keeps it inside the safe range for the ADC pin, which must never see more than 3.3 V.

Pico W with a 10 kΩ resistor from GP15 to a 100 µF electrolytic capacitor, GP26 reading the capacitor node, capacitor negative leg to the ground railPico W with a 10 kΩ resistor from GP15 to a 100 µF electrolytic capacitor, GP26 reading the capacitor node, capacitor negative leg to the ground rail

Write the Code

This program drives the circuit high to charge the capacitor, then low to discharge it, reading the voltage many times a second and printing it for the plotter. Type it into the editor and run it.

Python

from machine import Pin, ADC
import time

drive = Pin(15, Pin.OUT)   # GP15 drives the RC network through the 10 kOhm resistor
adc = ADC(26)              # GP26 / ADC0 reads the voltage on the RC node

SAMPLE_STEP = 0.02          # seconds between readings (~50 samples per second)
PHASE_TIME = 5              # seconds to watch each of charging and discharging (~5 tau)


def read_volts():
    # read_u16() gives 0-65535; scale it to the 0-3.3 V the ADC measures
    return adc.read_u16() * 3.3 / 65535


def sample_for(seconds):
    start = time.ticks_ms()
    while time.ticks_diff(time.ticks_ms(), start) < seconds * 1000:
        t_ms = time.ticks_diff(time.ticks_ms(), start)   # milliseconds since phase start
        print("{},{:.3f}".format(t_ms, read_volts()))     # CSV: t_ms,volts
        time.sleep(SAMPLE_STEP)


# Charge: drive GP15 high so the capacitor fills up through the resistor.
drive.value(1)
sample_for(PHASE_TIME)

# Discharge: drive GP15 low so the capacitor empties back through the resistor.
drive.value(0)
sample_for(PHASE_TIME)

Each line prints two numbers: the milliseconds since the phase started, then the measured voltage. Turn on the plotter's timestamp toggle so it uses that first column as the time axis. The voltage climbs toward 3.3 V during the charge phase, then falls back toward 0 V during the discharge phase. New to the plotter? See Using the Plotter.

How It Works

Pin(15, Pin.OUT) sets up GP15 as an output you can drive high or low, and ADC(26) sets up GP26 to read voltage. read_volts turns the raw ADC reading (a whole number from 0 to 65535) into an actual voltage between 0 and 3.3 V, so the plot is in units you can read.

sample_for loops for a set number of seconds, printing the elapsed time and the voltage on each pass, with a short time.sleep between readings to set the sample rate. First the code drives GP15 high to charge the capacitor, then low to discharge it.

Look at the shape of the plot. When you drive the pin high, the voltage doesn't jump to 3.3 V, it rises quickly at first and then eases off, curving toward 3.3 V. When you drive the pin low, it drops quickly at first and then eases toward 0 V. The capacitor's voltage can't change all at once because charge has to flow in or out through the resistor, and the resistor only lets it flow so fast. Every RC circuit has a characteristic timescale set by the size of the resistor and the capacitor: bigger values mean a slower, more stretched-out curve. With the 10 kΩ resistor and 100 µF capacitor here, that timescale is about one second, which is why the curve takes a few seconds to flatten out.

Try It

Run the program and watch the plot fill in.

  • During the first few seconds (the charge phase) the line sweeps upward from near 0 V, steep at the start and gentler as it nears 3.3 V. It never quite touches 3.3 V, it just keeps creeping closer.
  • During the next few seconds (the discharge phase) the line falls the same way in reverse: a fast drop that flattens out as it approaches 0 V.

Notice that both curves are steepest at the start and flatten as they go, and that both take roughly the same amount of time to settle. That shared timescale is the fingerprint of your particular resistor and capacitor.

Challenge

Predict first, then check if you can. If you swapped the 10 kΩ resistor for a larger one, say 22 kΩ, what would happen to the curve? Would the capacitor fill faster or slower, and would the curve get steeper or more stretched out? Write down your guess, then try the larger resistor and compare the two plots.

Review

  • Why can't a capacitor's voltage change instantly when you connect it to 3.3 V?
  • What does the shape of the charging curve tell you: why is it steep at first and gentle later?
  • Both the charge and discharge curves take about the same time to settle. What in your circuit decides how long that is?