Python and Raspberry Pi go hand in hand. In the early days of the Pi, there was one Python library that dominated the maker and educational scene: RPi.GPIO. It was quickly adopted by Raspberry Pi as the de facto standard means to interact with the GPIO via Python. In this tutorial, we’ll go back to the early days to learn how this communitycreated project can be used with LEDs and buttons. We’ll also talk a little about how RPi.GPIO came to be.
Project 1: Blinking LEDs
The Hello World of hardware hacking is to blink an LED and we continue this great tradition now. This project is useful to debug hardware and software before moving on to more difficult projects. Our goal is to blink an LED 10 times, and with each change of state (LED on, LED off), we print the state to the Python shell. Doing so, we confirm the code is working as it prints the message. If the LED fails to light, we have a hardware issue.
From the main Raspberry Pi menu, go to Programming and select Thonny. We start the code by importing RPi.GPIO and renaming it to GPIO:
import RPi.GPIO as GPIO
Next use the sleep function from the time module. We use this to control how long the LED is on and off:
from time import sleep
Unlike GPIO Zero, RPi.GPIO requires us to specify the GPIO’s pin mapping. The Raspberry Pi Foundation specifies that Broadcom (BCM) mapping is the standard. This makes for a little confusion as the pins are mapped to a non-sequential series of pins. The alternative board uses physical pin mapping. Pin 1 being the top-left pin nearest the microSD slot. Pin 2 is on the next column of pins. We’ll use BCM mapping:
GPIO.setmode(GPIO.BCM)
This story is from the April 2023 edition of Linux Format.
Start your 7-day Magzter GOLD free trial to access thousands of curated premium stories, and 9,000+ magazines and newspapers.
Already a subscriber ? Sign In
This story is from the April 2023 edition of Linux Format.
Start your 7-day Magzter GOLD free trial to access thousands of curated premium stories, and 9,000+ magazines and newspapers.
Already a subscriber? Sign In
Create your first WebSocket service
Mihalis Tsoukalos explains how to use the Go programming language to work with the WebSocket protocol.
Fantastic Mr Firefox
Nick Peers takes a trip down memory lane to reveal the story behind the rise - and slight fall - of Mozilla's popular web browser.
Set up your terminal and email like it's 1983
Jump in the hot terminal time machine with Mats Tage Axelsson who emails from the command line using the latest technology.
Universal layer text effects with GIMP
Posters use them, films and presentations are hard to imagine without them: text effects. Attract attention with Karsten Günther and GIMP.
Jump to a federated social network
Nick Peers reveals how you can get up and running with this free, decentralised and non-profit alternative to Twitter.
Free our SOFTWARE!
Taking anything for granted is dangerous, so Jonni Bidwell and Mike Saunders revisit how the free software movement got started to help free us from proprietary tyranny!
Master RPI.GPIO
Les Pounder goes back to the early days of the Raspberry Pi - and his career with this classic library! -
Waveshare Zero to Pi3
Transform your Pi Zero into a Pi 3, they promised Les Pounder, but it's more like adding on go-faster stripes.
The Best OPEN SOURCE Software Ever!
In an attempt to trigger controversy, Michael Reed and Neil Mohr unequivocally state these are the greatest free software apps ever. Probably. We’re just trying to be helpful.
Linux-Mandrake 7
Simplicity and a wide range of applications make this a great distribution for all Linux users.