Building MicroPython UF2 from C Source Code for Raspberry Pi Pico W



Building MicroPython UF2 from C Source Code for Raspberry Pi Pico W


Introduction

This guide walks through building the MicroPython firmware from source for the Raspberry Pi Pico W on Raspberry Pi OS. This environment provides all necessary tools for a smooth process, and the steps ensure that everything is done correctly. We’ll also walk through copying the firmware to the Pico, and safely disconnecting the Pico once testing is complete.


Preparing Your Environment

You’ll start by setting up the environment on Raspberry Pi OS, ensuring all dependencies are installed and that the necessary directories are created.


Step 1: Clone the MicroPython Repositories

Start by working in your home directory (/home/pi), but if your user directory differs, adjust paths as needed.


$ pwd
/home/pi


Create and navigate into the project folder:


$ mkdir pico_w
$ cd pico_w


Clone the MicroPython and MicroPython-lib repositories:


git clone https://github.com/micropython/micropython.git --branch master



git clone https://github.com/micropython/micropython-lib.git --branch master

Installing Dependencies

Make sure you install the necessary dependencies before proceeding. All these commands are run from your home directory (/home/pi).


Step 2: Install CMake

If you encounter an error stating cmake isn’t installed, install it with:


$ sudo apt-get install cmake


Step 3: Install the ARM GCC Toolchain

You'll need the ARM GCC toolchain to cross-compile MicroPython for the Pico W. Install it with:


$ sudo apt-get install gcc-arm-none-eabi


Initializing Submodules and Building the Firmware

With dependencies installed, you can now initialize submodules and build the firmware.


Step 4: Initialize Submodules

To include wireless functionality (like the cyw43-driver), you need to initialize the required submodules:


$ cd micropython/portsrp2
$ make BOARD=RPI_PICO_W submodules


Step 5: Build the Firmware

After initializing submodules, build the firmware with this command:


$ make BOARD=RPI_PICO_W


Step 6: Verify the Build Location

Once the build is complete, verify that the firmware.uf2 file is located in the correct directory:


$ pwd
$ /home/pi/pico_w/micropython/ports/rp2/build-RPI_PICO_W


Flashing the UF2 to the Pico W

Now that the firmware is built, it’s time to copy it to the Pico W.


Step 7: Flash the Firmware

Now let's load the firmware onto the Pico W:


1. Put the Pico W into BOOTSEL mode:

- Hold down the BOOTSEL button while plugging the Pico W into your computer.
- The Pico W will appear as a mass storage device named RPI-RP2.

2.  Copy the firmware file from the build directory to the mounted Pico W:  


cp /home/pi/pico_w/micropython/ports/rp2/build-RPI_PICO_W/firmware.uf2 /media/pi/RPI-RP2


Verifying the Installation in Thonny

Let’s confirm the firmware installation using Thonny.


Step 8: Open Thonny and Test MicroPython

1. Launch Thonny. If you don’t already have Thonny installed, you can download it here.

2. In Thonny, go to Tools > Options > Interpreter.

3. Select MicroPython (Raspberry Pi Pico) and choose the appropriate port (Thonny should detect your Pico W automatically).


Now, open the REPL in Thonny:


4. Click the Stop/Restart Backend button (the red stop sign) to ensure the REPL is connected.

5. In the REPL at the bottom of Thonny, enter the following to confirm that MicroPython is running:


>>> 2 + 2
4


Step 9: Flash an LED (Verification)

To further confirm the installation, let's flash the onboard LED with this MicroPython script:


import machine
import time

# Onboard LED is connected to Pin 25 on Pico W
led = machine.Pin("LED", machine.Pin.OUT)

while True:
    led.on()       # Turn the LED on
    time.sleep(1)  # Wait for 1 second
    led.off()      # Turn the LED off
    time.sleep(1)  # Wait for 1 second


Save the script on the Pico W and run it. If the LED starts flashing, you’ve successfully flashed the firmware!


Exiting Thonny and Safely Removing the Pico


Step 10: Quit Thonny

To safely quit Thonny and avoid corrupting files:


- Click File > Quit or press Ctrl + Q to close the Thonny session.


Step 11: Unmount and Remove the Pico W


1. Unmount the Pico W to ensure no data is lost:


$ sudo umount /media/pi/RPI-RP2


2. Safely remove the Pico by unplugging it from your computer.


Conclusion

By following these steps, you’ve built and flashed MicroPython firmware to the Raspberry Pi Pico W. From building the environment to verifying with Thonny and safely removing the device, you're all set! Let me know if you have any more questions or suggestions. 😊✨



Image:  Raspberry Pi

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process