
An Introduction to RP2040 PIO with CircuitPython
Created by Jeff Epler
Last updated on 2021-05-03 11:50:57 AM EDT

2
3
3
3
3
4
4
7
7
8
9
9
9
11
12
13
16
17
19
20
23
23
25
25
29
30
Guide Contents
Guide Contents
Overview
Major differences between adafruit_pioasm and standard pioasm
.wrap and .wrap_target are not supported
mov operator restrictions
Expressions are not supported
Products
Installing CircuitPython
CircuitPython Quickstart
Flash Resetting UF2
Installing Mu Editor
Download and Install Mu
Using Mu
Installing Libraries
Using PIO to turn an LED on and off
Code Walkthrough
Using PIO to control LED brightness
Code Walkthrough
Using PIO to blink a LED quickly or slowly
Code Walkthrough
Using PIO to drive a NeoPixel
Parts
Full Code Listing
Code Walk-through
API Documentation: adafruit_pioasm
API Documentation: rp2pio
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 2 of 31

Overview
A feature that sets the Raspberry Pi Foundation RP2040 microcontroller apart from other microcontrollers
is "PIO". The RP2040 datasheet says that the "programmable input/output block (PIO) is a versatile
hardware interface. It can support a variety of IO standards… PIO is programmable in the same sense as a
processor."
In this guide, you'll learn how to write and use PIO programs from CircuitPython. The official
datasheet (https://adafru.it/QLC) (chapter 3), the book "Get Started with MicroPython on Raspberry Pi
Pico" (https://adafru.it/QLD) and pico-examples (https://adafru.it/Qa2) (pio folder) are helpful resources too,
but CircuitPython sometimes deviates from the way that PIO is used in other environments like C or
MicroPython.
Examples in this guide require CircuitPython 6.2.0-beta.3 or later, or the Absolute Latest version.
Major differences between adafruit_pioasm and standard pioasm
.wrap and .wrap_target are not supported
Instead, an implicit .wrap is at the bottom of every program, and an implicit .wrap_target is at the top of
every program. You can add a jmp instruction instead of using .wrap but remember that this slightly
changes the instruction length and the timing of your pio program.
mov operator restrictions
The mov instruction can accept an optional argument, called an operator, to reverse ( :: ) or invert ( ~ ) its
argument. In adafruit_pioasm, one or more spaces must come between the :: or ~ and the operand.
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 3 of 31

These spaces are not required by the official dialect.
The official dialect allows "!" to mean the same as "~". This is not accepted by adafruit_pioasm.
Expressions are not supported
Standard pioasm supports expressions like [T1 + 1] . In pioasm, calculations are not supported. Instead,
use string formatting operations to insert the computed value where necessary, e.g., f"""…[{T1+ 1}]…"""
Products
These examples were designed for the Raspberry Pi Pico but can be adapted to the range of boards with
the RP2040 microcontroller. The QT Py RP2040 requires the addition of an external LED and currentlimiting resistor for the standard LED examples.
Adafruit Feather RP2040
A new chip means a new Feather, and the Raspberry Pi RP2040 is no exception. When we saw this chip
we thought "this chip is going to be awesome when we give it the Feather...
Out of Stock
Your browser does not support the video tag.
Adafruit ItsyBitsy RP2040
A new chip means a new ItsyBitsy, and the Raspberry Pi RP2040 is no exception. When we saw this chip
we thought "this chip is going to be awesome when we give it the ItsyBitsy...
$9.95
In Stock
Out of
Stock
Add to Cart
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 4 of 31

Your browser does not support the video tag.
Adafruit QT Py RP2040
What a cutie pie! Or is it... a QT Py? This diminutive dev board comes with one of our new favorite chip,
the RP2040. It's been made famous in the new
$9.95
In Stock
Raspberry Pi Pico RP2040
The Raspberry Pi foundation changed single-board computing when they released the Raspberry Pi
computer, now they're ready to...
Out of Stock
Add to Cart
Out of
Stock
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 5 of 31

Raspberry Pi Pico RP2040
The Raspberry Pi foundation changed single-board computing when they released the Raspberry Pi
computer, now they're ready to...
Out of Stock
Out of
Stock
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 6 of 31

Installing CircuitPython
CircuitPython (https://adafru.it/tB7) is a derivative of MicroPython (https://adafru.it/BeZ) designed to simplify
experimentation and education on low-cost microcontrollers. It makes it easier than ever to get
prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the
CIRCUITPY drive to iterate.
CircuitPython Quickstart
Follow this step-by-step to quickly get CircuitPython working on your board.
https://adafru.it/QaP
Click the link above and download the latest UF2 file.
Download and save it to your desktop (or wherever is handy).
Start with your Pico unplugged from USB. Hold down the
BOOTSEL button, and while continuing to hold it (don't let
go!), plug the Pico into USB. Continue to hold the BOOTSEL
button until the RPI-RP2 drive appears!
If the drive does not appear, unplug your Pico and go through
the above process again.
A lot of people end up using charge-only USB cables and it is
very frustrating! So make sure you have a USB cable you
know is good for data sync.
https://adafru.it/QaP
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 7 of 31

You will see a new disk drive appear called RPI-RP2.
Drag the adafruit_circuitpython_etc.uf2 file to RPI-RP2.
The RPI-RP2 drive will disappear and a new disk drive called
CIRCUITPY will appear.
That's it, you're done! :)
Flash Resetting UF2
If your Pico ever gets into a really
weird
state and doesn't even show up as a disk drive when installing
CircuitPython, try installing this 'nuke' UF2 which will do a 'deep clean' on your Flash Memory. You will
lose all the files on the board, but at least you'll be able to revive it! After nuking, re-install CircuitPython
https://adafru.it/QAJ
https://adafru.it/QAJ
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 8 of 31

Installing Mu Editor
Mu is a simple code editor that works with the Adafruit CircuitPython boards. It's written in Python and
works on Windows, MacOS, Linux and Raspberry Pi. The serial console is built right in so you get
immediate feedback from your board's serial output!
Mu is our recommended editor - please use it (unless you are an experienced coder with a favorite
editor already!)
Download and Install Mu
Download Mu from https://codewith.mu (https://adafru.it/Be6).
Click the Download or Start Here links there for downloads
and installation instructions. The website has a wealth of other
information, including extensive tutorials and and how-to's.
Using Mu
The first time you start Mu, you will be prompted to select
your 'mode' - you can always change your mind later. For now
please select CircuitPython!
The current mode is displayed in the lower right corner of the
window, next to the "gear" icon. If the mode says "Microbit" or
something else, click the Mode button in the upper left, and
then choose "CircuitPython" in the dialog box that appears.
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 9 of 31

Mu attempts to auto-detect your board, so please plug in your
CircuitPython device and make sure it shows up as
a CIRCUITPY drive before starting Mu
You can now explore Mu! The three main sections of the window are labeled below; the button bar, the
text editor, and the serial console / REPL.
Now you're ready to code! Let's keep going...
© Adafruit Industries https://learn.adafruit.com/intro-to-rp2040-pio-with-circuitpython Page 10 of 31