Posted on Leave a comment

How to use the Raspberry Pi GPIO pins

raspberry pi GPIO pinout LED

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.

  1. 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.

  2. 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 cable

    raspberry pi GPIO cable T-cobblerOr, 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. Raspberry Pi GPIO kit

  3. 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 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 Raspberry Pi GPIO pinout card. Raspberry Pi GPIO header pinoutBe 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!

  4. Setting up the hardware part by using a 40 pin GPIO cable and a T-cobbler (1st option = our preference)


    raspberry pi LED– 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

    raspberry pi GPIO LED

  5. Setting up the hardware part by using jumper cables (2nd option)


    raspberry pi LED jumpers
    – 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

  6. 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 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

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

    Raspberry Pi Thonny Python LED blink

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!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter

freva.com respects your privacy. Read our privacy policy on how we handle your personal information.