Installation of the IDE
After completing the download on Windows or macOS, users start the installation by executing the installation package. Linux users either use their respective package manager or other installation options (see Thonny on GitHub). The illustration for Windows PCs is an example. After starting the installation you get the following picture: The program can then be found on the target system. When starting the installed IDE, Thonny’s simple IDE window opens, which contains an editor in the upper half and a REPL shell (Read Eval Print Loop) in the lower half. The Shell is an ideal playground for interactive experimentation. At the bottom right of the window, Thonny shows the currently used Python interpreter in text form, in this case Python 3.7.9. However, only slimmed-down Python variants such as CircuitPython or MicroPython run on most embedded boards, whereby the Pico supports the latter by default. To get the suitable variant, all you have to do is click on the text field “Python 3.7.9”. Thonny then offers a selection menu with all supported interpreters, including “MicroPython (Raspberry Pi Pico)”: Before selecting MicroPython, developers should press and hold the Raspberry Pi Pico board BOATSELConnect the key to a USB input on the host computer. This means that the board identifies itself to the host as a storage device, such as a memory stick. As mentioned above, developers then select the option “MicroPython (Raspberry Pi Pico)” as the interpreter to be installed in Thonny, whereupon the following dialog appears: Users click the Install-Button, starts the installation of the MicroPython firmware. After completing the firmware installation, users only need to close the dialog box: The firmware consists of an executable program with the extension.UF2
. It can also be transferred manually to the Pico.
Ready for any outrage
The host and Pico are now connected to each other, and developers can either work directly with the MicroPython interpreter via the REPL shell or alternatively enter a program in the editor. It is important at this point that after transferring a MicroPython program to the board (ending:.py
) the board first shuts down and then starts up again, whereupon the runtime system automatically executes the Python program.
However, if there are several program files on the board, the Pico does not initially know which one to execute. In this case it looks for a file named main.py
to execute it or remain motionless if it cannot find this file.
Programming with the MicroPython SDK
Now it’s finally time to write a simple MicroPython program to test access to the board’s I / O system. As is traditionally customary, the first program should make the internal LED of the Pico flash before we get to know other possibilities of the Pico in the following episodes: Now let’s dissect the program step by step:from machine import Pin, Timer
The import
Statement uses from the machine
Library the components Pin
and Timer
. The former allows access to the I / O ports of the Pico, the latter provides functions for the timing of actions. In general contains – nomen est omen – machine
various components for access to the Pico hardware.
onboardLED = Pin(25, Pin.OUT)
The variables are now initialized onboardLED
. It should represent GPIO port 25, where the built-in LED is located. Important: The numbers do not refer to physical pin 25, but to GPIO port 25, for which there is not even a physical pin.
The second parameter in the constructor called Pin
tells the interpreter how the I / O port is to be used, namely as an output port in the present case. Hence the configuration with Pin.OUT.
timer = Timer()
Here the program initializes the variable timer
. More on that in a moment.
def blinker(timer):
onboardLED.toggle()
The method blinker
receives an initialized as argument timer
-Object. Whenever the timer
gives the initiative for this, the call takes place toggle
-Method. The sets the output depending on its current state either from 0
on 1
or from 1
on 0
which consequently causes the LED to flash.
timer.init(freq = 2.5, mode = Timer.PERIODIC, callback = blinker)
In the initialization method init
the program defines a periodically firing timer (Timer.PERIODIC
) with a frequency of 2.5. This gives a period of 400 milliseconds. timer
for this purpose calls a callback function every 400 milliseconds (callback = blinker
). In our case that is the blinker
-Method that inverts the output signal for the LED accordingly every 400 milliseconds.