Skip to content
RoboLearn
Not completed

MicroPython basics

ProgrammingIntermediate30 minRun MicroPython ↓Before this: Loops and repetition

What you’ll learn

  • What MicroPython is and which boards run it
  • Python syntax compared with Arduino C++
  • Controlling pins, reading inputs and timing

MicroPython

MicroPython is a small version of Python that runs directly on microcontrollers such as the Raspberry Pi Pico, ESP32 and BBC micro:bit. You type code, save it as main.py on the board, and it runs.

Python vs Arduino C++

Arduino C++ MicroPython
int x = 5; x = 5
{ } blocks indentation (4 spaces)
; at line ends no semicolons
if (x > 3) { ... } if x > 3:
for (int i = 0; i < 5; i++) for i in range(5):
delay(500) time.sleep(0.5)
void loop() while True:

Pins

from machine import Pin, ADC
import time
led = Pin(25, Pin.OUT) # output
button = Pin(14, Pin.IN, Pin.PULL_UP)
knob = ADC(26) # analog input on the Pico
while True:
if button.value() == 0: # pressed (pulled up)
led.on()
else:
led.off()
print(knob.read_u16()) # 0–65535
time.sleep(0.1)

Finished this lesson?