MicroPython on Raspberry Pi Pico

From Embedded Workshop
Jump to navigation Jump to search

Last weekend, I picked up two Raspberry Pi Pico boards at the local Microcenter.
Once you have a Pico, you should visit the following website:
https://www.raspberrypi.org/documentation/rp2040/getting-started/

If you plan on installing MicroPython onto your Pico, I recommend downloading and reading the following two resources (books):
(Both of them detail how to install MicroPython onto your device.)
https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf
https://hackspace.raspberrypi.org/books/micropython-pico/pdf/download

CircuitPython is available via this webpage:
https://circuitpython.org/board/raspberry_pi_pico/

I recommend using Thonny when using MicroPython with the Pico.
It allows storing (and retrieving) programs to/from your host's hard drive.

Here is my version of the "blinky" program for the board:

#led_blink.py
#With the Raspberry Pi Pico board, blink the on-board LED
from machine import Pin
import utime
led=Pin(25,Pin.OUT) # on-board LED is GPIO 25
while True:
    led.on()
    utime.sleep(0.2)
    led.off()
    utime.sleep(0.2)
If you change the GPIO to 0 (upper left corner of the board), select that GPIO for your LED output,
and remove the delays, you'll get a 66.9KHz square wave output from GPIO_0. Not too bad for an interpreter....
from machine import Pin
led=Pin(0,Pin.OUT)
while True:
    led.on()
    led.off()
If you use led.value(0) and led.value(1) instead of led.off() and led.on(), the resulting output frequency
is a little slower, 64.3KHz, since the code now has to evaluate the parameter passed in.

Using the Thonny application to interface with the Raspberry Pi Pico, running MicroPython, provides
the ability to store files onto the Pico board. If you store a Python program as main.py, it will auto-start
when power is applied.