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는 임베디드 소프트웨어 컨설턴트로서 현재 십여 개국 이상의 국가에 있는 고객들과 협력하여 제품 품질, 비용, 출시 기간을 향상시켜 고객의 비즈니스를 극적으로 변화시키고 있습니다. 그는 인기 있는 강연자이자 기술 교육자이며 임베디드 소프트웨어 개발 기술에 대해 200개 이상의 기사를 발표했습니다. 그는 미시간 대학교 공학 석사 학위를 비롯하여 세 개의 학위를 소지하고 있습니다. 월간 Embedded Bytes 뉴스레터를 수신하려면 jacob@beningo.com 및 Jacob Beningo의 웹 사이트인 www.beningo.com으로 언제든지 문의해 주세요.

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