Posted on Leave a comment

Raspberry Pi first program using GPIO

raspberry pi GPIO LED pinout

In this tutorial we’ll start using the GPIO (general-purpose input/output) pins of your Raspberry Pi. We’ll use Python to write the code for letting blink an LED. Once this tutorial completed, you will have had a short introduction of how the GPIO pins of your Pi can be used to interact with an external electronic component.

What you’ll need for this tutorial

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 “First Python program on the Raspberry Pi” to have a quick introduction.

Next you’ll need some extra hardware:

  • a breadboard (we are using a 400 points breadboard)
  • a 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 cable

Or, the second option (without having the pins clearly labelled and accessible):

  • 2 female to male jumper cables

Raspberry Pi GPIO discovery kit

If you miss any equipment, don’t hesitate to visit our shop. We have a nice kit which contains all the things you need tot start.

The GPIO pins

It is via the General Purpose Input Output (GPIO) that Raspberry Pi is able to interact with external electronic components. The Raspberry Pi 3 is 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 or True, there is 3.3 Volts on it, if the pins are low or False, 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 card.

raspberry pi GPIO header pinout
raspberry pi GPIO header pinout

Setting up the hardware part

Before starting connecting wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable of the board!

1st option: you are using a 40 pin cable and a T-cobbler

  • connect the 40 pin cable on the GPIO pins of your Pi (if necessary, remove the cover of your Pi first)
  • plug the 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

2nd option: you are using jumper cables

  • 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

Writing 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 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

LED = 21
ledState = False

while True:
    ledState = not ledState
    GPIO.output(LED, ledState)

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 program
  • GPIO.setup(LED, GPIO.OUT): We define the LED pin (=21) as an output pin
  • while 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 code
  • GPIO.output(LED, ledState): Assign the value of the variable “ledState” to the pin. True = 3.3V and False = 0V
  • time.sleep(0.5): wait for 0.5 seconds

When you finished to enter the code, 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 where computers interact on objects of the physical world. Lots of fun with your next project!

Leave a Reply

Your email address will not be published. Required fields are marked *

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter respects your privacy. Read our privacy policy on how we handle your personal information.