Everything so far was C++. Today you try MicroPython — Python for microcontrollers — on a Raspberry Pi Pico W. Same ideas (pins, loops, timing), different language, and no compile step.
No Pico W? MicroPython also runs on the ESP32 from week three; the ESP32 board page links the firmware. Only the LED pin changes (Pin(2, Pin.OUT)), and the ESP32 has no ADC 4 temperature sensor.
Install MicroPython (once)
- Download the Pico W .uf2 file from micropython.org — make sure it’s the Pico W build, not plain Pico.
- Hold the BOOTSEL button while plugging in USB. A drive called RPI-RP2 appears.
- Drag the .uf2 onto it. The board reboots into MicroPython.
- Install Thonny (thonny.org) and pick Tools → Options → Interpreter → MicroPython (Raspberry Pi Pico).
Drive doesn’t appear? See Pico RPI-RP2 drive not showing.
The REPL: talk to the board
The bottom pane in Thonny is the REPL (read–evaluate–print loop). Type a line and the board runs it at once:
>>> from machine import Pin
>>> led = Pin("LED", Pin.OUT)
>>> led.on()
The LED lights before you’ve written a program. This instant feedback is MicroPython’s best feature for learning.
Today’s program
Paste the code below into the editor and press Run. To make it start on its own at power-up, File → Save as → Raspberry Pi Pico and name it main.py.
Pin("LED", Pin.OUT)— on the Pico W the LED is wired to the Wi-Fi chip, so it has a name instead of a number.ADC(4)is a temperature sensor inside the RP2040.read_u16()returns 0–65535; the formula from the RP2040 datasheet turns the voltage into °C. It measures the chip, so it reads a few degrees warm and isn’t a room thermometer.time.sleep(1)is Python’sdelay(1000).- Indentation is the code structure. Everything inside
while True:is indented four spaces. A wrong indent is a real error, not just bad style.
C++ vs MicroPython in one line each
- C++: faster, tiny memory use, the most libraries, but compile → upload every change.
- MicroPython: type and run instantly, friendlier errors, but slower and needs more RAM.
MicroPython vs Arduino C++ goes deeper. Many FlashReady guides have both versions, e.g. DHT22 on the Pico W.
Try it
- Light the LED only when the chip is above 30 °C (hold your finger on the RP2040).
- Use
Pin(15, Pin.IN, Pin.PULL_UP)to read day 3’s button on GP15.
You’re done when…
- The RPI-RP2 drive appeared, and vanished by itself after you copied the .uf2.
- Thonny’s Shell shows the
>>>prompt, andled.on()lights the LED. - Running the program prints
tick 1 chip temperature: … Conce a second while the LED toggles. - Saved as main.py, it blinks on its own after unplugging and plugging in again — no Thonny needed.
Today's code
# Day 26 — MicroPython on the Pico W
# No wiring. Blinks the on-board LED and prints the RP2040's own temperature once a second.
# Save this as main.py on the Pico W (Thonny: File → Save as → Raspberry Pi Pico) to run it at power-up.
from machine import Pin, ADC
import time
led = Pin("LED", Pin.OUT) # on the Pico W the LED hangs off the Wi-Fi chip, so it's "LED", not 25
sensor = ADC(4) # ADC channel 4 = the temperature sensor inside the RP2040
TO_VOLTS = 3.3 / 65535 # read_u16() returns 0..65535
count = 0
while True:
led.toggle()
volts = sensor.read_u16() * TO_VOLTS
temp_c = 27 - (volts - 0.706) / 0.001721 # formula from the RP2040 datasheet
count += 1
print("tick", count, "chip temperature: {:.1f} C".format(temp_c))
time.sleep(1)# Day 26 — MicroPython on the Pico W
# No wiring. Blinks the on-board LED and prints the RP2040's own temperature once a second.
# Save this as main.py on the Pico W (Thonny: File → Save as → Raspberry Pi Pico) to run it at power-up.
from machine import Pin, ADC
import time
led = Pin("LED", Pin.OUT) # on the Pico W the LED hangs off the Wi-Fi chip, so it's "LED", not 25
sensor = ADC(4) # ADC channel 4 = the temperature sensor inside the RP2040
TO_VOLTS = 3.3 / 65535 # read_u16() returns 0..65535
count = 0
while True:
led.toggle()
volts = sensor.read_u16() * TO_VOLTS
temp_c = 27 - (volts - 0.706) / 0.001721 # formula from the RP2040 datasheet
count += 1
print("tick", count, "chip temperature: {:.1f} C".format(temp_c))
time.sleep(1)