1/65
A vocabulary reference of MicroPython functions, hardware APIs, modules, and code patterns.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
print()
print("Hello") — display text.
Variables
x = 10; name = "LED"; on = True
if / elif / else
if x > 5: …
for / range
for i in range(10): print(i)
while
while True: …
Functions
def add(a, b): return a + b
Lists
items=[1,2,3]; items.append(4); items[0]
Dictionaries
d={"name":"LED"}; d["name"]
try / except
try: … except Exception as e: print(e)
len(x)
Number of items/characters.
int(x)
Convert to integer.
float(x)
Convert to float.
str(x)
Convert to string.
bool(x)
Convert to Boolean.
range()
Generate integer sequences.
enumerate()
Iterate with an index.
min() / max()
Smallest/largest value.
sum()
Add values.
abs()
Absolute value.
round()
Round a number.
Pin(pin, mode)
Create a GPIO pin. Example: Pin(2, Pin.OUT).
pin.value()
Read or set digital state.
pin.on() / pin.off()
Set output high/low where supported.
pin.toggle()
Toggle output where supported.
Pin.IN / OUT
Input/output modes.
Pin.PULL_UP / PULL_DOWN
Internal pull resistors where supported.
pin.irq()
Run a callback on a pin interrupt.
PWM(Pin(…))
Generate PWM output.
pwm.freq(hz)
Set/read PWM frequency.
pwm.duty_u16(value)
Set duty from 0–65535 where supported.
pwm.duty_ns(ns)
Set pulse width in nanoseconds where supported.
pwm.deinit()
Stop PWM.
ADC(Pin(…))
Read analog input.
adc.read_u16()
Read ADC as a 16-bit value where supported.
I2C(…)
Communicate with I2C devices.
i2c.scan()
Find I2C device addresses.
i2c.readfrom()
Read I2C bytes.
i2c.writeto()
Write I2C bytes.
SPI(…)
Communicate with SPI devices.
UART(…)
Serial communication.
Timer(…)
Run callbacks periodically.
machine.reset()
Reset the board.
time.sleep(s)
Pause for seconds.
time.sleep_ms(ms)
Pause for milliseconds.
time.sleep_us(us)
Pause for microseconds.
time.ticks_ms()
Monotonic millisecond counter.
time.ticks_us()
Monotonic microsecond counter.
time.ticks_diff(a,b)
Safely calculate tick differences.
import math
Math library.
math.sqrt(x)
Square root.
math.sin/cos/tan(x)
Trigonometry.
math.pi
π
math.floor/ceil(x)
Round down/up.
import random
Random-number library.
random.randint(a,b)
Random integer from a through b.
random.random()
Random float from 0 to 1.
open(path, mode)
Open a file: open("test.txt","w").
f.read()
Read file.
f.write(data)
Write data.
f.close()
Close file.
os.listdir()
List files/directories.
os.remove(path)
Delete a file.
json.dumps(x)
Convert object to JSON.
json.loads(s)
Parse JSON.
LED blink pattern
from machine import Pin; from time import sleep; led=Pin(2,Pin.OUT); led.value(1); sleep(1); led.value(0)
Button pattern
button=Pin(15,Pin.IN,Pin.PULL_UP); pressed=(button.value()==0)