Let’s see with this tutorial how to start programming Raspberry Pi Pico GPIO pins. We’ll use MicroPython to let blink an LED connected to the Raspberry Pi Pico pins.
Once this tutorial completed, you will have had a quick introduction of how to connect an LED to the the Raspberry Pi Pico and how to program the Pico to interact with the LED.
- Prepare the hardware
– First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.
– Next you need an USB cable with micro-USB plug.
– You also need a Raspberry Pi Pico of course. For this tutorial you need pin headers soldered to the GPIO-pins of your board.
And finally you’ll need some extra components :
– 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)
Visit our shop if you miss any components.
- Get to know the GPIO pins of the Raspberry Pi Pico
It is through the General Purpose Input Output (GPIO) pins that the Raspberry Pi Pico is able to interact with external electronic components. Many pins have specific features, explaining them all will be out of scope of this tutorial.
In contrary of the Raspberry Pi computer, the Raspberry Pi Pico has analog ports. Because an analog input or output is needed for a whole range of sensors, this is a very useful feature.
If you’re not familiar with it, just remember for the moment that you can program some pins as input or output. And when using them in the most simple mode (digital), these pins can be
True, with 3.3 Volts on it, or
False, with 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 Pico GPIO pinout card. Check the official Raspberry Pi documentation for more information regarding the pins.
Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.
- Setup the hardware part
– connect the longer end (+) of the LED to GP15 (=GPIO 15)
– 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
– to finish, connect the Pico to your computer through the micro-USB plug. Remember, if your Pico board has been used and firmware is present on it, it’s necessary to push and hold the white BOOTSEL button while you connect the board to your computer. Release the button a few seconds after both elements are connected.
- Write the code
The aim is here to write a very basic script to let blink the our LED.
Now, open Thonny and write or paste following code in the IDE:
from machine import Pin
led = Pin(15, Pin.OUT)
Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tab” before
Some explanations about the code :
from machine import Pin: to partially import the machine module to have access to the GPIO pins.
import time: to import the time module. This will allow us to use time-related tasks.
led = Pin(15, Pin.OUT): here we define the LED pin (=GP15) as an output pin
while True:is an infinitive loop (until we stop the program)
Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tab” before the next lines of code
led.toggle(): this alternates the output of the LED pin
time.sleep(1): wait for 1 second
- Run your script
Now, it’s time to save your script. You can either save it on your computer or on your Pico board.
Then, click on the Run button and the LED should start blinking. To stop the LED blinking, just click on the STOP button.
Have a look at our tutorial “How to start programming the Raspberry Pi Pico” if you want to learn how to let your Pico run the script without being attached to your computer.
Congratulations ! You just made your first project for the Raspberry Pi Pico while it’s connected to the external world. Have lots of fun with your next projects !
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?