top of page

Raspberry Pi Pico Onboard LED Control

Introduction

In our previous lesson, we learned how to install Thonny IDE and how to add MicroPython firmware to our Pico. Now, let's test if our newly installed Micropython will work fine with our Pico by attempting to control the LED on the Pico Board with the help of the 'picozero' library.

​

​

Things we'll need

  • Raspberry Pi Pico. 

  • Compatible USB cable.

  • Internet connection.  

​

​

Lesson goal

Our goal for this lesson is to achieve the following:

  • Download 'Picozero' library.

  • Toggle on/off Pico onboard LED.

  • Blink onboard LED.

​

​

​

Picozero library

​

For newbies in the house, a library in programming is like a toolbox full of ready-made tools that programmers can use to build software more easily. Instead of inventing new tools for every project, programmers can use libraries to quickly add common features or solve common problems without starting from scratch.

​

Picozero is a beginner-friendly Micropython library to help you use common electronics components with the Raspberry Pi Pico. With picozero, we can easily interact with the General Input Output (GPIO) pins of the pico with very few lines of code.

​

​

Picozero library download

​

The picozero library does not come preinstalled with the Micropython firmware, so we have to install it ourselves using Thonny IDE with the procedures below:

​

1. Open your Thonny IDE, and click on 'Tools >> Manage packages...'. A new window should pop up.

thonny-manage-packages.jpg

2. In the pop-up window, type 'picozero' in the search bar and click ‘Search on PyPi’.

thonny-install-package.jpg

3. Click on ‘picozero’ in the search results then click on ‘Install.

​

When installation has been completed, close the package window then exit and reopen Thonny.

thonny-packages-picozero.jpg

Controlling Pico onboard LED

​

Now that we've successfully installed the picozero library, let's blink some lights!! Follow the steps below:

​

1. Connect the Raspberry Pi pico board to your PC via a USB cable.

2. Launch the Thonny IDE and copy and paste the code below:

​

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

​

from picozero import pico_led

pico_led.on()   

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

​

​

In the first line, we imported the picozero library and then selected the function we needed which is the pico_led to help us control the onboard LED.

​

In the second line, we instruct the code to turn on the LED using the pico_led.on().

​

​

Save code file:

​

Choose ‘File > Save As’. Thonny will ask whether you want to save the file on This computer or the Raspberry Pi Pico. Choose This computer to save your code to your computer.

​

Choose a location on your computer such as your ‘Documents’ folder. Name your file ‘pico_led.py’.

save-on-computer.png

Test:

 

Thonny has a green play button with a small white triangle inside it. Pressing this button allows you to run your code.

  • Press the play button.

  • Check that the small LED on the Raspberry Pi Pico turns on.

​

Replace 'pico_led.on()' with 'pico_led.off()' and then run the code again to turn off the onboard LED.

led-on.jpeg

Blink onboard LED

​

You can use the function below to make the LED turn on and off in a loop:

​

pico_led.blink()

​

​

If your onboard LED is responding fine, Congratulations!! you've completed your first Raspberry Pi pico project.

Picozero library
bottom of page