AcademyEngineer

Microcontrollers: MicroPython on the Raspberry Pi Pico 2 W

Flash MicroPython, blink the onboard LED and read an analog sensor on a $7 board.

LevelEngineer
Time30 min
Est. cost~$28
Steps5

The Pico 2 W is a tiny microcontroller with Wi-Fi. With MicroPython you write the same Python you already know, and it runs instantly at power-on.

Video tutorial

▶ RonsTechHubWatch on YouTube ↗

Video by RonsTechHub, embedded from YouTube. The written steps below are our own and match the parts list.

Mission steps

  1. Flash MicroPython

    Hold the BOOTSEL button, plug the Pico into USB and drag the MicroPython .uf2 file for Pico 2 W onto the drive that appears.

  2. Install Thonny

    Thonny is a beginner-friendly editor. Select the MicroPython (Raspberry Pi Pico) interpreter in the bottom-right corner.

  3. Blink the onboard LED

    Run the example. Save it as main.py on the Pico to make it start automatically.

  4. Read an analog input

    Pins GP26–GP28 are analog inputs. Connect a potentiometer or soil sensor and read values from 0 to 65535.

  5. Go wireless

    Use the network module to join Wi-Fi and send readings to your Pi or Home Assistant.

Code

# main.py (MicroPython)
from machine import Pin, ADC
import time

led = Pin("LED", Pin.OUT)
sensor = ADC(26)          # GP26

while True:
    led.toggle()
    print(sensor.read_u16())
    time.sleep(0.5)

Tip: test every script on the bench before installing it in a moving machine.

Questions

MicroPython or Arduino C++?

MicroPython is faster to learn and test. C++ (Arduino IDE) runs faster and has more libraries for specialized hardware. The Pico supports both.