Raspberry Pi's lessons
What is a Raspberry Pi?
3D tour of a Raspberry Pi 5: processor, memory, GPIO header, USB, Ethernet, power and HDMI.
A Raspberry Pi is a whole computer on one small board. It runs a full operating system, just like a laptop.
It also has a row of pins for wiring up electronics. Code you write can switch lights on, read buttons and talk to sensors.
This course uses the Raspberry Pi 5, released in 2023. Most of it works the same on older models.
A whole computer on one chip
The main chip is a system on a chip, or SoC. It packs the processor cores and the graphics into one package.
On the Pi 5 it has four cores running at 2.4 GHz. The RAM chip beside it holds the programs that are running.
A second chip, the RP1, runs the USB ports, Ethernet and GPIO pins. The microSD card stores the operating system and your files.
Example: one key press travels from the keyboard through the RP1 and the SoC to the screen (FlowChain).
Practice: Match each part to its job
Match board parts to their jobs (MatchPairs), four random pairs a round.
What you need to get started
A Pi 5 needs a USB-C power supply. The official one gives 5 volts at up to 5 amps.
The operating system lives on a microSD card that slots in underneath the board. Use a card of at least 32 GB.
A screen plugs into a micro-HDMI port, and a keyboard and mouse use the USB ports. Later you can skip all three and control the Pi over your network.
Practice: Plug it in
Tap the connector on a top-down board where each device plugs in.
Installing the operating system
The Pi runs Raspberry Pi OS, a version of Linux. You copy it onto the microSD card with a free app called Raspberry Pi Imager.
Imager asks for your Pi model, the OS and the card. It then lets you set a user name, your Wi-Fi details and remote access.
Example: the Imager wizard steps through device, OS, storage, settings, writing and done.
Put the card in the Pi and plug in the power last. The first start takes a minute or two while the OS sets itself up.
Practice: Order the setup
Put the Imager and first-boot steps in order (OrderSteps).
Linux and the terminal
Raspberry Pi OS has a desktop you use with a mouse. It also has a terminal, where you type commands instead.
A command is a short word followed by what it works on. mkdir sensors makes a folder called sensors, and cd sensors moves into it.
Python 3 comes installed, so python3 hello.py runs a Python file. Putting sudo in front runs a command as the administrator.
Example: a terminal session that lists files, runs a Python file, makes a folder and updates software (TerminalDemo).
Practice: Type the command
Type the terminal command for a task (TypedCode), random tasks.
The GPIO header
The GPIO header is the double row of 40 pins along one edge. GPIO stands for general purpose input and output.
Some pins give power at 3.3 volts or 5 volts, and eight are ground pins at 0 volts. Most of the rest are GPIO pins your code can control.
Every pin has two numbers. Its physical number counts along the header, and its GPIO number is the one your code uses.
3D example: the header pin by pin, with physical and GPIO numbers in the captions.
Practice: Find the pin
Tap the right pin on a 2D pinout chart, by GPIO number, physical number or job.
3.3 volt logic
GPIO pins work at 3.3 volts. An output pin set high gives 3.3 V, and set low gives 0 V.
An input pin reads high above 2.0 V and low below 0.8 V. In between, the reading is not reliable.
Never connect 5 V to a GPIO pin. The pins are not built for it, and it can damage the Pi for good.
Example: a voltage into an input pin, read as low, unsure, high or damaging.
Practice: Safe or not?
Sort wiring ideas into safe and damaging (SortGroups).
Blinking an LED
Wire an LED and a resistor in a loop from a GPIO pin to ground. The resistor keeps the current small enough for the LED and the pin.
The gpiozero library comes with Raspberry Pi OS. LED(17) controls an LED on GPIO17, and led.on() sets that pin to 3.3 V.
Example: blink code runs line by line while the LED circuit lights in step.
A red LED uses about 2 V of the 3.3 V, and the resistor drops the rest. A GPIO pin can safely supply up to 16 mA.
Practice: Work out the LED current
Type the current through an LED and resistor on a 3.3 V pin, in mA.
Practice: Complete the blink program
Fill the gaps in a gpiozero blink program (FillBlanks).
Reading a button
A button joins a GPIO pin to ground while you press it. When it is not pressed, the pin is joined to nothing, so its reading drifts.
A pull-up resistor fixes this by gently pulling the pin up to 3.3 V. The pin reads high until the button pulls it down to 0 V.
Button(4) turns on the pull-up inside the Pi for GPIO4. Then button.is_pressed is True whenever the pin reads low.
Example: a button with a pull-up resistor, pressed and released, with the pin voltage and reading.
Buttons that control LEDs
gpiozero can run a function the moment something happens. button.when_pressed = led.on says what to do on every press.
There are no brackets after led.on. You hand the function over to run later, instead of running it now.
Example: press the button and the LED follows, with the matching line of code lit.
pause() keeps the program waiting for presses. Press Ctrl+C in the terminal to stop it.
Practice: Predict what happens
Predict pin readings and what button code does (ChoiceQuiz).
Sensors: I2C and SPI
Simple sensors just switch a pin high or low, like a button. Smarter ones send numbers over a bus, a set of wires many devices share.
I2C uses two wires: SDA carries the data and SCL keeps time. Every device has its own address, and only the one called answers.
SPI uses more wires and runs faster. Instead of addresses, the Pi picks a device with that device's own chip select wire.
Example: the Pi calls each I2C address in turn, then selects SPI devices with chip select wires.
Practice: I2C or SPI?
Sort facts and devices into I2C and SPI (SortGroups).
Pins with special jobs
Any GPIO pin can switch something on or read a button. Some pins also have a special job built into the chip.
GPIO2 and 3 carry I2C, and GPIO7 to 11 carry SPI. GPIO14 and 15 are the UART, a serial link that sends data one bit after another.
GPIO12, 13, 18 and 19 make PWM, a fast on-off pulse that sets a servo's angle or an LED's brightness. Pins 27 and 28 are kept for HAT boards.
Example: the pinout lights up power, plain GPIO, I2C, SPI, UART, PWM and the ID pins in turn, saying what plugs in.
Kinds of devices
A one-pin device such as an LED, a button or a buzzer needs one GPIO pin and a ground. An LED also needs its resistor.
Bus devices share wires. An I2C sensor needs power, ground, SDA and SCL, and a UART module such as a GPS crosses its TX and RX.
A servo needs a PWM signal, 5 V and a shared ground. Motors and relays need a driver board and their own supply, never a bare pin.
Example: LED, I2C sensor, SPI display, GPS, servo and motor driver, each wired to the header in turn.
HATs: boards on the whole header
A HAT is a board the size of the Pi that sits on all 40 pins at once. A pHAT is a smaller one, about the size of a Pi Zero.
A HAT carries a small memory chip, its ID EEPROM, on pins 27 and 28. The Pi reads it at start-up to learn what the HAT is and how to set it up.
A HAT covers every pin but uses only a few. A stacking header has extra-long pins that poke through, so another board or wires can reach the rest.
Example: three HATs over the header, the pins each one uses lit against the 40 it covers, and a stacking header from the side.
Reading a device's labels
Every module has its pins labelled. VCC or VIN is power, GND is ground, and labels like SDA, SCL, SIG or DATA are signals.
Check the voltage before wiring anything. A 3.3 V part takes a 3V3 pin, and a 5 V part must only send 3.3 V back to a GPIO pin.
Then match each label to a header pin, one wire at a time. Signals go to the bus pins, or to the GPIO pin your code names.
Example: a BME280 and a PIR sensor, their labels read, their voltage checked and each label wired to a pin step by step.
Practice: Wire it up
Wire an LED, a button, an I2C sensor or a servo to the header with soft wires, by drag or by tap.
The camera
A camera joins the Pi with a flat ribbon cable. The Pi 5 has smaller sockets than older models, so it needs a Pi 5 camera cable.
The Camera Module 3 takes 12 megapixel photos and focuses by itself. From the terminal, rpicam-still -o photo.jpg takes a photo.
In Python, the Picamera2 library does the same job. A program can take a photo when a button is pressed.
Example: each button press makes Picamera2 save the next numbered photo.
Project ideas
Most Pi projects follow one pattern: an input, a program that decides, and an output. A sensor reads the world, Python makes a choice, and something happens.
Start small and get one part working before adding the next. Each project below reuses skills from this course.
Example: four projects shown as input, decision and output.
Practice: Plan a project
Pick every part a project brief needs (ChoiceQuiz, several answers).
Practice: Final review
Mixed review questions from the whole course (ChoiceQuiz).