Today we will look at the new Raspberry Pi Pico board. We will setup the programming environment and write our first couple applications using MicroPython. Now the Pico is a whole new class of computing device from Raspberry Pi. Instead of a full fledge computer, we have a ultra low cost microcontroller development board running a custom Arm Cortex M0+ chip called the RP2040. This board is more in line with the likes of Arduino , STM32 Nucleo, and the ESP32 then it is with it’s name sake Raspberry Pi.
So why should we care about yet another development board. At the end of the day it really comes down to usability and cost. At a whopping $4 dollar cost point, with banner specs like 133 MHz dual core processor, 264kB of RAM, 2 MB of on-board flash memory, and all the usual suspects for peripherals GPIO, SPI, I2C, 12-bit ADC, PWM, and list goes on…
This development board packs a huge punch for little investment. Heck, a proper breadboard will cost more than this development platform. Ultimately, you will have to decide if the Pico is right for your needs or project.
The parts you will need to follow along with this video are:
- 1x Raspberry Pi Pico Board
- 1x micro-USB cable
Just like any other microcontroller development board, we need to get a programming environment setup and running. You have several options for programming the Pi Pico, the two primary ways recommended on the Raspberry Pi website is either MicroPython or good old fashion C. There is also Beta support utilizing Adafruit’s CircuitPython and the Arduino team has announced they are porting the Arduino Core to the new architecture.
For me, I plan to start with MicroPython now and in the near future we may take a look at some of the other programming options.
For the development environment, I will be using the Thonny IDE as recommended by the Raspberry Pi Foundation. Much like the Arduino IDE, Thonny strikes me as just enough application to get the job done. It has a main toolbar, script area, a shell, and has the Pico interpreter support already built in. Best of all, it supports CPython and MicroPython for multiple board types, as well as generic support for CircuitPython. So, no matter what we want to do in the future we should be covered.
After downloading and installing the Thonny IDE, you need to set it up for use with the Pico board. In the bottom right-hand corner, you can left click on the Python Version and then select MicroPython (Raspberry Pi Pico).
The next thing we need to do is get MicroPython up and running on the board itself. To do that, you will press and hold the BOOTSEL button on the Pico board as you plug it into the computer, your computer will recognize the Pico as an external flash drive. Here you will see two files. One is the INFO_UF2.TXT file, which contains information about your Pico board and the other is an INDEX.HTM file that when executed will open the Raspberry Pi website. You can download the MicroPython firmware.uf2 file from here.
Flashing the firmware to the Pico is as simple as copying it from your computer to the Pico. After a few seconds the board will reboot and it will no longer be connected to computer as a mass storage device.
If you don’t want to download and transfer the uf2 firmware to the Pico board. Another option is to let the Thonny IDE install the MicroPython firmware for us. If you press and hold the BOOTSEL button while plugging the board into the computer and then open the Thonny IDE, you will see a prompt that provides instructions for installing the MicroPython Firmware. You will have already completed the first two steps at this point, so all you need to do is press the install button. The Thonny IDE will copy the required files over to the Pico and after pressing the close button, your Pico board will be seen by the IDE and can now be programmed with MicroPython.
We have two main ways of inputting a program into the Pico microcontroller. The first is through the Shell where you can input individual commands and press enter for immediate execution. This process is known as REPL or Read, Evaluate, Print, and Loop. The second method is by writing multiline programs in the script area and then running the code.
Lets first take a look at the Shell. If we type 5 + 5 in the shell and press enter, the value 10 is returned. It may not seem like it, but the script 5 + 5 is actually being handled by the interpreter on the Pico Microcontroller.
Next, lets see if we can get the on board LED to turn on.
In the Shell, first type “from machine import Pin” and press enter, this will import the Pin class from the Machine library. The Machine library is what does most of the heavy lifting when interfacing with hardware like the Pico.
Next type the function “Pin(25, Pin.OUT)” and give it two arguments, the first first is the GPIO pin number and the second is the mode we want to set. Here we are setting Pin 25 as an output because the onboard LED is physically connected to GPIO Pin 25.
Finally type “Pin(25).value(1) “and press enter, this will set the value of GPIO Pin 25 to HIGH, which turns on the LED.
Now the important thing to remember here is that all of the code executed through the shell will not persist through a power cycle. If we unplug our Pico and plug it right back into the computer, you will see that the LED is no longer on.
Next we will use the script area to write a multiline program that we can save to the computer or Pico. However, we need to restart our session with the Pico board first, this can be done by restarting the IDE or pressing the STOP button in the main tool bar to initiate a connection with the board.
Now lets write a program to blink the onboard LED on and off at a frequency of 500 milliseconds.
We first need to import the Machine and utime libraries.
Next lets define an object called LED and set it equal to machine.Pin(25, machine.Pin.OUT), this will set the mode of the GPIO pin 25 as an output.
Now we can set the value utilizing our object LED.value(1). The possible values in this case is 1 for ON and 0 for OFF.
In order for us to add a delay, we use utime.sleep function which as a single argument that corresponds to time in seconds. Here we will use 0.5 to give us a half second interval.
With the code written we can press the save button. Now we have to decide whether we want to save the program locally on the computer or on the Pico itself. For this example, I will save it locally, with the filename blinky.py.
All that is left to do is press the Run button and we can see our application running on the Pico, flashing the LED at the half second interval.
We still have the issue of the program not persisting through a power cycle, but at this point we have the code saved in a .py file and can open it back up on in the IDE or anther text editor.
To allow our program to continue to work on the Pico board without the computer we simply need to save our program with the file name “main.py” and save it to the Pico board. To get all that done, first make sure the blinky.py application is open in the IDE. Then select File>Save As, this time at the Where to save to? Prompt, select Raspberry Pi Pico and give it the name main.py. Finally, press the OK button. The next time we cycle the power to the board the LED will start flashing. The great news is we can now connect the board to an external power source, like a USB Power Bank, and the program will continue to run