
In this tutorial we’ll start using the GPIO (general-purpose input/output) pins of the Raspberry Pi. We’ll use Python to write the code to let blink an LED. Once this tutorial completed, you will have had a short introduction of how the GPIO pins of your Raspberry Pi can be used to interact with an external electronic component.
- Prepare your Raspberry Pi
First, you need to have a Raspberry Pi 3 or 4 running on the latest version of Raspberry Pi OS. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. If you’re not familiar with Python or with Thonny, I suggest to have a look at our tutorial “How to write your first Python program on the Raspberry Pi” to have a quick introduction.
- Prepare the extra hardware
Next you’ll need some extra hardware:
– a breadboard (we are using a 400 points breadboard)
– an LED (we are using a red 5mm LED)
– a 100 Ohm resistor (a little higher or lower resistance value won’t be a problem)
Then, you have 2 options, first the easiest one (with each pin clearly labelled and easily accessible):
– a T-cobbler
– a 40 pin GPIO cableOr, the second option (without having the pins clearly labelled and accessible):
– 2 female to male jumper cables
If you miss any hardware, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start. - Get to know the GPIO pins
It is through the General Purpose Input Output (GPIO) that the Raspberry Pi is able to interact with external electronic components. The Raspberry Pi 3 and Raspberry Pi 4 are equipped with 40 GPIO pins. Many pins have specific features, explaining them all will be out of scope of this tutorial. If you’re not familiar with it, just remember you can program some pins as input or output. And when these pins are
high
orTrue
, there is 3.3 Volts on it, if the pins arelow
orFalse
, there is no tension on it.
All the pins are well labelled. If you bought one of our kits, you can see the numbering on the provided Raspberry Pi GPIO pinout card.Be careful ! Before starting to connect wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable from the board!
- Setting up the hardware part by using a 40 pin GPIO cable and a T-cobbler (1st option = our preference)
– connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
– plug the T-cobbler onto the breadboard as shown in the figure above or below
– plug the other end of the 40 pin cable in the T-cobbler
– connect the longer end (+) of the LED to GPIO 21
– connect the shorter end (-) of the LED on a free row of your breadboard
– connect one end of the resistor on the same row
– connect the other end of the resistor to a GND (ground) pin - Setting up the hardware part by using jumper cables (2nd option)
– connect a jumper cable from GPIO21 to a free row of your breadboard
– connect the longer end (+) of the LED in the same row
– connect the shorter end (-) of the LED to another free row of your breadboard
– connect one end of the resistor on the same row
– connect the other end of the resistor to another free row of your breadboard
– connect a jumper cable from this same row to a GND (ground) pin of your Raspberry Pi - Write the code
The aim is to write a very basic program to let blink our LED until we stop running the program. To write the Python code we’ll use the Thonny IDE. You can find Thonny in the applications menu of your Raspberry Pi.
Write or paste following code in the IDE:import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LED = 21
ledState = False
GPIO.setup(LED, GPIO.OUT)
while True:
ledState = not ledState
GPIO.output(LED, ledState)
time.sleep(0.5)
Some explanations about the code:GPIO.setmode(GPIO.BCM)
: The GPIO.BCM option means that we are referring to the pins by the “Broadcom SOC channel” number, these are the numbers after “GPIO”GPIO.setwarnings(False)
: We use this line of code to avoid warning messages because we don’t end the GPIO connection properly while interrupting the programGPIO.setup(LED, GPIO.OUT)
: We define the LED pin (=21) as an output pinwhile True:
is an infinitive loop (until we stop the program)
Be careful, Python is whitespace-sensitive. Don’t remove the “tab” before the next lines of codeGPIO.output(LED, ledState)
: Assign the value of the variable “ledState” to the pin. True = 3.3V and False = 0Vtime.sleep(0.5)
: wait for 0.5 seconds - Run the Python script
Before running the program you’ll have to give it a name and save it. Then, when you click on the Run button, the LED should blink. To stop the LED blinking, just click on the STOP button.
Congratulations! You just made your first step in a wonderful world of Physical Computing where computers interact with objects of the physical world. Lots of fun with your next project!