← All course materialsLecture 03 · LED control & timingReading notes →

Lecture 03: Raspberry Pi, LEDs, and PWM

1 / 23
PHYS 351 · Lecture 0301

Raspberry Pi 5: a computer on one board

Official photograph of the Raspberry Pi 5 board, with the processor, GPIO header, USB connectors, Ethernet jack, and display connectors visible.

A complete Linux computer, about the size of a credit card.

Processor + memory + storage + connections

Python can control real electronics through its GPIO pins.

Board photo and specifications: Raspberry Pi 5.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0302

The Pi 5 ports and connectors

Freenove diagram of Raspberry Pi 5 connectors: GPIO header, PCIe, power button, USB-C power, RTC battery, UART, micro HDMI, camera and display, fan, USB and Ethernet. The microSD slot is underneath the board.

microSD storage is underneath. USB: two USB 3.0 and two USB 2.0 ports. Diagram: Freenove.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0303

Why use a Pi in an instrumentation lab?

Python programChoose what happens
and when it happens
GPIO interfaceWrite a voltage
or read a logic level
Physical systemLight, motion,
sensor readings
Raspberry Pi Foundation illustration of an LED with a resistor in series on the blue anode lead, and a black cathode return lead.

The circuit sets the current.

The program sets the behavior.

The same wiring can blink, dim, or signal an event.

Wiring image: Raspberry Pi Foundation, CC BY-SA 4.0.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0304

What can we build with it?

LED photo: Adafruit. Other images: course assets.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0305

Raspberry Pi OS runs on Linux

Raspberry Pi OS desktop on a monitor, showing the taskbar and desktop interface.

Raspberry Pi OS combines Debian Linux with a graphical desktop.

Your applicationsVS Code, terminal, Python
Operating systemFiles, processes, permissions
Linux kernel + driversCPU, memory, device access
Pi hardwareProcessor, storage, GPIO

Desktop image and OS background: Raspberry Pi.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0306

The terminal makes actions repeatable

$ mkdir lab3
$ cd lab3
$ python3 -c "print('Hello, Pi!')"
Hello, Pi!

Commands can run programs, search files, and save output automatically.

CommandMeaning
mkdirMake a folder
cdChange folder
python3Run Python
Ctrl+CStop a program

Edit in VS Code. Run in the terminal.

Keep the Linux command-line guide open. The lab Pis and libraries are already prepared. No installation is needed.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0307

GPIO switches between two voltage levels

Ideal digital output alternating between 3.3 volts HIGH and 0 volts LOW. For the illustrated sourcing circuit, HIGH turns the LED on and LOW turns it off.

General-purpose input/output. Today we configure an output: HIGH ≈ 3.3 V, LOW ≈ 0 V. The LED circuit will turn on when the output is HIGH.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0308

BCM 17 means physical pin 11

Raspberry Pi 40-pin header map distinguishing physical pin positions from BCM GPIO numbers and marking 3.3 V, 5 V, ground, I²C, UART, and SPI connections. Power pins are not GPIO inputs.
In the programOn the header
BCM 17Pin 11
GNDPin 6
3.3 V powerPin 1
5 V powerPins 2 and 4
GPIO.setmode(GPIO.BCM)

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

Power pins are fixed supplies. GPIO pins are programmable.

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0309

An LED conducts in one direction

Adafruit photograph of a clear through-hole LED showing its lens and two unequal-length leads.
Adafruit LED symbol: conventional current flows from the anode toward the cathode, with arrows indicating emitted light.

Anode (+)
Usually the longer lead.

Cathode (−)
Usually the shorter lead and flat rim.

Conventional current flows from anode to cathode when forward biased.

Illustrations: Adafruit, All About LEDs. Check the component if its leads have been trimmed.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0310

The LED driver is one complete current path

Raspberry Pi Foundation wiring illustration: BCM 17, physical pin 11, connects through a resistor to the LED anode. The cathode connects to GND, physical pin 6. The source depicts 330 ohms; the course uses 470 ohms.

GPIO 17 supplies current when HIGH.

Series resistor limits current.

LED cathode returns to GND.

Use 470 Ω in our lab. The source illustration uses 330 Ω.

Raspberry Pi Foundation wiring illustration, CC BY-SA 4.0. The header connections also apply to Pi 5.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0311

The resistor sets the on-current

Voltage-budget bar: a 3.3 volt output is split into a 2.0 volt LED forward drop and a 1.3 volt drop across the series resistor.

For an LED with VF=2.0 VV_F=2.0\,\mathrm{V}:

Ion=3.3−VFRI_{\mathrm{on}}=\frac{3.3-V_F}{R}

Ion=1.3 V470 Ω≈2.8 mAI_{\mathrm{on}}=\frac{1.3\,\mathrm{V}}{470\,\Omega}\approx2.8\,\mathrm{mA}

The GPIO supplies a few mA.
The resistor drops the remaining voltage.

VFV_F depends on the LED and its current. Use a measured value for your circuit.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0312

The same circuit on a breadboard

Raspberry Pi Foundation breadboard diagram: GPIO 17 connects via the blue wire and a series resistor to the red LED anode. The LED cathode connects via the black wire to GND. An older Pi is shown with the same header positions used on Pi 5. The course uses 470 ohms in place of the illustrated 330 ohms.

Each group of five holes shares one metal contact.

The center gap separates the two sides.

Follow GPIO 17, resistor, LED, GND through the connections.

Our resistor: 470 Ω.

Raspberry Pi Foundation, CC BY-SA 4.0. Older board shown, same GPIO 17 and GND positions. Source resistor: 330 Ω.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0313

The GPIO controls a small load

Raspberry Pi Foundation illustration showing an LED and a resistor connected in series between GPIO 17 and ground on a Raspberry Pi header.

A few mA is enough for an indicator LED.

ConnectionRule
LEDA resistor in series
Several LEDsOne resistor per LED
GPIO input3.3 V logic only
Motor or large loadSeparate driver stage

Never connect 5 V to a GPIO signal pin.

Power off before changing wiring. Image: Raspberry Pi Foundation, CC BY-SA 4.0.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0314

RPi.GPIO connects Python to the pins

import RPi.GPIO as GPIO

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

StageCallEffect
NumberingGPIO.setmode(GPIO.BCM)Use GPIO names
ConfigureGPIO.setup(17, GPIO.OUT, initial=GPIO.LOW)Output, initially off
DriveGPIO.output(17, GPIO.HIGH)LED on
DriveGPIO.output(17, GPIO.LOW)LED off
ReleaseGPIO.cleanup()Release configured pins

The prepared lab Pi 5 image supports this API. RPi.GPIO reference. Use the installed course library.

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0315

Sample program: blink one LED

import RPi.GPIO as GPIO
from time import sleep
LED = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)
try:
    for i in range(10):
        GPIO.output(LED, GPIO.HIGH)
        sleep(1.0)
        GPIO.output(LED, GPIO.LOW)
        sleep(0.5)
finally:
    GPIO.cleanup()

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

blink.py

BCM 17
Physical pin 11

1.0 s on
0.5 s off
10 cycles

finally releases the pin, including after Ctrl+C.

Run from the file’s folder: python3 blink.py

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0316

The blink period includes both delays

Ideal blink waveform: one second HIGH, half a second LOW, giving a period of 1.5 seconds and frequency of two thirds of a hertz.

Ideal T=1.0+0.5=1.5 sT=1.0+0.5=1.5\,\mathrm{s}, so f=1/T≈0.67 Hzf=1/T\approx0.67\,\mathrm{Hz}. Python and Linux add overhead and timing variation. Measure the actual period on the scope.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0317

Fast switching can look like steady light

Two ideal signals at equal 50 percent duty cycle: slow switching at 1 hertz over a two-second window and faster switching at 100 hertz over a 50-millisecond window. The fast trace expands the time scale to show individual pulses. Flicker visibility varies with the observer and conditions.

The LED still switches fully on and off. At sufficiently high frequency, vision blends the flashes. Flicker visibility depends on brightness, motion, and the observer.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0318

PWM controls the fraction of time on

Ideal PWM waveform: HIGH for 3 milliseconds in each 10-millisecond period. Duty is 30 percent and frequency is 100 hertz.

Pulse-width modulation: d=ton/T=3/10=0.30d=t_{\mathrm{on}}/T=3/10=0.30. Duty D=30%D=30\%. Here f=1/T=100 Hzf=1/T=100\,\mathrm{Hz}.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0319

More on-time gives more average light

Three ideal PWM waveforms at 20, 50, and 80 percent duty and a common 10-millisecond period. Dashed average voltages are 0.66, 1.65, and 2.64 volts. Each instantaneous output still switches between 0 and 3.3 volts.

Iavg≈dIonI_{\mathrm{avg}}\approx d I_{\mathrm{on}}. With Ion=2.8 mAI_{\mathrm{on}}=2.8\,\mathrm{mA}, 50% duty gives about 1.4 mA average. The pin still switches between 0 and 3.3 V. Perceived brightness is nonlinear.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0320

Duty and frequency are separate controls

Two ideal PWM waveforms both at 25 percent duty: 100 hertz with 2.5-millisecond pulses, and 200 hertz with 1.25-millisecond pulses. Frequency changes without changing the ideal average.

Both traces have 25% duty and the same ideal average. Doubling frequency halves the period and on-time. Duty sets average light. Frequency affects visible flicker.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0321

The PWM library schedules the switching

CallMeaning
GPIO.PWM(17, 100)100 Hz, BCM 17
pwm.start(0)0% duty
pwm.ChangeDutyCycle(30)Set 30% duty
pwm.stop()Stop switching

Duty uses 0–100, not 0–1.

Requested regular PWM edges compared with illustrative late, irregular software-timed edges. These are teaching plots, not measured data.

Software PWM: scheduled edges.
Hardware PWM: clocked counter.

RPi.GPIO PWM API: software timing. A hardware-capable pin alone does not select hardware PWM. Traces are illustrative.

© Ran Yang, Ph.D.Advanced Instrumentation
PHYS 351 · Lecture 0322

Sample program: change the LED brightness

import RPi.GPIO as GPIO
from time import sleep
LED = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT, initial=GPIO.LOW)
pwm = GPIO.PWM(LED, 100)  # frequency in Hz
try:
    pwm.start(20)        # duty in percent
    sleep(2)
    pwm.ChangeDutyCycle(80)
    sleep(2)
finally:
    pwm.stop()
    GPIO.cleanup()

Example only. Do not copy this code as it is: read it, learn from it, then write your own.

pwm_demo.py

Same LED circuit.
Same 100 Hz rate.

20% for 2 s.
80% for 2 s.

The library toggles the pin while the main program sleeps.

Look for a change in pulse width on the scope and a change in LED brightness.

© Ran Yang, Ph.D.Example code only: learn from it, then write your ownAdvanced Instrumentation
PHYS 351 · Lecture 0323

Live demo: the Pi, LED, and oscilloscope

Raspberry Pi Foundation breadboard diagram: GPIO 17 connects via the blue wire and a series resistor to the red LED anode. The LED cathode connects via the black wire to GND. An older Pi is shown with the same header positions used on Pi 5. The course uses 470 ohms in place of the illustrated 330 ohms.

Pi + terminal
Run the sample programs.

Breadboard + 470 Ω + LED
Watch blinking and dimming.

Oscilloscope
Probe tip: GPIO 17.
Ground clip: circuit GND.

Setup overview only. Construction and measurements: Lab 3 manual. Diagram: Raspberry Pi Foundation, CC BY-SA 4.0.

© Ran Yang, Ph.D.Advanced Instrumentation

Use ← → to move, Home / End to jump, and F for fullscreen.

Figure descriptions

Slide 1 · Raspberry Pi 5: a computer on one board

Official photograph of the Raspberry Pi 5 board, with the processor, GPIO header, USB connectors, Ethernet jack, and display connectors visible.

Slide 2 · The Pi 5 ports and connectors

Freenove diagram of Raspberry Pi 5 connectors: GPIO header, PCIe, power button, USB-C power, RTC battery, UART, micro HDMI, camera and display, fan, USB and Ethernet. The microSD slot is underneath the board.

Slide 3 · Why use a Pi in an instrumentation lab?

Raspberry Pi Foundation illustration of an LED with a resistor in series on the blue anode lead, and a black cathode return lead.

Slide 4 · What can we build with it?

Adafruit photograph of a clear through-hole LED showing its lens and two unequal-length leads.

Photograph of an MCP9808 digital temperature-sensor breakout board used for temperature measurements.

NEMA 17 stepper motor with a square body, output shaft, mounting holes, and four coil leads. Photo: Adafruit.

Windmills on the riverbank at Zaandam, the Netherlands, under an overcast sky.

Slide 5 · Raspberry Pi OS runs on Linux

Raspberry Pi OS desktop on a monitor, showing the taskbar and desktop interface.

Slide 7 · GPIO switches between two voltage levels

Ideal digital output alternating between 3.3 volts HIGH and 0 volts LOW. For the illustrated sourcing circuit, HIGH turns the LED on and LOW turns it off.

Slide 8 · BCM 17 means physical pin 11

Raspberry Pi 40-pin header map distinguishing physical pin positions from BCM GPIO numbers and marking 3.3 V, 5 V, ground, I²C, UART, and SPI connections. Power pins are not GPIO inputs.

Slide 9 · An LED conducts in one direction

Adafruit photograph of a clear through-hole LED showing its lens and two unequal-length leads.

Adafruit LED symbol: conventional current flows from the anode toward the cathode, with arrows indicating emitted light.

Slide 10 · The LED driver is one complete current path

Raspberry Pi Foundation wiring illustration: BCM 17, physical pin 11, connects through a resistor to the LED anode. The cathode connects to GND, physical pin 6. The source depicts 330 ohms; the course uses 470 ohms.

Slide 11 · The resistor sets the on-current

Voltage-budget bar: a 3.3 volt output is split into a 2.0 volt LED forward drop and a 1.3 volt drop across the series resistor.

Slide 12 · The same circuit on a breadboard

Raspberry Pi Foundation breadboard diagram: GPIO 17 connects via the blue wire and a series resistor to the red LED anode. The LED cathode connects via the black wire to GND. An older Pi is shown with the same header positions used on Pi 5. The course uses 470 ohms in place of the illustrated 330 ohms.

Slide 13 · The GPIO controls a small load

Raspberry Pi Foundation illustration showing an LED and a resistor connected in series between GPIO 17 and ground on a Raspberry Pi header.

Slide 16 · The blink period includes both delays

Ideal blink waveform: one second HIGH, half a second LOW, giving a period of 1.5 seconds and frequency of two thirds of a hertz.

Slide 17 · Fast switching can look like steady light

Two ideal signals at equal 50 percent duty cycle: slow switching at 1 hertz over a two-second window and faster switching at 100 hertz over a 50-millisecond window. The fast trace expands the time scale to show individual pulses. Flicker visibility varies with the observer and conditions.

Slide 18 · PWM controls the fraction of time on

Ideal PWM waveform: HIGH for 3 milliseconds in each 10-millisecond period. Duty is 30 percent and frequency is 100 hertz.

Slide 19 · More on-time gives more average light

Three ideal PWM waveforms at 20, 50, and 80 percent duty and a common 10-millisecond period. Dashed average voltages are 0.66, 1.65, and 2.64 volts. Each instantaneous output still switches between 0 and 3.3 volts.

Slide 20 · Duty and frequency are separate controls

Two ideal PWM waveforms both at 25 percent duty: 100 hertz with 2.5-millisecond pulses, and 200 hertz with 1.25-millisecond pulses. Frequency changes without changing the ideal average.

Slide 21 · The PWM library schedules the switching

Requested regular PWM edges compared with illustrative late, irregular software-timed edges. These are teaching plots, not measured data.

Slide 23 · Live demo: the Pi, LED, and oscilloscope

Raspberry Pi Foundation breadboard diagram: GPIO 17 connects via the blue wire and a series resistor to the red LED anode. The LED cathode connects via the black wire to GND. An older Pi is shown with the same header positions used on Pi 5. The course uses 470 ohms in place of the illustrated 330 ohms.