0
0
Iot-protocolsHow-ToBeginner · 3 min read

How to Use PIO on Raspberry Pi Pico: Simple Guide

To use PIO on Raspberry Pi Pico, write a small assembly program for the PIO state machine and load it using the rp2.asm_pio decorator in MicroPython. Then, start the state machine and communicate with it via Python code on the Pico.
📐

Syntax

The basic syntax to use PIO on Raspberry Pi Pico involves defining a PIO program with @rp2.asm_pio(), creating a state machine with rp2.StateMachine(), and starting it with sm.active(1).

Key parts:

  • @rp2.asm_pio(): Decorator to write PIO assembly code.
  • rp2.StateMachine(id, program, freq): Creates a state machine instance.
  • sm.active(1): Starts the state machine.
  • sm.put() and sm.get(): Send and receive data to/from the PIO.
python
import rp2
from machine import Pin

@rp2.asm_pio()
def pio_program():
    # PIO assembly instructions here
    nop() [31]

sm = rp2.StateMachine(0, pio_program, freq=2000_000)
sm.active(1)

# Use sm.put() and sm.get() to communicate
💻

Example

This example shows a simple PIO program that blinks an LED connected to GPIO 15 on the Raspberry Pi Pico.

The PIO program sets the pin high and low with delays, controlled by the state machine frequency.

python
import rp2
from machine import Pin
import time

# Define PIO program to blink LED
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW)
def blink():
    wrap_target()
    nop()    .side(1) [31]  # Set pin high
    nop()    .side(0) [31]  # Set pin low
    wrap()

# Initialize state machine on GPIO 15
sm = rp2.StateMachine(0, blink, freq=2000_000, sideset_base=Pin(15))
sm.active(1)

# Run for 5 seconds
time.sleep(5)
sm.active(0)
⚠️

Common Pitfalls

Common mistakes when using PIO on Raspberry Pi Pico include:

  • Not setting the correct sideset_base pin for output pins.
  • Forgetting to start the state machine with sm.active(1).
  • Using incorrect PIO assembly syntax or timing delays.
  • Not matching the state machine frequency to the timing needs.

Always test your PIO program with simple outputs first to verify pin control.

python
import rp2
from machine import Pin

# Wrong: Missing sideset_base, LED won't blink
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW)
def wrong_blink():
    wrap_target()
    nop()    .side(1) [31]
    nop()    .side(0) [31]
    wrap()

sm = rp2.StateMachine(0, wrong_blink, freq=2000_000)  # sideset_base missing
sm.active(1)  # LED won't blink

# Right: Specify sideset_base for GPIO 15
sm = rp2.StateMachine(0, wrong_blink, freq=2000_000, sideset_base=Pin(15))
sm.active(1)  # LED blinks now
📊

Quick Reference

CommandDescription
@rp2.asm_pio()Decorator to write PIO assembly code
rp2.StateMachine(id, program, freq, sideset_base)Create and configure a PIO state machine
sm.active(1)Start the state machine
sm.active(0)Stop the state machine
sm.put(value)Send data to PIO
sm.get()Receive data from PIO

Key Takeaways

Use @rp2.asm_pio() to write PIO assembly programs for Raspberry Pi Pico.
Create a StateMachine with rp2.StateMachine and specify the program and frequency.
Always set sideset_base pin when controlling output pins with PIO.
Start the state machine with sm.active(1) to run your PIO program.
Test simple PIO programs first to avoid common mistakes.