Python & GPIO: code meets circuit
420-302-VA · WEEK 4 · FALL 2026

Week 4 · Monday, September 21, 2026 · Room D-221

Your first program moves real electrons.

The platform is built, secured and reachable. Today code enters the picture: Python from its very first line, a clean environment to run it in, and by the end of the lab a circuit you wired that obeys a script you wrote. These six pages are the whole path.

1 h theory 2 h lab Bring the breadboard kit
The Week 4 path: kit check, Python basics, project environment, GPIO rules, LED and button circuit, lab hand-in 1Kit check 2Python 3Environment 4GPIO rules 5First light 6Hand-in Week 5: requirements

What happens today

Theory · first hour

What a program actually is

Instructions, executed in order, by an interpreter. Variables, decisions, loops and functions, the four moves every program is built from. Then the environment idea: why each project gets its own toolbox, and what a GPIO pin is electrically.

Start with Python from zero →

Lab · two hours

From hello.py to a working circuit

Boot the same drive, connect over SSH, create a virtual environment, write and run your first scripts, then wire an LED and a button on the breadboard and drive both with gpiozero. Everything gets committed and pushed, as always.

Start the lab at Environments →

In the course outline

This is Week 4 of 420-302-VA and the start of the Python thread: Week 5 adds requirements and pseudocode, Week 6 object-oriented programming, and the midterm with its reflection log lands in Week 7 (October 19), covering everything from Week 1 to Week 6. The journal notes you make this week feed that reflection log directly. From today until the end of the semester, bring the breadboard kit to every class.

What is on the bench

Same station as always (left set lab PC, right set Pi), plus, for the first time, your own components:

Why Python, and why now

You already program in a sense: ladder logic, function blocks, relay diagrams. Those languages excel at what PLCs do, deterministic control of fixed hardware, and struggle at everything around it: talking to web services, parsing data, driving a dashboard, gluing systems together. That "everything around it" is exactly what IoT is, and Python is the language the field settled on for it:

  • It reads like the pseudocode you will write in Week 5. if button.is_pressed: led.on() means what it says. For a first text language, that matters more than raw speed.
  • The libraries already exist. GPIO control (gpiozero), MQTT messaging (Week 9), web dashboards (Flask, Week 11): each is an import away, written and tested by others. You assemble systems instead of reinventing parts.
  • It is the Raspberry Pi's home language. The Pi was built for it, the OS ships it, and every official example uses it. The same language runs on your laptop and, in a small dialect, even on microcontrollers.
  • It is the industry's glue. Automation, data logging, test benches, machine learning: Python is the common denominator on job postings that mention any of them.

Why now and not in Week 1: because code that controls hardware needs the hardware to exist first. You spent three weeks building a computer you trust and a workflow that preserves your work. Today both start paying rent.

Three ideas you will reuse for the rest of the course

A program is a precise recipe

The interpreter does exactly what the text says, in order, without judgement. Every bug is a difference between what you meant and what you wrote. Debugging is reading your own recipe honestly, and Python's error messages point at the line.

One project, one toolbox

A virtual environment gives each project its own Python and its own libraries, so projects cannot break each other or the OS. The habit costs two commands per session and prevents the classic "it worked yesterday" disasters.

GPIO is a software-controlled wire

A pin is just a point your code can pull to 3.3 V or 0 V, or read. All your electronics knowledge applies unchanged; the only new part is that the switch flipping it is a line of Python.

What you will be able to do by the end of the week

  • Explain what an interpreter does with a script, and use the REPL as a calculator and experiment bench.
  • Write and run Python scripts using variables, if/else, while and for loops, and simple functions.
  • Read a traceback from the bottom up and fix the line it names.
  • Create and activate a virtual environment, install a package into it with pip, and explain why pip outside an environment is refused on the Pi.
  • State the GPIO electrical rules (3.3 V logic, current limits, resistor with every LED) and translate between BCM and physical pin numbers.
  • Wire an LED and a button on the breadboard and control them with gpiozero, by polling and by events, over SSH.

Words you will hear all day

TermPlain meaningCommon mix-up to avoid
InterpreterThe program (python3) that reads your text file and performs it, line by line.Python the language versus python3 the interpreter that runs it.
ScriptA text file of Python instructions, run start to finish: python3 blink.py.Saving a file does nothing by itself; running it does.
REPLThe interactive >>> prompt: type one line, see its result immediately.Work you type there vanishes when you quit; scripts are for keeping.
VariableA name attached to a value: delay = 0.5.Not a box with a fixed type; the same name can point at anything.
Library / moduleCode written by others that import brings into your program, like gpiozero.Importable only if it is installed in the environment you are running.
pipPython's own package installer, fetching libraries from the Python Package Index (PyPI).Not apt: apt installs system software, pip installs Python libraries into an environment.
Virtual environment (venv)A project-private Python plus its own installed libraries, activated per terminal session.It is a folder, not a virtual machine; deleting the folder deletes the environment, nothing else.
GPIOGeneral-Purpose Input/Output: the 40 header pins your code can drive or read.Two numbering systems exist; gpiozero uses BCM numbers (GPIO17), not physical positions (pin 11).
Pull-up / pull-downA resistor holding an input at a known level so it does not float; the Pi has internal ones your code selects.Without one, an unconnected input reads random noise, not 0.
TracebackPython's error report: the file, the line, and the error type.Read it bottom-up; the last line is the diagnosis, the lines above are the trail.

Where this fits in the course

Week 3 · Sep 14

Secure configuration

The Pi became trustworthy: updated, key-only pushes to GitHub, firewalled, remotely reachable. Today's whole lab runs over that SSH setup.

Week 4 · Sep 21 · this hub

Python and GPIO

First code, first circuit: the interpreter, a clean environment, the pin rules, and an LED and button under program control.

Week 5 · Sep 28

Requirements and pseudocode

Before programs grow, learn to specify them: what the system must do, written precisely, then sketched in pseudocode before any Python.

Working at home

Python itself runs anywhere: install it from python.org (Windows/macOS; Linux has it) and everything on the Python page works identically on your laptop, REPL, scripts, venvs included. Only the GPIO parts need the Pi. With your own boot drive (see Week 2, working at home) and a few components, the whole lab reproduces at home; gpiozero even has a remote mode for driving a Pi's pins from a PC, linked on the Resources page.