alankrantas / raspberrypi-pico-micropython-cookbook

MicroPython drivers and experiments on Raspberry Pi Pico

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Raspberrypi Pico MicroPython Cookbook

Updating...

Blinky

from machine import Pin
import time

led = Pin(25, Pin.OUT)

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

or

from machine import Pin, Timer

led = Pin(25, Pin.OUT)
blink = lambda _: led.toggle()
    
Timer().init(mode=Timer.PERIODIC, period=500, callback=blink)

or

from machine import Pin
import uasyncio

async def blink(led):
    while True:
        led.toggle()
        await uasyncio.sleep_ms(500)

uasyncio.run(blink(Pin(25, Pin.OUT)))

or

from machine import Pin
import rp2

@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink():
    set(pins, 1)
    set(x, 31)                  [6]
    label('delay_high')
    nop()                       [29]
    jmp(x_dec, 'delay_high')

    set(pins, 0)
    set(x, 31)                  [6]
    label('delay_low')
    nop()                       [29]
    jmp(x_dec, 'delay_low')
    
sm = rp2.StateMachine(0, blink, freq=2000, set_base=Pin(25))
sm.active(1)

Overclocking

The Pico has default frequency of 125 MHz but can be overclocked as high as 270 MHz (which uses higher CPU voltage and might be less stable):

from machine import freq

freq(270000000)

Remember to reset the frequency back to 125000000. (Note: firmware v1.18 seems fixed this problem.)

NeoPIxel (WS2812) PIO Driver

The NeoPixel driver is based on the official PIO example, repackaged into a class similar to CircuitPython's NeoPixel driver.

DHT11/DHT22 PIO Driver

The DHT driver is based on Harry Fairhead & Mike James' DHT22 PIO code, repackaged into a class similar to MicroPython's dht module on ESP boards.

Multicore and Game of Life

threads.py is a simple example of how to run two tasks simultaneously on both of RP2040's cores.

There's also two versions of Conway's Game of Life with OLED display, with or without utilizing dual cores.

Links

About

MicroPython drivers and experiments on Raspberry Pi Pico

License:MIT License


Languages

Language:Python 100.0%