All paths

Pi hardware in Python: I2C, SPI and PWM

The Raspberry Pi’s hardware from Python: gpiozero beyond blinking, I2C sensors with smbus2, an SPI ADC with spidev, a servo gauge needle, your own OLED driver and callbacks, ending in a Pi weather terminal.

Raspberry Pi 4 Model BFree7 lessons6 h4 chapters2,446 XPEmbedded engineering · Intermediate+150 XP

0 of 7 done

What you will build

3 projects, built step by step

7 lessons in 4 chapters, about 6 hours on a simulated Raspberry Pi 4 Model B. Free, in your browser, every step checked by the simulator.

Chapter 1 project

smbus2 sensors

Talk to an I2C chip yourself: scan the bus as i2cdetect does, read one register and a block of them from a BME280, let a driver do the datasheet’s math, and read the error when nobody answers.

Open it in the simulator
Chapter 2 project

PWM servos: a gauge needle

A servo as a barometer’s needle: a 50 Hz pulse from 1 ms to 2 ms sets its angle, a duty cycle from 5 to 10 percent with RPi.GPIO PWM, and air pressure mapped onto 180 degrees.

Open it in the simulator
Chapter 3 project

Callbacks

Let the button call your code: when_pressed, when_released and when_held with hold_time, a counter shared with the callbacks, and a main loop that stays free the whole time.

Open it in the simulator
The map

Chapter by chapter

Each lesson opens when the one before it is done, and each chapter ends in a project.

Chapter 1

Devices and I2C

gpiozero’s devices, and a sensor register by register.

  1. Open249 XP

    gpiozero beyond basics

    Devices with a value, not just on and off: PWMLED brightness, blink a set number of times, a pulse that runs in the background, and devices wired to each other with source.

    0 of 7 steps

  2. ProjectLocked498 XP

    smbus2 sensors

    Talk to an I2C chip yourself: scan the bus as i2cdetect does, read one register and a block of them from a BME280, let a driver do the datasheet’s math, and read the error when nobody answers.

    0 of 7 steps

4 questions, graded as you go.
Chapter 2

SPI and PWM

An ADC over SPI, and a servo needle on PWM.

  1. Locked187 XP

    spidev ADC

    The Pi has no analog inputs, so an MCP3008 converts for it over SPI. Send the three bytes yourself with spidev and decode the 10-bit answer, then let gpiozero’s MCP3008 do the same in one line.

    0 of 5 steps

  2. ProjectLocked498 XP

    PWM servos: a gauge needle

    A servo as a barometer’s needle: a 50 Hz pulse from 1 ms to 2 ms sets its angle, a duty cycle from 5 to 10 percent with RPi.GPIO PWM, and air pressure mapped onto 180 degrees.

    0 of 7 steps

4 questions, graded as you go.
Chapter 3

Drivers and events

Your own OLED driver, and code the hardware calls.

  1. Locked218 XP

    An OLED driver in Python

    Write the driver yourself: the SSD1306’s setup commands, a 1024-byte framebuffer where each byte is eight stacked pixels, pixel() with bit arithmetic, and show() in 16-byte I2C writes. Then a bar graph of the knob.

    0 of 6 steps

  2. ProjectLocked498 XP

    Callbacks

    Let the button call your code: when_pressed, when_released and when_held with hold_time, a counter shared with the callbacks, and a main loop that stays free the whole time.

    0 of 7 steps

4 questions, graded as you go.
Chapter 4

Boss project

A Pi weather terminal from the whole bench.

  1. Locked298 XP

    Boss: a Pi weather terminal

    The whole bench as one device: BME280 readings on the OLED, a knob that turns the pages, a servo pressure needle, a damp-air light, and a button that logs readings to a CSV file.

    0 of 8 steps

3 questions, graded as you go.
Syllabus

Every lesson in the course

All 7 lessons, in order, each with what it covers. Any of them opens in the editor right away.

Chapter 1

Devices and I2C

  1. gpiozero beyond basicsDevices with a value, not just on and off: PWMLED brightness, blink a set number of times, a pulse that runs in the background, and devices wired to each other with source.
  2. smbus2 sensorsTalk to an I2C chip yourself: scan the bus as i2cdetect does, read one register and a block of them from a BME280, let a driver do the datasheet’s math, and read the error when nobody answers.
Chapter 2

SPI and PWM

  1. spidev ADCThe Pi has no analog inputs, so an MCP3008 converts for it over SPI. Send the three bytes yourself with spidev and decode the 10-bit answer, then let gpiozero’s MCP3008 do the same in one line.
  2. PWM servos: a gauge needleA servo as a barometer’s needle: a 50 Hz pulse from 1 ms to 2 ms sets its angle, a duty cycle from 5 to 10 percent with RPi.GPIO PWM, and air pressure mapped onto 180 degrees.
Chapter 3

Drivers and events

  1. An OLED driver in PythonWrite the driver yourself: the SSD1306’s setup commands, a 1024-byte framebuffer where each byte is eight stacked pixels, pixel() with bit arithmetic, and show() in 16-byte I2C writes. Then a bar graph of the knob.
  2. CallbacksLet the button call your code: when_pressed, when_released and when_held with hold_time, a counter shared with the callbacks, and a main loop that stays free the whole time.
Chapter 4

Boss project

  1. Boss: a Pi weather terminalThe whole bench as one device: BME280 readings on the OLED, a knob that turns the pages, a servo pressure needle, a damp-air light, and a button that logs readings to a CSV file.
Kits

Build it on your bench too

Every lesson runs in the simulator with nothing to buy. When you want the real thing, these kits have the parts.

Have a kit already?

Find your kit

Pick the starter kit you own and run every lesson in its manual with the same parts on the same pins.

See every kit
Questions

About Pi hardware in Python: I2C, SPI and PWM

Is Pi hardware in Python: I2C, SPI and PWM free?

Yes. All 7 lessons of Pi hardware in Python: I2C, SPI and PWM are free and run in your browser, with nothing to install and no parts to buy before you start.

Do I need a real Raspberry Pi 4 Model B?

No. Every lesson runs on a simulated Raspberry Pi 4 Model B with its parts on the canvas, and the simulator checks each step as you go. When you want to build it for real, the same wiring and code work on a real board.

How long does Pi hardware in Python: I2C, SPI and PWM take?

About 6 hours: 7 lessons in 4 chapters, each one a few short steps. Your progress is kept, so you can stop after any lesson and pick up where you left off.

What will I build?

Each chapter ends in a project that puts it together: smbus2 sensors, PWM servos: a gauge needle and Callbacks.

Where do I start?

With "gpiozero beyond basics", and each lesson builds on the one before it. New to circuits? Electronics basics is the course that starts from zero.