The Raspberry Pi Pico is an interesting board. The Pico is a low-cost Arm-based microcontroller that we can program using C/C++ and MicroPython, not another Linux single-board computer like every other Raspberry Pi. In this guide, we'll show you how to setup a Raspberry Pi Pico W and the original Raspberry Pi Pico. Take a look at our review if you're interested in learning more technical information about the board.
How to Setup the Raspberry Pi Pico / Pico W
- Download the MicroPython UF2 file for your Raspberry Pi Pico model.
- Use a micro USB cable to connect the Pico to your computer after pressing and holding the BOOTSEL button. After the drive RPI-RP2 shows up on your computer, release BOOTSEL.
- Drag and drop the UF2 file onto the RPI-RP2 disc. The Raspberry Pi will reboot and then run MicroPython.
Developed for microcontrollers, MicroPython is a version of Python 3. You can write MicroPython if you can write Python. We need a special editor to write MicroPython code, and Thonny is the default, simple editor that we will use in this tutorial.
- If you don't already have it, download and install Thonny for your operating system. On the Thonny website, you may grab it for free.
- Attach the Raspberry Pi Pico to your computer, then go to Tools > Options > Interpreter tab in Thonny. Select MicroPython from the interpreter drop-down menu (Raspberry Pi). The Pico may be detected automatically using the port drop-down selection. To close, click OK.
- We can write a quick print function to say “Hello World” as a test. To run the code, press Enter.
|Seeed Studio Raspberry Pi Pico Flexible Microcontroller Board Based on The Raspberry Pi RP2040…||$7.80||Buy on Amazon|
How to Blink an LED Light on Raspberry Pi Pico
We will write the “Hello World” equivalent for hardware projects, flashing an LED, to further test that we can successfully program the Raspberry Pi Pico. This quick test verifies that our hardware is functional and introduces the MicroPython language and syntax in its simplest form.
We must first wire up our test circuit before we can start writing any code. This will require header pins to be soldered to the Raspberry Pi Pico. You will need the following materials to complete this project:
- The Raspberry Pi Pico should be inserted into the breadboard such that it sits above the main channel. Make sure the Micro USB port is located at the end of the breadboard.
- Put a 330-Ohm resistor on the breadboard; one leg should be in line with GND, which is pin 38. The second leg should be inserted into the rail of the breadboard. This gives us a GND rail with all of its pins wired to GND.
- Put an LED with the long leg (the anode) inserted into the breadboard at pin 34 and the short leg inserted into the GND rail. The circuit is now constructed.
We can now start writing the code to blink the LED now that the circuit has been constructed.
- Import the necessary libraries. Our code is written in the wide blank space above the REPL, and we start by importing two MicroPython libraries. The first is the Machine library's Pin class, and the second is the time variable, which regulates the pace of our code.
from machine import Pin import utime
- Create an object, “led” which is used to create a connection between our code and the physical GPIO pin. In this instance, it will set GPIO 28 (which corresponds to physical pin 34 on the board) as an output pin, via which current will pass from the Raspberry Pi Pico GPIO to the LED. The GPIO pin is then instructed to draw low using the object. In other words, this will make sure that the GPIO pin is turned off at the start of our project.
led = Pin(28, Pin.OUT) led.low()
- We toggle the LED on and off within a while true loop, a loop without an end, and print a message to the Python Shell (REPL) to prove that the loop is functional. Finally, we include a sleep statement to stop the code for one second between each iteration of the loop.
while True: led.toggle() print("Toggle") utime.sleep(1)
- Select to save the code to the MicroPython device by clicking Save (Raspberry Pi Pico). Click OK to save the file with the name blink.py. Your code ought to look like this.
from machine import Pin import utime led = Pin(28, Pin.OUT) led.low() while True: led.toggle() print("Toggle") utime.sleep(1)
- The Python Shell will update to say TOGGLE every second, and the LED will flash on and off when you click the green play/arrow button to run the code.
We successfully tested our Raspberry Pi Pico, and now we can move on to another project. For instance, learning how to use sensors with the Raspberry Pi