Parts ReferenceWriting MicroPython

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

  1. Place a BBC micro:bit V1 or RP2040 board from the Microcontrollers section of the palette.
  2. Select the board.
  3. Open the Code tab in the Inspector.
  4. Pick an example or write your own program.
  5. 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:

VersionMeaning
DraftWhat you are currently typing. It is session-local until you press Run.
Last-run programThe 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

BoardImport styleCommon APIs
BBC micro:bit V1from microbit import *display, Image, button_a, button_b, accelerometer, pin0, pin1, pin2
RP2040 MicroPython Boardfrom machine import Pin, PWM, ADCPin, 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

SymptomCheck
Board says unplugged or unpoweredToggle the board USB/power control on the canvas or Inspector.
Code edits do nothingPress Run; editing alone does not reflash the board.
Restart ignores my editsRestart reruns the last-run program. Use Run for new code.
GPIO reads wrongConfirm GND is shared and the pin is not floating.
LED does not lightCheck polarity and add a series resistor.
RP2040 input from 5 V logicUse a divider or level shifter; RP2040 pins are 3.3 V only.

For compiled Arduino C++ sketches, use Writing Arduino Sketches instead.