Skip to content

5. Peripherals

Time: 2 to 3 weeks · Board: Pico 2 · Prerequisites: Modules 3 and 4

Why this matters

A microcontroller is a CPU surrounded by peripherals, and the job is mostly configuring those peripherals correctly from a several-hundred-page reference manual. GPIO, timers, PWM, ADC, interrupts, DMA, and the watchdog show up in every product. Learn each one once at the register level and you will recognize it on every chip you ever use, because they all work the same way with different register names.

You will be able to

  • Configure a GPIO as input with pull-up or pull-down, or as push-pull or open-drain output, and explain when each is right.
  • Set up a hardware timer with a prescaler to fire at an exact rate, generate PWM, and measure a pulse width with input capture.
  • Read an analog voltage with the ADC and explain resolution, reference voltage, sampling time, and why the reading is noisy.
  • Write an interrupt service routine that is short, safe, and shares data correctly with main().
  • Move data with DMA without the CPU touching it, and explain what the watchdog is for.

Learn

GPIO

Timers and PWM

ADC

  • Article Analog to Digital Conversion by SparkFun ~20 min
    Resolution, reference voltage, and converting a count back to volts.
  • Docs RP2350 datasheet: ADC chapter ~1 h
    Pay attention to the sampling rate, the input impedance requirements, and the noise notes. Real ADCs are not ideal.

Interrupts

DMA and watchdog

  • Docs RP2350 datasheet: DMA chapter ~1 h
    Channels, transfer sizes, chaining, and pacing from a peripheral's data-request signal.
  • Article Great Watchdogs by Jack Ganssle ~45 min
    Why every shipped product has one, and how to use it so it actually catches failures.
  • Article Firmware Watchdog Best Practices by Memfault Interrupt ~30 min
    Hardware plus software watchdogs, and capturing why a reset happened.

Do

All register-level, no SDK peripheral functions, on the Pico 2. You may use the SDK for stdio and clocks. Keep the datasheet open.

  • Checkpoint 5.1: Button, LED, debounce. Configure one GPIO as input with an internal pull-up and a button to ground. Read it in a loop and light an LED while pressed. Then add Ganssle-style software debouncing and count presses; print the count over UART. Show that without debouncing the count is wrong.
  • Checkpoint 5.2: Timer and PWM. Configure a PWM slice from its registers to drive your LED at 1 kHz. Sweep the duty cycle so the LED breathes. Measure the frequency with your multimeter if it has a frequency mode, or with the logic analyzer from Module 6.
  • Checkpoint 5.3: Read a knob. Wire a potentiometer between 3.3 V and ground with the wiper on an ADC pin. Read it from the ADC registers, convert to millivolts, and print it. Take 64 readings in a row and print the min and max: that spread is your noise. Then average 16 samples and show the spread shrink.
  • Checkpoint 5.4: Interrupt-driven button. Replace the polled button with a GPIO interrupt. The handler must only set a volatile flag and clear the interrupt; main() does the work. Then deliberately do something slow in the handler and observe what breaks.
  • Checkpoint 5.5: DMA and watchdog. Use a DMA channel to copy a 256-byte buffer to another and confirm the copy with no CPU loop. Then enable the watchdog with a 1-second timeout, feed it in your main loop, and add a "hang" command over UART that stops feeding it. Watch the board reset, and read the reset-reason register on boot to prove why.

Check yourself

  • Why does an I2C bus need open-drain outputs and pull-up resistors rather than push-pull?
  • A 16-bit timer runs from a 150 MHz clock. What prescaler and compare value give a 1 Hz interrupt?
  • A 12-bit ADC with a 3.3 V reference reads 2048. What voltage is that, and what is one LSB in millivolts?
  • Name three things an interrupt handler should never do.
  • What happens if you clear an interrupt flag after the work instead of before, and the event fires again in between?
  • Why does a watchdog in the main loop not catch a stuck interrupt handler, and what does?

Go deeper

Optional extras
  • Video Raspberry Pi Pico Bare Metal Programming by Low Byte Productions Clocks, DMA, and PIO videos, ~2 h
    The same chip family driven from the registers on video.
  • Video Level Up Your Arduino Code: Registers by SparkFun ~20 min
    If you came from Arduino, this shows exactly what digitalWrite() was hiding.
  • Article Introduction to Microcontrollers series index by Mike Silva Reference
    The rest of the series: buttons and bouncing, ADC, driving displays, and more.
  • Docs RP2350 datasheet: PIO chapter ~2 h
    The Pico's programmable I/O is unique: tiny state machines that bit-bang protocols at hardware speed. Not needed for the path, but delightful.