The machine module
Everything here is MicroPython's own API, implemented over the simulator. Pin numbers are GP numbers: Pin(13) is GP13, wherever it sits on the header.
Pin
from machine import Pin
led = Pin(13, Pin.OUT)
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
led.on()
led.off()
led.toggle()
led.value(1)
print(led.value())
Modes are Pin.IN and Pin.OUT; pulls are Pin.PULL_UP, Pin.PULL_DOWN and Pin.PULL_NONE. Pin(25) is the LED built into the board: not a header pin, but a real output. An input's pull drives the pin's net like the real 50–80 kΩ internal pull, so a button can be wired to 3V3 with a pull-down or to GND with a pull-up.
Reading an output returns the level you set, immediately — no need to wait for the board to settle.
PWM
from machine import PWM, Pin
pwm = PWM(Pin(13), freq=1000, duty_u16=32768)
pwm.duty_u16(65535)
pwm.duty_ns(1_500_000)
pwm.deinit()
Duty is 0–65535 (duty_u16) or a pulse width in nanoseconds (duty_ns); the frequency is in Hz. The same duty drives the parts on the pin, so an LED dims and a servo sees the pulse width.
ADC
from machine import ADC
adc = ADC(26) # GP26, GP27 or GP28; ADC(0)–ADC(2) are the channel names
counts = adc.read_u16() # 0–65535 over 0–3.3 V
small = adc.read() # 0–4095, the 12-bit value
temp = ADC(4) # the RP2040's internal temperature sensor
print(temp.read())
The reading is the voltage on the pin's net, scaled against the board's 3.3 V reference. A pin with nothing driving it reads 0. ADC(4) is not a header pin: it reports the room temperature through the chip's internal sensor, as it does on a real Pico.
I2C
from machine import I2C, Pin
bus = I2C(0, sda=Pin(4), scl=Pin(5), freq=400_000)
bus.scan() # [104]
bus.writeto(0x68, b"\x6b\x00")
bus.readfrom(0x68, 1) # b"\x00"
bus.writeto_mem(0x68, 0x6B, b"\x00")
bus.readfrom_mem(0x68, 0x6B, 1) # b"\x00"
The Pico has no fixed I²C pins: name the SDA/SCL pair, and the devices wired to those pins answer on that bus. scan() lists the addresses; a read from a missing device raises OSError. Register helpers use 8-bit register addresses (readfrom_mem, writeto_mem); readfrom_into and readfrom_mem_into fill a buffer you pass.
SPI
from machine import Pin, SPI
cs = Pin(17, Pin.OUT, value=1)
spi = SPI(0, baudrate=1_000_000, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
cs.value(0)
reply = bytearray(3)
spi.write_readinto(bytes([1, 0x80, 0]), reply)
cs.value(1)
Name the SCK/MOSI/MISO pins; chip select is an ordinary output pin you drive yourself, as on the board. The simulator watches the CS pin you drive and only the device on that net answers, exactly as the hardware behaves. write sends only, read clocks in bytes (sending zeros), and write_readinto does both. Devices on those pins answer; a wrong mode or a missing device shows up as zeros and a warning.
UART
from machine import UART, Pin
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
uart.write(b"hello\r\n")
while not uart.any():
utime.sleep_ms(10)
line = uart.readline()
Name the TX and RX pins. any() reports buffered bytes, read() drains (or takes a count), readline() waits for a newline in simulated time, and readinto() fills a buffer. A device wired to the pins answers at its own baud rate; a mismatched baud garbles the bytes, as it would on the bench.
Time
import utime
utime.sleep(0.5)
utime.sleep_ms(50)
start = utime.ticks_ms()
elapsed = utime.ticks_diff(utime.ticks_ms(), start)
On the Pico utime and time are the same module. Sleeps follow the simulation's clock: the board pauses when you pause it, and a judged run can fast-forward through long waits. ticks_ms wraps like the board's 30-bit counter; ticks_diff handles the wrap.
Board identity
import machine
print(machine.freq()) # 133000000
print(machine.unique_id()) # a stable id for this simulated board
machine.reset() # restarts the run
Timers and pin interrupts
from machine import Pin, Signal, Timer
import utime
def every(timer):
print("tick")
timer = Timer()
timer.init(period=500, mode=Timer.PERIODIC, callback=every)
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
def changed(pin):
print("changed", pin.value())
irq = button.irq(trigger=Pin.IRQ_FALLING, handler=changed)
active_low = Signal(Pin(15, Pin.OUT), invert=True)
active_low.on() # physical pin low
utime.sleep(2)
timer.deinit()
irq.off()
active_low.off()
Callbacks are cooperative: the firmware runs them in short slices during a simulated sleep, so a program that never sleeps never ticks. A timer catches up every period a long sleep covered; an IRQ compares the pin with its last level at each tick, so a pulse shorter than the longest sleep can be missed. Signal is an output wrapper, not an edge watcher. A judged run keeps timers and IRQs alive until the end of the run window even after main.py returns.
Default bus pins and missing modules
I2C(0) uses GP4/GP5, I2C(1) uses GP6/GP7, SPI(0) uses GP18/GP19/GP16, SPI(1) uses GP10/GP11/GP8, UART(0) uses GP0/GP1, and UART(1) uses GP4/GP5. Naming the pins explicitly is still clearer in a circuit problem.
import rp2 and import network raise a clear RuntimeError: PIO and Wi-Fi are not simulated. os.uname() returns the simulated Pico identity, and asyncio.run raises a clear error directing you to create_task()/await. time.time() follows the simulated clock with a fixed 2024-01-01 UTC epoch.