Hardware

Pin numbers below are Pico-shaped (Pin("LED") or GPIO 25). On ESP32 the onboard LED is often GPIO 2 or 8; on pyboard use pyb.LED or the documented pin. Check the device page.

Hardware

Pin

from machine import Pin
from time import sleep

led = Pin('LED', Pin.OUT)
btn = Pin(14, Pin.IN, Pin.PULL_UP)
while True:
    led.value(not btn.value())  # button to GND
    sleep(0.05)

Try the LED blink on the Pico emulator.

PWM

from machine import Pin, PWM
from time import sleep

pwm = PWM(Pin(15), freq=1000, duty_u16=0)
for duty in range(0, 65536, 4096):
    pwm.duty_u16(duty)
    sleep(0.05)
pwm.deinit()

ADC

from machine import ADC, Pin

probe = ADC(Pin(26))     # Pico ADC0
print(probe.read_u16())  # 0..65535

IRQ

from machine import Pin

led = Pin('LED', Pin.OUT)
btn = Pin(14, Pin.IN, Pin.PULL_UP)

def toggle(_):
    led.toggle()

btn.irq(trigger=Pin.IRQ_FALLING, handler=toggle)

while True:
    pass

Keep IRQ handlers tiny. Schedule work with micropython.schedule or set a flag for the main loop.

Timer

from machine import Pin, Timer

led = Pin('LED', Pin.OUT)
Timer(period=200, mode=Timer.PERIODIC, callback=lambda t: led.toggle())

I2C

from machine import Pin, I2C

i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400_000)
print([hex(a) for a in i2c.scan()])

Drivers for specific sensors are listed under library resources.

SPI

from machine import Pin, SPI

spi = SPI(0, baudrate=1_000_000, sck=Pin(2), mosi=Pin(3), miso=Pin(4))
cs = Pin(5, Pin.OUT, value=1)
cs(0)
spi.write(b'\x9f')
print(spi.read(3))  # JEDEC id on many flash chips
cs(1)

UART

from machine import UART, Pin

uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
uart.write(b'AT
')
print(uart.read())

RTC

from machine import RTC

rtc = RTC()
rtc.datetime((2026, 8, 17, 0, 12, 0, 0, 0))
print(rtc.datetime())

Watchdog

from machine import WDT

wdt = WDT(timeout=5000)
# call wdt.feed() at least every 5 seconds or the chip resets

Sleep

import machine, time

machine.lightsleep(1000)   # ms, clocks pause, RAM kept
# machine.deepsleep(1000)  # wakes by reset; use RTC alarms where supported
time.sleep_ms(100)