MicroPython Basics Compact Reference

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/65

flashcard set

Earn XP

Description and Tags

A vocabulary reference of MicroPython functions, hardware APIs, modules, and code patterns.

Last updated 1:13 AM on 9/20/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

66 Terms

1
New cards

print()

print("Hello") — display text.

2
New cards

Variables

x = 10; name = "LED"; on = True

3
New cards

if / elif / else

if x > 5: …

4
New cards

for / range

for i in range(10): print(i)

5
New cards

while

while True: …

6
New cards

Functions

def add(a, b): return a + b

7
New cards

Lists

items=[1,2,3]; items.append(4); items[0]

8
New cards

Dictionaries

d={"name":"LED"}; d["name"]

9
New cards

try / except

try: … except Exception as e: print(e)

10
New cards

len(x)

Number of items/characters.

11
New cards

int(x)

Convert to integer.

12
New cards

float(x)

Convert to float.

13
New cards

str(x)

Convert to string.

14
New cards

bool(x)

Convert to Boolean.

15
New cards

range()

Generate integer sequences.

16
New cards

enumerate()

Iterate with an index.

17
New cards

min() / max()

Smallest/largest value.

18
New cards

sum()

Add values.

19
New cards

abs()

Absolute value.

20
New cards

round()

Round a number.

21
New cards

Pin(pin, mode)

Create a GPIO pin. Example: Pin(2, Pin.OUT).

22
New cards

pin.value()

Read or set digital state.

23
New cards

pin.on() / pin.off()

Set output high/low where supported.

24
New cards

pin.toggle()

Toggle output where supported.

25
New cards

Pin.IN / OUT

Input/output modes.

26
New cards

Pin.PULL_UP / PULL_DOWN

Internal pull resistors where supported.

27
New cards

pin.irq()

Run a callback on a pin interrupt.

28
New cards

PWM(Pin(…))

Generate PWM output.

29
New cards

pwm.freq(hz)

Set/read PWM frequency.

30
New cards

pwm.duty_u16(value)

Set duty from 0–65535 where supported.

31
New cards

pwm.duty_ns(ns)

Set pulse width in nanoseconds where supported.

32
New cards

pwm.deinit()

Stop PWM.

33
New cards

ADC(Pin(…))

Read analog input.

34
New cards

adc.read_u16()

Read ADC as a 16-bit value where supported.

35
New cards

I2C(…)

Communicate with I2C devices.

36
New cards

i2c.scan()

Find I2C device addresses.

37
New cards

i2c.readfrom()

Read I2C bytes.

38
New cards

i2c.writeto()

Write I2C bytes.

39
New cards

SPI(…)

Communicate with SPI devices.

40
New cards

UART(…)

Serial communication.

41
New cards

Timer(…)

Run callbacks periodically.

42
New cards

machine.reset()

Reset the board.

43
New cards

time.sleep(s)

Pause for seconds.

44
New cards

time.sleep_ms(ms)

Pause for milliseconds.

45
New cards

time.sleep_us(us)

Pause for microseconds.

46
New cards

time.ticks_ms()

Monotonic millisecond counter.

47
New cards

time.ticks_us()

Monotonic microsecond counter.

48
New cards

time.ticks_diff(a,b)

Safely calculate tick differences.

49
New cards

import math

Math library.

50
New cards

math.sqrt(x)

Square root.

51
New cards

math.sin/cos/tan(x)

Trigonometry.

52
New cards

math.pi

π\pi

53
New cards

math.floor/ceil(x)

Round down/up.

54
New cards

import random

Random-number library.

55
New cards

random.randint(a,b)

Random integer from a through b.

56
New cards

random.random()

Random float from 0 to 1.

57
New cards

open(path, mode)

Open a file: open("test.txt","w").

58
New cards

f.read()

Read file.

59
New cards

f.write(data)

Write data.

60
New cards

f.close()

Close file.

61
New cards

os.listdir()

List files/directories.

62
New cards

os.remove(path)

Delete a file.

63
New cards

json.dumps(x)

Convert object to JSON.

64
New cards

json.loads(s)

Parse JSON.

65
New cards

LED blink pattern

from machine import Pin; from time import sleep; led=Pin(2,Pin.OUT); led.value(1); sleep(1); led.value(0)

66
New cards

Button pattern

button=Pin(15,Pin.IN,Pin.PULL_UP); pressed=(button.value()==0)