Setting Up Raspberry Pi Pico on Ubuntu | Generated by AI
To use a Raspberry Pi Pico on Ubuntu, you need to set up the development environment, install necessary software, and configure tools to program the Pico. The Pico supports programming in MicroPython, C/C++, or CircuitPython. Below, I outline the steps and software required for each approach, focusing on the most common setups.
General Prerequisites
- Raspberry Pi Pico (or Pico W) with a USB cable (Micro-USB for Pico, USB-C for Pico 2).
- Ubuntu system (e.g., Ubuntu 20.04, 22.04, or later; these instructions assume a recent version like 24.04).
- Basic familiarity with the terminal.
Option 1: Programming with MicroPython
MicroPython is the most beginner-friendly way to program the Pico. It’s a lightweight Python implementation designed for microcontrollers.
Software to Install
- MicroPython Firmware
- Download the latest MicroPython UF2 firmware file for the Raspberry Pi Pico from the official MicroPython website or the Raspberry Pi Pico page.
- For Pico W or Pico 2, ensure you select the appropriate firmware (e.g.,
rp2-pico-w
for Pico W).
- Python 3
- Ubuntu typically includes Python 3 by default. Verify with:
python3 --version
- If not installed, install it:
sudo apt update sudo apt install python3 python3-pip
- Ubuntu typically includes Python 3 by default. Verify with:
- Thonny IDE (Recommended for Beginners)
- Thonny is a simple IDE for programming the Pico with MicroPython.
- Install Thonny:
sudo apt install thonny
- Alternatively, use
pip
for the latest version:pip3 install thonny
- Optional:
picotool
(for advanced management)- Useful for managing MicroPython firmware or inspecting the Pico.
- Install
picotool
:sudo apt install picotool
Setup Steps
- Install MicroPython Firmware
- Connect the Pico to your Ubuntu machine via USB while holding the BOOTSEL button (this puts the Pico in bootloader mode).
- The Pico appears as a USB storage device (e.g.,
RPI-RP2
). - Drag and drop the downloaded MicroPython
.uf2
file onto the Pico’s storage. The Pico will reboot automatically with MicroPython installed.
- Configure Thonny
- Open Thonny:
thonny
in the terminal or via the application menu. - Go to Tools > Options > Interpreter.
- Select MicroPython (Raspberry Pi Pico) as the interpreter.
- Choose the correct port (e.g.,
/dev/ttyACM0
). Runls /dev/tty*
in the terminal to identify the port if needed. - Thonny should now connect to the Pico, allowing you to write and run Python scripts.
- Open Thonny:
- Test a Program
- In Thonny, write a simple script, e.g.:
from machine import Pin led = Pin(25, Pin.OUT) # Onboard LED (GP25 for Pico) led.toggle() # Toggle LED on/off
- Click the Run button to execute the code on the Pico.
- In Thonny, write a simple script, e.g.:
- Optional: Use
picotool
- Verify the Pico’s status:
picotool info
- Ensure the Pico is connected and in bootloader mode if needed.
- Verify the Pico’s status:
Option 2: Programming with C/C++
For more advanced users, the Pico can be programmed in C/C++ using the official Pico SDK.
Software to Install
- Pico SDK and Toolchain
- Install the required tools for building C/C++ programs:
sudo apt update sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential git
- Install the required tools for building C/C++ programs:
- Pico SDK
- Clone the Pico SDK repository:
git clone -b master https://github.com/raspberrypi/pico-sdk.git cd pico-sdk git submodule update --init
- Set the
PICO_SDK_PATH
environment variable:export PICO_SDK_PATH=~/pico-sdk echo 'export PICO_SDK_PATH=~/pico-sdk' >> ~/.bashrc
- Clone the Pico SDK repository:
- Optional: Pico Examples
- Clone the Pico examples for reference:
git clone -b master https://github.com/raspberrypi/pico-examples.git
- Clone the Pico examples for reference:
- Visual Studio Code (Optional)
- For a better development experience, install VS Code:
sudo snap install code --classic
- Install the CMake Tools and C/C++ extensions in VS Code.
- For a better development experience, install VS Code:
Setup Steps
- Set Up a Project
- Create a new directory for your project, e.g.,
my-pico-project
. - Copy a sample
CMakeLists.txt
frompico-examples
or create one:cmake_minimum_required(VERSION 3.13) include($ENV{PICO_SDK_PATH}/pico_sdk_init.cmake) project(my_project C CXX ASM) pico_sdk_init() add_executable(my_project main.c) pico_add_extra_outputs(my_project) target_link_libraries(my_project pico_stdlib)
- Write a simple C program (e.g.,
main.c
):#include "pico/stdlib.h" int main() { const uint LED_PIN = 25; gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); while (true) { gpio_put(LED_PIN, 1); sleep_ms(500); gpio_put(LED_PIN, 0); sleep_ms(500); } }
- Create a new directory for your project, e.g.,
- Build and Flash
- Navigate to your project directory:
cd my-pico-project mkdir build && cd build cmake .. make
- This generates a
.uf2
file (e.g.,my_project.uf2
). - Hold the BOOTSEL button on the Pico, connect it via USB, and copy the
.uf2
file to the Pico’s storage:cp my_project.uf2 /media/$USER/RPI-RP2/
- Navigate to your project directory:
- Debugging (Optional)
- Install
openocd
for debugging:sudo apt install openocd
- Use a debugger (e.g., another Pico as a debug probe) and run:
openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg
- Install
Option 3: Programming with CircuitPython
CircuitPython is another Python-based option, similar to MicroPython but with a focus on Adafruit’s ecosystem.
Software to Install
- CircuitPython Firmware
- Download the CircuitPython UF2 file for the Pico from the Adafruit CircuitPython website.
- For Pico W or Pico 2, select the appropriate firmware.
- Python 3 and Tools
- Same as for MicroPython (Python 3, Thonny, etc.).
Setup Steps
- Install CircuitPython Firmware
- Similar to MicroPython: hold BOOTSEL, connect the Pico, and copy the CircuitPython
.uf2
file to the Pico’s storage. - The Pico reboots as a USB drive named
CIRCUITPY
.
- Similar to MicroPython: hold BOOTSEL, connect the Pico, and copy the CircuitPython
- Program with Thonny or a Text Editor
- Use Thonny as described in the MicroPython section, selecting CircuitPython as the interpreter.
- Alternatively, edit
code.py
directly on theCIRCUITPY
drive using any text editor. - Example
code.py
:import board import digitalio import time led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT while True: led.value = True time.sleep(0.5) led.value = False time.sleep(0.5)
Additional Notes
- Permissions: If the Pico’s port (e.g.,
/dev/ttyACM0
) isn’t accessible, add your user to thedialout
group:sudo usermod -a -G dialout $USER
Log out and back in to apply.
-
Pico W Considerations: For the Pico W, ensure you use firmware specific to it (e.g., MicroPython with Wi-Fi support). Wi-Fi programming requires additional libraries, like
network
for MicroPython. -
Pico 2: The newer Raspberry Pi Pico 2 (with RP2350) may require updated firmware or SDK versions. Check the official Raspberry Pi documentation for compatibility.
- Troubleshooting:
- If the Pico doesn’t appear in bootloader mode, double-check the USB cable (it must support data, not just power).
- Run
lsusb
ordmesg
to confirm the Pico is detected by Ubuntu. - For C/C++ builds, ensure the Pico SDK path is correctly set.
Recommended Approach
- For beginners: Use MicroPython with Thonny for simplicity and quick setup.
- For advanced users: Use C/C++ with the Pico SDK for performance and low-level control.
- For Adafruit ecosystem users: Use CircuitPython if you plan to use Adafruit libraries or hardware.
If you have a specific project or programming preference, let me know, and I can tailor the instructions further!