Machine - Hardware Related Functions
Pin Class
machine.Pin(id, mode=None, pull=None, value)
Pin object constructor
- id: GPIO number (0-29 for Pico)
- mode: Pin mode, options:
None, Pin.IN(0), Pin.OUT(1), Pin.OPEN_DRAIN(2) - pull: Internal pull-up/down resistor (only valid in input mode), options:
None, Pin.PULL_UP(1), Pin.DOWN(2) - value: Port value in output or open-drain mode (0 for low, 1 for high)
Pin.init(mode=None, pull=None)
Reinitialize GPIO port
Pin.value([x])
- Returns GPIO port value when no parameter is given
- Writes value to GPIO port when parameter 0/1 is provided
Pin.toggle()
Toggles port state in output or open-drain mode
Example: LED blinking
from machine import Pin
import time
led = Pin(25, Pin.OUT)
while True:
led.toggle()
time.sleep(1) # Toggle LED every second
Pin.irq(handler=None, trigger=(Pin.IRQ_FALLING|PIN.IRQ_RISING))
External interrupt function
- handler: Callback function when interrupt triggers
- trigger: Interrupt trigger condition (edge/level triggered)
Other Functions
For output/open-drain mode:
Pin.low(), Pin.off(): Set port to low voltagePin.high(), Pin.on(): Set port to high voltage
Example: LED Control
from machine import Pin
import utime
# Button on GPIO15, input mode with pull-up
button_num = 15
button = Pin(button_num, Pin.IN, Pin.PULL_UP)
# Onboard LED on GP25, external LED on GP16
led1_num = 25
led2_num = 16
led1 = Pin(led1_num, Pin.OUT)
led2 = Pin(led2_num, Pin.OUT)
while True:
led2.off() # Turn off external LED
if button.value() == 0: # Check if button pressed (0 when pressed)
utime.sleep(0.01)
if button.value() == 0: # Software debounce
led1.toggle() # Toggle onboard LED
led2.on() # Turn on external LED (GP16 high)
print("The button is pressed.")
while button.value() == 0:
# Wait while button remains pressed (external LED stays on)
utime.sleep(0.01)
PWM Class
machine.PWM(pin)
Reinitialize specified GPIO as PWM output
- pin: Pin class object
PWM.deinit()
Deinitialize PWM, stop PWM output
PWM.freq([value])
Set PWM frequency (in Hz), automatically calculates divider and TOP register values
PWM.duty_u16([value])
Set duty cycle
- value: Duty cycle ratio [0,65536], calculates corresponding value for CC register
PWM.duty_ns([value])
Set high-level duration per cycle in nanoseconds
Example: Breathing LED
from machine import Pin, PWM
import time
led = PWM(Pin(25)) # Initialize onboard LED as PWM
led.freq(1000) # Set frequency
led_duty = 0 # Initial value
led_direction = 1 # Step size
while True:
led_duty += led_direction # Increase/decrease duty cycle
if led_duty >= 100: # Max
led_duty = 100
led_direction = -1
elif led_duty <= 0: # Min
led_duty = 0
led_direction = 1
led.duty_u16(int(led_duty * 655.36)) # Convert ratio to value
if led_duty % 5 == 0:
print(led_duty) # For monitoring
time.sleep(0.01)
# 2-second cycle
ADC Class
machine.ADC(id)
Initialize as ADC object
- id: Can be GPIO or ADC channel (GPIO must support ADC when using Pin object)
- Channels 0-3: Pico GPIO 26-29
- Channel 4: Internal temperature sensor
ADC.read_u16()
Read ADC channel value [0,65525]

When will I have a drink and discuss the details again?