Category Archives: MicroPython

MicroPython on the Raspberry Pi Pico

So I bought myself a “Wireless Plasma Kit” which included the Raspberry Pi Pico W as the controller. Now a long time back (probably when they were first introduced) I played with the non-wireless Pico, but have pretty much forgotten everything around it. First off I accidentally removed all the software off of my new Pico W and then had to remind myself/learn to put things back together, so let’s look at this. It’s pretty simple but like most things, it is when you know how.

Note: The first item in the list below will wipe your Pico (and yes, this is exactly what I did not realising that software was already installed – correct, I didn’t read the guide on setting things up.

  • Push and hold the BOOTSEL button and plug the pico’s USB connect into your computer, then release the BOOTSEL. If all went well your pico will appear like an attached USB driver
  • Locate and download the MicroPython UF2 file, for example the latest UF2 for the Pico W is available here http://www.micropython.org/download/rp2-pico-w/ – if you’re not using the Pico W look for the UF2 for your device, i.e. the non-W Pico.
  • Drag the UF2 onto the Pi USB folder. At this point the pico will reboot

The UF2 file is USB Flashing Format file and in this case is a bootloader. MicroPython sets up a MicroPython REPL, so you can type commands directly to it via the assigned COM port.

Okay, so MicroPython is installed, what next?

Depending on your preference, there are editors such as Thonny and also an extension to VS Code (and others). I prefer VS Code, but Thonny seemed simpler to interact with the pico, so I went with that in the end.

In Thonny, if connected okay you should see a Shell with the MicroPython REPL running, just type in the following, followed by the enter key

print("Hello World")

If all went well the REPL should write back Hello World

If you weren’t able to see the REPL or got no response try choosing or configuring the interpreter (bottom right of Thonny, click on the option there). Mine says MicroPython (Raspberry Pi Pico) COM8.

Start up

When the Pico starts, i.e. first plugged in, it looks for the file /boot.py. If one exists it’ll run this to set any low-level code. Whilst you can supply one of these files, more likely you’ll add a /main.py which is run after /boot.py and this should contain your main script that should run when the board is restarted or reset.

Note: Remember, the main.py will run and end unless you explicitly loop within it. So, if you output “Hello World” (for example) then that’s all the Pico will do and then essentially sits there doing nothing.

Let’s add the hardware equivalent of Hello World, i.e. turn on the Pico onboard LED, type/paste the following into the REPL

from machine import Pin
#led = Pin(25, Pin.OUT) # On the Pico not W
led = Pin("LED", Pin.OUT)
led.value(1)

Or try this, to flash the LED

import time
from machine import Pin

#led = Pin(25, Pin.OUT) # On the Pico not W
led = Pin("LED", Pin.OUT)

while True:
    led.toggle()    
    time.sleep(1.0)  

So when you use Ctrl+D from Thonny to soft reset, now the Pico should restart and blink the LED.

References

Raspberry Pi Pico Python SDK
Connecting to the Internet with Pico W
MicroPython, Quick Reference