Writing MicroPython
de:volt’s MicroPython boards use the Code tab in the Inspector. Unlike Arduino boards, there is no external compile step: write Python, press Run, and the board restarts with that program.
This page applies to:
Opening the editor
- Place a BBC micro:bit V1 or RP2040 board from the Microcontrollers section of the palette.
- Select the board.
- Open the Code tab in the Inspector.
- Pick an example or write your own program.
- Press Run to flash or load the program onto the board.
You can press Cmd/Ctrl + Enter from the editor to run the current draft.
Drafts, Run, and Restart
The editor keeps two versions of your program:
| Version | Meaning |
|---|---|
| Draft | What you are currently typing. It is session-local until you press Run. |
| Last-run program | The program stored on the board and saved with the circuit. |
Use Run when you want the board to execute your latest code. Use Restart when you want to rerun the last-run program from the beginning.
If the editor shows an edited indicator, the board is still running the previous program.
Board syntax
| Board | Import style | Common APIs |
|---|---|---|
| BBC micro:bit V1 | from microbit import * | display, Image, button_a, button_b, accelerometer, pin0, pin1, pin2 |
| RP2040 MicroPython Board | from machine import Pin, PWM, ADC | Pin, PWM, ADC, time.sleep |
micro:bit code should use pin0, pin1, and pin2. RP2040 code should use GPIO numbers, for example Pin(15, Pin.OUT).
Wiring rules
MicroPython boards in de:volt use 3.3 V logic.
- Always connect board GND to the breadboard ground rail.
- Use the board’s 3.3 V output pin for small logic rails.
- Do not feed 5 V directly into micro:bit P0/P1/P2 or RP2040 GP pins.
- Add series resistors for LEDs and transistor or driver stages for motors, relays, buzzers, and speakers.
Example: micro:bit output
from microbit import *
while True:
pin0.write_digital(1)
display.show(Image.YES)
sleep(500)
pin0.write_digital(0)
display.clear()
sleep(500)Wire P0 through a resistor and LED to GND.
Example: RP2040 input to output
from machine import Pin
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
led = Pin(15, Pin.OUT)
while True:
led.value(button.value())Wire GP14 to a button with a pull-down or pull-up pattern, and wire GP15 through a resistor and LED to GND.
Troubleshooting
| Symptom | Check |
|---|---|
| Board says unplugged or unpowered | Toggle the board USB/power control on the canvas or Inspector. |
| Code edits do nothing | Press Run; editing alone does not reflash the board. |
| Restart ignores my edits | Restart reruns the last-run program. Use Run for new code. |
| GPIO reads wrong | Confirm GND is shared and the pin is not floating. |
| LED does not light | Check polarity and add a series resistor. |
| RP2040 input from 5 V logic | Use a divider or level shifter; RP2040 pins are 3.3 V only. |
For compiled Arduino C++ sketches, use Writing Arduino Sketches instead.