Created
September 7, 2025 04:36
-
-
Save anecdata/ea24c479e8794f000c39faf0e8483c0b to your computer and use it in GitHub Desktop.
CircuitPython PIO UART pin finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PIO UART pin finder | |
# modified from an old SPI pin finder, probably _somewhere_ in the Adafruit docs | |
# PIO UART doesn't require sequential pins, and pins don't have to be associated with a particular peripheral | |
# But... ValueError: Cannot use GPIO0..15 together with GPIO32..47 | |
import board | |
import busio | |
from microcontroller import Pin | |
import adafruit_pio_uart | |
def is_pio_uart(tx, rx, cts, rts): | |
try: | |
p = adafruit_pio_uart.UART(tx, rx, 9600, 8, None, 1, 1, cts, rts) | |
p.deinit() | |
return True | |
except ValueError: | |
return False | |
def get_unique_pins(): | |
# any combination of 4 pins >= GP32 [GP30]? No in 10.0.0-beta.3 | |
exclude = ['GP0', 'GP1', 'GP10', 'GP11', 'GP12', 'GP13', 'GP14', 'GP15', 'GP16', 'GP17', 'GP18', 'GP19', 'GP2', 'GP20', 'GP21', 'GP22', 'GP23', 'GP24', 'GP25', 'GP26', 'GP27', 'GP28', 'GP29', 'GP3', 'GP30', 'GP31', 'GP4', 'GP5', 'GP6', 'GP7', 'GP8', 'GP9',] | |
pins = [pin for pin in [ | |
getattr(board, p) for p in dir(board) if p not in exclude] | |
if isinstance(pin, Pin)] | |
unique = [] | |
for p in pins: | |
if p not in unique: | |
unique.append(p) | |
return unique | |
pins = get_unique_pins() | |
for tx_pin in pins: | |
for rx_pin in pins: | |
for cts_pin in pins: | |
for rts_pin in pins: | |
pin_set = {tx_pin, rx_pin, cts_pin, rts_pin} # set() | |
if len(pin_set) < 4: | |
continue | |
print(f"tx={tx_pin}, rx={rx_pin}, cts={cts_pin}, rts={rts_pin}", end="\r") | |
if is_pio_uart(tx_pin, rx_pin, cts_pin, rts_pin): | |
print(f"tx={tx_pin}, rx={rx_pin}, cts={cts_pin}, rts={rts_pin}") | |
print(f"Done {' '*50}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment