Blink in MicroPython
In this lab, we will use MicroPython to make the green on-board LED on the Raspberry Pi Pico blink on and off every half second. The only things you need to run this program are
- a working IDE like Thonny
- a USB cable
- a $4 Raspberry Pi Pico microcontroller
Blinking the Builtin LED
The pico has a single built in green LED wired to logical pin 25. We call this GPIO 25. GPIO means General Purpose Input and Output pin. Here is a sample program that you can use. Don't worry about understanding each line yet. We will cover the various parts of the file in later sections.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
This program has two main parts. The first part is often called the preamble or the setup code. This code gets executed only once when the program is run. In this example, the setup loads the right libraries and initializes global variables.
The second part is the main event loop. This program continues to run until the device is powered down or the program is reset.
from machine import Pin statement is required to define the characteristics of our physical machine. In this case, we are loading the Pin python library.
from time import sleep library is required for the python sleep function. Note that some programs use
utime for MicroPython time. These both work the same. They are really just synonyms or alias of each other. Although the use of
utime is technically a little bit more precise - the reader knows that the code is using the actual MicroPython time library, the use of the
time alias makes our code on character smaller and can make our code more portable to other systems.
Note that the text after the hash or pound characters are comments. Comments are ignored by the Python interpreter but it is a good practice to put in comments in your code to help others understand your program.
Changing the Blink Speed
Next, lets create a Python global variable for the delay that the LED is on and off.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This program will blink the built-in LED on and off every 1/4 of a second. By changing the delay variable you can make the LED blink faster and slower.
What is the fastest you can make the LED blink and still see it changing? What does this tell you about the human eye?
Instead of using the
off() methods, we can also just use the
1 2 3 4 5 6
toggle() function looks at the state of the output pin. If it is high, it sets the value low. If it is low, it sets it high. Whatever the value currently is, it will set it to the opposite value.
If you save the file as main.py, this program will run when the microcontroller starts up without the BOOTSEL being pressed.
Blinking an External LED
Although the builtin LED is convenient, you can use the almost the code to blink any external LED that is connected through a 330 ohm resister in series to the LED.
We will assume that an LED is connected to pin GIO16 and is connected via a 330 ohm resistor to ground.
Here is the code that will blink an LED that is connected to PIN GIO16, which is in the upper right corner of the Pico.
1 2 3 4 5 6 7 8 9 10 11 12
Here is that same program using the
1 2 3 4 5 6 7 8
Solution to LED Row Lab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
Extra credit lab
Can you rewrite the program above using an array of pin values like this:
More to Explore
- Can you blink both the internal on board LED and an external LED?
- How many external LEDs can you blink? What is the maximum number of output pins on the Raspberry Pi Pico?
- What if you wanted to control more LEDs then you have available output pins? What do you think your options might be? (answer - use a NeoPixel or a shift register like a SN74HC595). See the peppe8o Shift Register Tutorial on the RP2040 for an example of this.