Maker Portal

View Original

An Introduction to the Raspberry Pi Pico with MicroPython

“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”

See this content in the original post

See this content in the original post

A Raspberry Pi Pico and Raspberry Pi 4 computer are the minimum required components for testing the Pico. The Pico has an onboard LED that can be controlled and used to test the basic functionality of the microcontroller. However, we want to test the capabilities on an external piece of hardware. An RGB LED will be used to test the general purpose inputs/outputs (GPIOs) of the Pico along with pulse-width modulation (PWM) for adjusting the brightness of the LED(s). A half breadboard (mini breadboards are too small) will also be used for our testing of the RGB LED. The full parts list is given below to follow along with the tutorial:

  • 1x Raspberry Pi Pico Starter Kit (Breadboard, RGB LED, USB Cable)- $15.00 [Our Store]

  • 1x Raspberry Pi 4 Computer - $51.99 (2GB), $61.89 (4GB), $89.99 (8GB) [Amazon], $55.00 [2GB from Our Store]

  • 1x RGB LED - $3.00 [Our Store]

  • 4x Jumper Wires (Male-to-Male) - $0.60 [Our Store]

  • 1x Half Breadboard (400 ties) - $6.00 [Our Store]

See this product in the original post

Note that the Raspberry Pi Pico Starter Kit, in conjunction with a Raspberry Pi 4 computer, are the only required parts for this tutorial!

The pinout diagram for the Raspberry Pi Pico is given below for reference:

The pinout diagram will be essential for testing the Pico and determining which GPIO pins will be used to control the RGB LED.

See this content in the original post

See this content in the original post

The RP2040 microcontroller is at the center of the Raspberry Pi Pico. The RP2040 has a native MicroPython port and a USB Flashing Format (UF2) bootloader in its memory, meaning that the Pico can directly handle Python code uploaded via an IDE. Thonny will be used as the IDE on the Raspberry Pi 4 computer, which is already installed on newer Raspberry Pi OS distributions.

The MicroPython ‘Getting Started’ guide on the Raspberry Pi Pico webpage is used as the template for much of the MicroPython and Pico startup that follows. The following screenshots summarize the process required for preparing the Pico for MicroPython programming with Thonny:

The Pico should now be shown as a mass storage device on the Raspberry Pi 4 desktop. The name of the device should be RPI-RP2, which represents the RP2040 microcontroller.

Next, drag and drop the downloaded UF2 file containing the MicroPython distribution onto be onto the RPI-RP2 device:

At this point, the Pico is ready to function as a MicroPython-enabled device. The device is now acting as a microcontroller ready to be programmed using Python.

We can now check that Python is working on the Pico by testing its command line interface with minicom. First, make sure ‘minicom’ is installed on the RPi via the terminal:

See this content in the original post

Once minicom is installed, verify the location of the Pico microcontroller using the following command in the terminal:

See this content in the original post

The Pico can be found by unplugging and running the command above, and then plugging in the Pico and running the command again and looking for the changed port. The Pico is most likely on port /dev/ttyACM1, if no other devices are connected. The Pico port will appear as shown below:

Now we can verify the communication between the Raspberry Pi and Pico microcontroller using minicom commands to gain access to the Python terminal:

See this content in the original post

The resulting minicom menu should appear:

The first real test we can do at this point is turn on the onboard LED via a series of commands:

The three lines of code are:

See this content in the original post

Similarly, if the led variable is then set to led.low() - the onboard LED will be turned off.


See this content in the original post

Now that MicroPython is working on the Pico, we can move on to more robust programming with Python on the Pico. The way we do this is by working with the Thonny IDE on the Raspberry Pi. Most distributions of Raspberry Pi OS have an installation of Thonny included in the first boot of the OS. However, if a headless or lite version is being used, Thonny will need to be installed. This can be done via ‘sudo pip3 install thonny.’ Going forward, it is assumed that Thonny is already installed on the Raspberry Pi. First, open Thonny and look for the ‘Tools’ menu at the toolbar:

If the Thonny IDE looks similar to the one above, click ‘Switch to regular mode’ and then exit and reopen the IDE.

Next, we will tell Thonny to use the Raspberry Pi Pico as the Interpreter, which can be done as follows:

Now, the Thonny shell can be used in a similar manner as the minicom interface. Returning to the three lines of code that control the Pico’s onboard LED, we can test the MicroPython interpreter on the Pico:

The advantage of using Thonny is that it allows for scripting and live software debugging. A script can also be uploaded to the Pico under the name ‘main.py’ and it will run in an infinite loop similar to the loop() running indefinitely on an Arduino microcontroller. In the next section, a script will be developed that controls the red, green, and blue components of an RGB LED using the general purpose inputs/outputs (GPIOs) on the Pico.


See this content in the original post

The general purpose input/output (GPIO) pins on the Pico microcontroller will be tested by developing a script that adjusts the brightness of the components on an RGB LED using two types of control: boolean and pulse-width modulation (PWM). The boolean control turns each LED on and off with a high and low designation. The PWM control uses a duty cycle modulation to control the brightness of the LEDs.


RGB LED to Raspberry Pi Pico Wiring Diagram

FS Tech can help makers with its turnkey PCB assembly expertise, from design to packaging. Choosing a turnkey PCB assembly company can make your project easier, sleeker, and save you work in your future projects.

The script below is used to turn each LED on and then off in an infinite loop:

See this content in the original post

The code above should be inputted into the <untitled> script portion of the Thonny IDE.


See this content in the original post

A video demonstration of the RGB LED looping is shown below:

See this content in the original post

Next, we can test the pulse-width modulation (PWM) functions of the Pico by adjusting the brightness of each LED on the RGB LED. Specifically, the LED can be made to appear as ‘breathing’ by increasing and decreasing its brightness. The code to do this is given below:

See this content in the original post

Similar to the code above, be sure to select F5 or Run and save the file as the new ‘main.py’ file on the Pico. This will again cause the script to run indefinitely, resulting in an endless ‘breathing’ appearance form the RGB LED.

See this content in the original post

The frequency of the PWM pulse was set to 1000Hz (1kHz), which is similar to some Arduino boards. 500Hz can also be used as other Arduino boards/pins use that frequency as well. Similarly, the delay can be changed to alter the effect or speed of breathing.


See this content in the original post

The Raspberry Pi Pico was introduced as a new MicroPython-enabled microcontroller produced by the Raspberry Pi Foundation. It is a direct rival in some respects to competitive open-source and ‘maker’ electronics companies like Arduino, Espressif, PIC, Teensy, and others. Using only a Raspberry Pi 4, the Pico microcontroller can be programmed to run MicroPython and harness its numerous peripherals: I2C, SPI, UART, PWM, analog-to-digital conversion, etc. In this tutorial, the Raspberry Pi Pico was explored using Thonny, a Python IDE already installed on the general distribution of the Raspberry Pi operating system (Raspberry Pi OS). The basics of getting started with MicroPython and the Pico microcontroller were first introduced, followed by some simple hardware tests with general purpose inputs and outputs (GPIOs) and an RGB LED. This was meant as a simplified ‘getting started’ tutorial with the Pico, and the first of a series dedicated to working with the Raspberry Pi Pico microcontroller.

See this content in the original post

See More in Raspberry Pi and Microcontrollers:

See this content in the original post