Quick Guide to Rapid Prototyping Using MicroPython on the Raspberry Pi Pico

The ability to rapidly prototype a system, interface to an external device, or test architecture quickly and efficiently is critical for many engineering teams. Quite a few teams look to Arduino or their target hardware for rapid prototyping. However, a solution that I have found is to use MicroPython.

MicroPython is a port of CPython that runs on microcontroller-based systems. An interesting rapid prototyping solution is to use MicroPython with a low-cost SC0915 Raspberry Pi Pico board. In this blog, we will explore how to set up a Raspberry Pi Pico board with MicroPython and run applications on it using rshell.

Installing MicroPython on the Raspberry Pi Pico

The first step to rapid prototyping on the Raspberry Pi Pico is to install MicroPython. The procedure to do this is straightforward. First, download the MicroPython UF2 file. Next, hold down the BOOTSEL button on the Raspberry Pi Pico and then connect the board to a USB port on your computer (Figure 1). The Raspberry Pi Pico will enumerate over USB as an MSD class device with the name RPI-RP2. The last step is to copy and paste the MicroPython UF2 file onto the RPI-RP2 drive. Once this is done, the bootloader will program the Raspberry Pi Pico’s RP2040 microcontroller flash memory and start running MicroPython.

Figure 1: The Raspberry Pi Pico can be placed into a USB MSD bootloader mode by holding the BOOTSEL pin while power is applied to the board. (Image source: ElektorMagazine.com)

I would recommend waiting a minute or so and then power cycle the board. The Raspberry Pi Pico should then enumerate as a USD CDC class serial device. If you open a terminal application and connect to the communication port at 115200 baud, then press CTRL-D in the terminal, you should see a soft reboot of the system and the MicroPython REPL (Figure 2).

Figure 2: The MicroPython REPL appears after pressing CTRL-D and forcing a soft reboot of the MicroPython interpreter. (Beningo Embedded Group)

Writing a blink application in MicroPython

The simplest application that we can write is a blinky LED application. It’s the “Hello World” for embedded developers, even though we do at times like to print “Hello World” to a terminal as well. The Raspberry Pi Pico has an onboard LED that is located on gpio designation 25. In your favorite text editor, create a main.py file and write the following blinky application code in Python to toggle the onboard LED at 5 Hertz (Hz):

Copyfrom machine import Pin
import time

led = Pin(25, Pin.OUT)

while True:
    led.toggle()
    time.sleep_ms(100)

Listing 1: A basic LED application that toggles the onboard LED at 5 Hz. (Code source: Beningo Embedded Group)

Programming the blink application using rshell

Once the application is written, you can transfer it to the MicroPython file system using rshell. If you don’t have rshell on your computer, you can use the following Python command to install it:

Copypython -m pip install rshell

rshell is a remote shell application that specifically works with MicroPython. It can be used to communicate with the REPL, access the MicroPython file system, and transfer files between the development board and the host operating system. Running the command rshell will initiate a connection with the development board, as shown in Figure 3.

Figure 3: rshell is used as a remote shell to access the MicroPython file system, REPL, and perform communication operations. (Image source: Beningo Embedded Group)

Several commands that are supported by rshell. We can see the supported commands by typing help (Figure 4).

Figure 4: The help command will list all the commands supported by rshell. (Code source: Beningo Embedded Group)

Copying the file to the file system is easy. First, navigate to the directory where you saved your main.py application. Next, issue the copy command as follows:

Copycp main.py /pyboard

The result will be that main.py is copied to the root of the pyboard directory. You can verify this by typing ls /pyboard. Now enter the MicroPython REPL by typing the command repl into rshell. The MicroPython REPL will appear. Press CTRL-D to force a software reboot. The Pico board LED will now be blinking at 5 Hz.

The next steps are completely based on what you need to rapidly prototype! Go over the RP2 MicroPython documentation to learn more about the APIs and how to use the various peripherals.

Conclusion

There is always a need to rapidly prototype various solutions and explore how a system works. MicroPython can be a quick, efficient, and powerful means to do so, and installing it on a low-cost development board like the Raspberry Pi Pico can be very powerful. We saw that developers can easily leverage rshell to transfer applications and interact with the MicroPython interpreter. This interface makes it easy for both software and hardware developers to dig deeper before having to commit to production code.

关于此作者

Image of Jacob Beningo

Jacob Beningo 是一位嵌入式软件顾问,目前与十几个国家的客户保持合作,通过帮助客户改善产品质量、降低成本和加快上市时间来大幅改变他们的业务。Jacob 先后发表了 200 多篇关于嵌入式软件开发技术的文章,是一位广受欢迎的演讲者和技术培训师。他拥有三个学位,其中包括密歇根大学的工程硕士学位。如有需要,欢迎随时通过 jacob@beningo.com 与其联系,也可访问其网站 www.beningo.com,并订阅其月度 Embedded Bytes Newsletter

More posts by Jacob Beningo
 TechForum

Have questions or comments? Continue the conversation on TechForum, Digi-Key's online community and technical resource.

Visit TechForum