Examples#

Annotated firmware examples showing real-world PyMCU patterns.

Unless otherwise noted, examples target ATmega328P / Arduino Uno and ship with a cycle-accurate integration test (AVR8Sharp simulator) so you can run them without hardware. Raspberry Pi Pico (RP2040) examples run on the alpha ARM backend and are validated with the RP2040Sharp emulator.


MicroPython compatibility layer#

Use these examples if you are porting code from MicroPython or writing new firmware that should remain runnable on a real MicroPython board.

Example

Topics

Blink

machine.Pin, utime.sleep_ms

UART Echo

machine.UART, machine.Pin

ADC Read

machine.ADC, machine.UART

DHT11 Sensor

machine.Pin, local DHT11 driver

Elapsed time with ticks_ms()

utime.ticks_ms, utime.ticks_diff

Signal (active-low LED)

machine.Signal, active-low logic

PWM Fade

machine.PWM (hardware) + soft PWM


CircuitPython compatibility layer#

Use these examples if you are porting Adafruit / CircuitPython code or writing firmware that should remain runnable on a real CircuitPython board.

Example

Topics

Blink

board, digitalio, time

UART Echo

busio.UART, digitalio

Button-controlled LED

digitalio INPUT + OUTPUT, pull-up

DHT11 Sensor

busio, digitalio, local DHT11 driver

Morse Blinker

digitalio, time, @inline

Traffic Light

digitalio, time, state machine

ADC-controlled PWM Dimmer

analogio.AnalogIn, pwmio.PWMOut


PyMCU HAL (native)#

Direct use of the PyMCU hardware abstraction layer — maximum control, zero overhead.

Example

Topics

Blink

Pin, delay_ms

Button Debounce

Pin, edge detection, uint16

EEPROM Read / Write

EEPROM.read / EEPROM.write

Watchdog Timer

Watchdog.enable / feed / disable

I2C Bus Scanner

I2C.ping, hex output

SSD1306 OLED Display

SSD1306 driver, I2C

NeoPixel (WS2812B)

NeoPixel driver, interrupt-safe


Advanced patterns#

Example

Topics

Traffic-Light State Machine

Timer, match/case FSM, @property

Sensor Dashboard

ADC, Timer ISR, GPIOR flags, UART

Sleep / Wake on Interrupt

sleep_idle, Pin.irq, power management

C FFI — Calling C Functions from Python

@extern, C FFI, avr-gcc linking


Raspberry Pi Pico (RP2040) — alpha#

Firmware for the Raspberry Pi Pico via the alpha ARM backend (pip install pymcu-arm) — GPIO + UART0 on core 0. All three API styles (native HAL, MicroPython, CircuitPython) are shown in Raspberry Pi Pico (RP2040).