Getting MicroPython on the Raspberry Pi Pico

Micropython Pico Rshell Quickpost!

This is a quick post to describe how to get MicroPython on to the Raspberry Pi Pico microcontroller board, and then transfer files across on to it using rshell.

Most tutorials, and indeed the official Raspberry Pi documentation, describe how to connect using the Thonny program instead. There is nothing wrong with this approach, but I simply prefer to use the command line in a terminal instead.

I am using a Pico connected to my Macbook Air via USB and using iTerm. In an update to this post I will describe how to use the Pico with PyCharm.

Programming the Pico with MicroPython

MicroPython software installation

​[1]: Go to the downloads page to download a .uf2 file for the Pico:
MicroPython for Pico Downloads

[2]: Press the BOOTSEL button while connecting the Pico to the Mac USB port. A new disk drive called RPi-RP2 will come up, and the switch can be released. To install Micropython on the Pico, drag and drop the .uf2 file onto this drive. The drive will disappear. Ignore the warning - its ok! Reconnect the USB cable to the Mac now. ​

Accessing the REPL

​ [3]: In the terminal, use the $ ls /dev/tty.* command to find out the COM port being used on the Mac. The name should be: /dev/tty.usbmodem0000000000001.

[4]:Create a project directory and virtual environment:
$ mkdir pico-tests
$ cd pico-tests
$ python3 -m venv --prompt pico-env venv
then activate the venv in the usual way: $ source venv/bin/activate

​[5]: Install rshell: (pico-env) $ pip install rshell

[6]: Connect the Pico: (pico-env) $ rshell -p /dev/tty.usbmodem0000000000001 --buffer-size 512

​ The output will look like:

Using buffer-size of 512  
Connecting to /dev/tty.usbmodem0000000000001 (buffer-size 512)...  
Trying to connect to REPL  connected 
Retrieving sysname ... rp2 
Testing if ubinascii.unhexlify exists ... Y  
Retrieving root directories ...  
Setting time ... Feb 04, 2021 14:33:07  
Evaluating board_name ... pyboard  
Retrieving time epoch ... Jan 01, 1970  
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.     
/Users/anthlis/pico-tests> 

The command should end with a new prompt. Type help to see all the commands rshell supports.

Note, that the Pico is described as a "pyboard" (for now maybe).

[7]: Now type in repl to get to the REPL.

/Users/anthlis/pico-tests> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.14 on 2021-02-02; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> 

To enter straight into the REPL instead, simply type:
$ rshell -p /dev/tty.usbmodem0000000000001 --buffer-size 512 repl

Write some MicroPython code for the Pico...

The MicroPython code below, is the equivalent of the hardware's "Hello, World!". It simply flashes the onboard green LED on and off!

# led_blink.py

from machine import Pin
from utime import sleep

led = Pin(25, Pin.OUT)
for i in range(5):
    led.on()
    sleep(1)
    led.off()
    sleep(1)

Alternatively, you can get the temperature from the onboard temperature sense diode which is read on the fourth analogue-to-digital converter (ADC) channel.

# temperature.py

import machine 
import utime  

sensor_temp = machine.ADC(4)  

conversion_factor = 3.3 / (65535)

while True:  
    reading = sensor_temp.read_u16() * conversion_factor 
    temperature = 27 - (reading - 0.706)/0.001721 
    print(temperature)
    utime.sleep(2)

To copy the code to main.py for immediate restart using the command line

[8]: Connect the Pico: (pico-env) $ rshell -p /dev/tty.usbmodem0000000000001 --buffer-size 512

> cp led_blink.py /pyboard/main.py The cp command above copies the local led_blink.py file from your computer to the root directory of the Pico file system with the name main.py.

[9]: Now power cycle the Pico by unplugging it from USB and then plugging it in again. The green LED should blink! It will also run if do a soft reboot (CTRL-D) in a REPL session, or by wiring a switch from RUN (pin 30) to GND (pin 28).

Next steps:

I want to explore interrupts vs multithreading on the Pico. I also want to experiment with the ADC using a potentiometer and driving servos for a biped robot. I'm also thinking of getting a Raspberry Pi 4 and will be looking to drive the Pico from this too.

I'll have future posts describing these experiments soon...

Useful References:

[1]: Documentation for Raspberry Pi Pico and other RP2040-based boards.
[2]: Get Started with MicroPython on Raspberry Pi Pico
[3]: Firmware for Raspberry Pi Pico on micropython.org