MicroPython

For my latest project, I’ve decided to use a Pi Pico and MicroPython. You can get going with a simple editor and command line or the minimalist IDE, Thonny. But I am more familiar with VSCode so I decided to use that.

Pi Pico

You can install MicroPython on the Pi Pico by setting it into mass storage mode using the boot select button when it is plugged in. Details available at Raspberry Pi Pico Documentation.

VS Code

VS Code works with different libraries and systems via a plugin system. The plugin needed for MicroPython on a Pico is known as MicroPico. It was previously known as Pico-W-Go so you might find it referenced as that in a few tutorials. The plugin works along side others such as the intellicode and pylancee to provide highlighting of syntax, a REPL on the board and upload/remote file service.

I had a bit of a challenge installing it for a couple of reasons. I’m not a regular Python user so had an older version installed. When I installed the right version I then ended up with two different versions installed and the plugin needed to be told which one to use.

The second issue was that it wasn’t detecting the Pi Pico. This turned out to being the plugin itself using Python and PySerial to detect the Com ports. This is all clearly documented on the MicroPico extention page and in the MicroPico Source Code. So reading the manual did help!

MicroControllers with MicroPython

To test everything was working, I used the “Hello World” of Microcontrollers the Blink or Flashing LED code. This should look familiar to mode people who’ve used a high level language. It starts with some library references, defines a variable called “pin” from a class called “Pin” which takes in some parameters for which Pin (the LED) and the behaviour (output). It then prints a debug message and runs a continual loop which toggles the LED and then waits for 1s.

Flash.pyfrom machine import Pin
from utime import sleeppin = Pin("LED", Pin.OUT)print("LED flashing")
while True:
pin.toggle()
sleep(1)

For my code, I needed to detect pulses from an input. I did that using an interrupt and a timer.

from machine import Timer
from machine import Pin
from time import sleep

When defining the pin, you can assigning an “interrupt request” or “irq” handler. This is a piece of code which runs when the state changes. You’ll find that a lot of microcontrollers have restrictions on IRQ capability but in the Pi Pico case, all the Pins support these and you can have up-to 32 defined. Short pieces of code are recommend for the interrupt handlers and you do need to make them so they could be part way through when they get triggered again (re-entrant). So for my code, I just made them increment a counter.

pulse_count = 0 # global variable
pulses_detected = Falsedef pulse_detector(pin):
global pulse_count
pulse_count += 1pin_pulseDetection = Pin(26, mode=Pin.IN, pull=Pin.PULL_UP)
pin_pulseDetection.irq(trigger=Pin.IRQ_RISING,handler=pulse_detector)

Once I’ve detected pulses and started counting them, I used a timer to see how many pulses were generated in a time period.

def pulse_counter(timer):
global pulses_detected
global pulse_count
if pulse_count > 5:
pulses_detected = True
else:
pulses_detected = False
pulse_count = 0soft_timer = Timer(mode=Timer.PERIODIC, period=10000, callback=pulse_counter)

My code didn’t need to know an accurate count, just that we were getting pulses or not. So I simply check to see if I was getting more than a certain number of pulses per second.

Once I’d picked up the relevant constructs, I found it an easy language to work with and the plugin/VSCode arrangement worked well for me.

Further reference

RP2040 Datasheet – The chip behind the Pi Pico

Elicia and Christopher White have also finding out about MicroPython directly from the source, Damien George on the Embedded.FM Episode 456

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available