Skip to content

Tutorial for Pico 2

Preface

To begin this journey, I ordered 3 Raspberry Pi Pico 2 boards through Amazon. While I was waiting, I obtained the documentation that I would need to deep dive into the use of these new boards. You can find the list at the end of this document. I studied the datasheets for the RP2350 and the Pico 2 board. I also began studying the Getting started with Raspberry Pi Pico-series Microcontrollers document. Surprisingly, I received Pico 2 boards after 4 days. I soldered the headers and debug connectors on two boards and have the third one as a ready spare.

Note: I highly recommend taking a look the datasheets and familiarizing yourself with the table of contents and read the first chapter of the datasheets.

My initial goal was to work through the Getting started with Raspberry Pi Pico-series Microcontrollers document to become familiar with the board and the recommended software. I have a Windows 11 computer and a Raspberry Pi 5 with the Raspberry Pi OS. My recommendation is to follow the steps in the documentation to learn how to install the software and upload code to the Pico 2. I have a Raspberry Pi Debug Probe, so I intended to follow the document to experience the debugging process.

The learning experience was exciting at first, but I ran into a problem with the Pico 2 debugging process with the Raspberry Pi 5. After installing VS Code and the extensions per the document, OpenOCD could not find the rp2350.cfg file. To troubleshoot the problem, I tried the same process with the Pico W and it worked after I had the wires connected properly :) So I tried the Pico 2 again and still had the same problem. My next troubleshooting step was to try the Pico W and Pico 2 in Windows; both worked flawlessly and I was able to step through the blink program using the debug probe. I spent days trying to make the Pico 2 debugging work with the Raspberry Pi 5. I searched the Internet to try and find the solution, but never had success. I was able to flash the boards without a problem and did so countless times; I became very proficient at the process. I almost decided to move on without solving the debugging problem, but I persisted and discovered “Appendix C: Manual toolchain setup” in the document. I completely reloaded the Raspberry Pi OS to start from scratch and began again with Getting Started document. Right at the beginning there is a Tip: “The main method covered in this book uses a VS Code extension to make your life easy. If you would like to set up your development environment manually, see Manually Configure your Environment.” Just to be clear, my life was not easy, but I want to make yours easier. The Tip has a link to “Manually Configure your Environment”. Now you can do that if you want to, but I was at my wits end and decided to scroll up and follow “Configure your environment via Script”. My environment was setup for me while I watched the script do the work. Afterwards, I was able to flash and debug the Pico W and the Pico 2 with no problems.

I learned a lot though that journey and decided to get assistance from AI to make a tutorial as discussed with Nicolas De Coster. This turned into a bigger project than I anticipated due to the troubleshooting I had to do. I don’t regret a minute of it! It was very rewarding to finally make it work.

To organize a tutorial for the Raspberry Pi Pico 2, I provided the following prompt on an iPhone to the Chat Smith app using GPT-4o: “I want to develop a tutorial about the Raspberry Pi Pico 2 with a view toward using various programming languages. Using Go, Rust, Python, and Javascript”

The response was “That sounds like a fantastic and ambitious project! Here’s a structured outline to help you develop your tutorial:

Introduction

  • Brief Overview of Raspberry Pi Pico 2

The Raspberry Pi Pico 2 is a highly versatile and compact microcontroller. It boasts impressive specifications, such as a dual-core ARM Cortex-M0+ processor, with a processing speed of up to 133 MHz. It comes equipped with 264KB of SRAM and 2MB of onboard flash memory, providing robust storage and processing capabilities for various projects.

The Pico 2 features a rich set of functionalities that include, but are not limited to, multiple I/O options like GPIO, SPI, I2C, UART, and ADC. These features make it an ideal choice for a wide range of applications, from simple hobbyist projects to complex industrial automation tasks. It also supports popular programming languages such as MicroPython and C/C++, offering flexibility to developers of all skill levels.

  • Importance of Learning Multiple Programming Languages

In the modern tech landscape, the ability to code in multiple programming languages is invaluable. Each language has its own strengths and is best suited to different types of tasks. For instance, while Python is excellent for scripting and automation, C++ provides fine-grained control over system resources, making it ideal for systems programming.

Learning multiple languages enhances problem-solving skills and opens up greater career opportunities. It also allows developers to choose the most efficient tools for their specific needs, promoting innovation and increasing productivity. By mastering various languages, one can easily adapt to new challenges and technologies, ensuring long-term success in the ever-evolving field of tech.

Prerequisites

Hardware Requirements

Before you start working with the Raspberry Pi Pico 2, ensure you have the following hardware components:

  • Raspberry Pi Pico 2: The main microcontroller board.
  • Micro-USB Cable: For power supply and data communication between your Pico 2 and computer.
  • Breadboard: Optional, but useful for prototyping circuits.
  • Jumper Wires: For making connections between the Pico 2 and other components.
  • Sensors/Actuators: Depending on your project, you might need various sensors (like temperature, humidity) or actuators (like LEDs, motors).
  • Power Supply: If your project requires more power than what the Micro-USB can supply, you might need an external power source.

Software Requirements

  • Installing IDEs and Toolchains for Each Language

For optimal development with the Raspberry Pi Pico 2, you’ll need to install specific software tools and IDEs. Here’s a detailed guide for setting them up:

  • Operating System: Ensure you’re running a compatible OS like Windows, macOS, or Linux.

  • Installing MicroPython IDE

    • Thonny IDE: A beginner-friendly Python IDE.
      • Step 1. Download and Install the latest version of Thonny editor according to your operating system from the official website.
      • Step 2. Open Thonny.
      • Step 3. Click “Tools–> Options…” to open the settings.
      • Step 4. Choose the “Interpreter” tab and select the interpreter as “MicroPython(Raspberry Pi Pico)” and the port as “Try to detect port automatically”.
      • Step 5. Press and hold the “BOOTSEL” button and then connect the Pico 2 to the PC through the Micro-USB cable. If it works well, there is a “RP2350” drive shown on the PC.
      • Step 6. Click Install or update MicroPython. It will then automatically search for the device and display it on the Target Volume. You should see RP2350 (Drive Letter) and model Raspberry Pi RP2350.
      • Step 7. Select MicroPython family “RP2”.
      • Step 8. Select variant “Raspberry Pi - Pico2” and the version should be the latest firmware.
      • Step 9. Click Install. If everything works correctly, the “RP2350” drive should disappear.
      • Step 10. Click Close and Click OK. You should see a >>> prompt in the Shell section.
  • Installing C/C++ Toolchain

    • VS Code: A powerful IDE for a variety of languages, including C/C++.

      • Download and install Visual Studio Code from VS Code official website.
      • Install the necessary extensions:
        • C/C++ extension by Microsoft.
        • CMake Tools for project configuration.
        • MicroPico (formerly Pico-W-Go) for Pico development.
    • Installing Pico SDK:

      • Follow the official Raspberry Pi documentation to download and set up the Pico SDK.
      • Clone the SDK repository: git clone -b master https://github.com/raspberrypi/pico-sdk.git.
      • Set up your environment variables to integrate the SDK with your build system (usually done via .bashrc or .zshrc).
  • Arduino IDE (Optional, if you prefer Arduino framework)

    • Download from the Arduino official website.
    • Add Raspberry Pi Pico support by going to File > Preferences and adding the URL for the Pico board in the Additional Boards Manager URLs.
    • Go to Tools > Board > Boards Manager and install the Raspberry Pi Pico board.

By ensuring you have these hardware components and software tools ready, you’ll be well-prepared to start your projects with the Raspberry Pi Pico 2. This setup provides a comprehensive environment for programming in both MicroPython and C/C++, catering to a wide range of project requirements.

Setting Up Raspberry Pi Pico 2

Connecting to your Computer

  • USB Connection

    • Get the right cable: Ensure you have a micro-USB to USB cable for connecting the Pico to your computer.
    • Connect the Pico 2: Insert the micro-USB end into the Pico 2 and the USB end into your computer.
    • Boot the Pico 2: Hold the BOOTSEL button on the Pico 2 while plugging it into your computer. Release the button once connected. The Pico will enter USB mass storage mode; there is a “RP2350” drive shown on the PC.
  • Flashing the MicroPython firmware

    • Download MicroPython UF2 file: Visit the MicroPython download page and download the latest UF2 firmware for the Raspberry Pi Pico 2.
    • Copy the file to Pico: With the Pico 2 appearing as a “RP2350” storage device on your computer, drag and drop the downloaded UF2 file onto it. The Pico 2 will reboot automatically.
    • Verify installation: After reboot, the Pico 2 will no longer appear as a storage device. This indicates that MicroPython is successfully installed.

Now your Raspberry Pi Pico 2 is connected to your computer and ready to run MicroPython.

Basic Interfacing

  • Blinking an LED

  • MicroPython Code:

    • Open Thonny: Launch the Thonny IDE, which supports MicroPython and is great for programming the Pico 2.
    • Write the Code: Type the following code to make the on-board LED blink.
import machine
import utime

led = machine.Pin("LED", machine.Pin.OUT)

while True:
    led.value(1)  # Turn the LED on
    utime.sleep(1)  # Wait for 1 second
    led.value(0)  # Turn the LED off
    utime.sleep(1)  # Wait for 1 second
  • Upload the Code:
    • Save and Run: Save the file as main.py on the Raspberry Pi Pico 2 using Thonny’s “Save As” feature. This will ensure the code runs automatically each time the Pico is powered on.
    • Run the Script: Click the “Run” button in Thonny. The on-board LED should start blinking, turning on and off every second.

Now you have successfully interfaced with the Raspberry Pi Pico 2 and programmed it to blink the on-board LED. This basic interfacing project helps you get started with more complex circuits and functionalities.

Programming in Python

Introduction to MicroPython:

  • Installing MicroPython on Pico 2

    • Download the Firmware:

    • Upload to Pico 2:

      • Connect your Pico 2 to your computer while holding the BOOTSEL button.
      • Release the button once connected. Your Pico 2 should appear as a USB mass storage device.
      • Drag and drop the downloaded MicroPython UF2 file onto the Pico 2 USB storage. The Pico 2 will reboot and be ready to run MicroPython.
  • Writing your first “Hello World”

    • Install Thonny:

      • Download and install the Thonny IDE if you haven’t already.
    • Configure Thonny for Pico 2:

      • Open Thonny, go to Tools > Options > Interpreter.
      • Select MicroPython (Raspberry Pi Pico) in the interpreter dropdown and choose the correct COM port for Pico 2.
  • Code Example:

  print("Hello, World!")
  • Save and Run:
    • Save the script and click Run. Your Pico will output “Hello, World!” to Thonny’s console.

Interfacing with GPIO Pins

  • Basic Projects:
  • LED Blink

    • Materials Needed

      • Raspberry Pi Pico 2
      • Breadboard
      • 1 LED (any color)
      • 1 resistor (330 ohms)
      • Jumper wires
    • Wire Connections:

      • LED Setup: Place the LED on the breadboard. The longer leg (anode) connects to the GPIO pin, and the shorter leg (cathode) connects to the ground (GND) via a resistor.
      • Resistor Connection: Connect the 330Ω resistor to the shorter leg of the LED and then to a GND rail on the breadboard.
      • Pico 2 Setup: Connect a jumper wire from the longer leg (anode) of the LED to a GPIO pin on the Raspberry Pi Pico 2. For this example, use GPIO 26.
    • Code:

import machine
import utime

led = machine.Pin(26, machine.Pin.OUT)

while True:
    led.value(1)  # LED on
    utime.sleep(1)  # Wait for 1 second
    led.value(0)  # LED off
    utime.sleep(1)  # Wait for 1 second
  • Save and Run:

    • Save the script and click Run. Your Pico 2 will blink the external LED, turning it on and off every second.
  • Troubleshooting:

    • Check Connections: Ensure that the LED and resistor are connected correctly.
    • Verify GPIO Pin: Make sure the GPIO pin number in the code matches the physical pin connected to the LED.
  • Reading a Button Press

    • Materials:

      • Raspberry Pi Pico 2
      • Breadboard
      • Push button
      • 10kΩ-15kΩ pull-down resistor
      • Jumper wires
    • Connections:

      • Connect one side of the button to 3.3V.
      • Connect the other side to GPIO15.
      • Use a 10kΩ resistor between GPIO15 and GND to keep the pin low by default.
    • Code:

import machine
import utime

button = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)

while True:
    if button.value() == 1:
        print("Button pressed!")
        utime.sleep(0.2)  # Debounce delay
  • Save and Run:

    • Save the script and click Run. Your Pico 2 will output “Button pressed!” to Thonny’s console every time you press the push button.
  • Troubleshooting:

    • Check Connections: Ensure that the push button and resistor are connected correctly.
    • Verify GPIO Pin: Make sure the GPIO pin number in the code matches the physical pin connected to the push button.

These introductory exercises and projects will get you started with the Raspberry Pi Pico 2, making you comfortable with MicroPython and external hardware interfacing.

Programming in C/C++ in VS Code

  • Continuing with our external hardware connections we can program the Pico 2 to blink the LED with the button press.
  • Step 1. In the VS Code left sidebar, select the Raspberry Pi Pico icon, labelled “Raspberry Pi Pico Project”.
  • Step 2. Select New Project from Examples.
  • Step 3. In the Name field, select the blink example.
  • Step 4. Choose the board type that matches your device.
  • Step 5. Specify a folder where the extension can generate files. VS Code will create the new project in a sub-folder of the selected folder.
  • Step 6. Click Create to create the project.

The extension will now download the SDK and the toolchain, install them locally, and generate the new project. The first project may take 5-10 minutes to install the toolchain. VS Code will ask you whether you trust the authors because we’ve automatically generated the .vscode directory for you. Select yes.

On the left Explorer sidebar in VS Code, you should now see a list of files. Open blink.c to view the blink example source code in the main window.

Replace the example code with the following Google AI provided code:

#include "pico/stdlib.h"

int main() {
    const uint LED_PIN = 26; // External LED pin

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);

    while (true) {
        gpio_put(LED_PIN, 1); // Turn LED on
        sleep_ms(500);       // Wait for 500 milliseconds
        gpio_put(LED_PIN, 0); // Turn LED off
        sleep_ms(500);       // Wait for 500 milliseconds
    }

    return 0;
}

The Raspberry Pi Pico extension adds some capabilities to the status bar at the bottom right of the screen.

  • Compile - Compiles the sources and builds the target UF2 file. You can copy this binary onto your device to program it.

  • Run - Finds a connected device, flashes the code into it, and runs that code.

The extension sidebar also contains some quick access functions. Click on the Pico icon in the side menu and you’ll see Compile Project.

Hit Compile Project and a terminal tab will open at the bottom of the screen displaying the compilation progress.

Correct any errors/bugs you may find.

To run the blink example:

  • Step 1. Hold down the BOOTSEL button on your Pico 2 while plugging it into your development device using a micro USB cable to force it into USB Mass Storage Mode.
  • Step 2. Press the Run button in the status bar or the Run Project button in the sidebar.

You should see the terminal tab at the bottom of the window open. It will display information concerning the upload of the code. Once the code uploads, the device will reboot, and you should see the following output:

“The device was rebooted to start the application.”

Your blink code is now running. If you look at your device, the external LED should blink twice every second.

  • Step 1. In the VS Code left sidebar, select the Raspberry Pi Pico icon, labelled “Raspberry Pi Pico Project”.
  • Step 2. Select New Project from Examples.
  • Step 3. In the Name field, select the picoboard_button example.
  • Step 4. Choose the board type that matches your device.
  • Step 5. Specify a folder where the extension can generate files. VS Code will create the new project in a sub-folder of the selected folder.
  • Step 6. Click Create to create the project.

The extension will now download the SDK and the toolchain, install them locally, and generate the new project. The first project may take 5-10 minutes to install the toolchain. VS Code will ask you whether you trust the authors because we’ve automatically generated the .vscode directory for you. Select yes.

On the left Explorer sidebar in VS Code, you should now see a list of files. Open button.c to view the picoboard_button example source code in the main window.

Replace the example code with the following code provided by GurgleApps:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"

#define BUTTON_PIN 15
#define LED_PIN 26

int main() {
    stdio_init_all();
    printf("GurgleApps.com Button Test\n");
    gpio_init(BUTTON_PIN);
    gpio_set_dir(BUTTON_PIN, GPIO_IN);
    gpio_pull_down(BUTTON_PIN);
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    while (true) {
        if (!gpio_get(BUTTON_PIN)) {
            gpio_put(LED_PIN, 0);
            // printf("Button Not Pressed\n"); //This was added for debugging
        } else {
            gpio_put(LED_PIN, 1);
            printf("Button Pressed\n"); //This was added for debugging and can only be seen in the Serial Monitor of a PicoProbe/UART in VS Code
        }
        sleep_ms(150);
    }
}

The Raspberry Pi Pico extension adds some capabilities to the status bar at the bottom right of the screen.

  • Compile - Compiles the sources and builds the target UF2 file. You can copy this binary onto your device to program it.
  • Run - Finds a connected device, flashes the code into it, and runs that code.

The extension sidebar also contains some quick access functions. Click on the Pico icon in the side menu and you’ll see Compile Project.

Hit Compile Project and a terminal tab will open at the bottom of the screen displaying the compilation progress.

Correct any errors/bugs you may find.

To run the picoboard_button example:

  • Step 1. Hold down the BOOTSEL button on your Pico 2 while plugging it into your development device using a micro USB cable to force it into USB Mass Storage Mode.
  • Step 2. Press the Run button in the status bar or the Run Project button in the sidebar.

You should see the terminal tab at the bottom of the window open. It will display information concerning the upload of the code. Once the code uploads, the device will reboot, and you should see the following output:

“The device was rebooted to start the application.”

Your picoboard_button code is now running. If you look at your device and press the external button, the external LED should blink with every button press.

Appendix

  • Useful Links Pico-series Microcontrollers: I found the following documents near the bottom of the page.
    • RP2350 Datasheet
    • Raspberry Pi Pico 2 Datasheet
    • Getting started with Raspberry Pi Pico-series Microcontrollers
    • Raspberry Pi Pico W Datasheet
    • Raspberry Pi Pico C/C++ SDK
    • Raspberry Pi Pico Python SDK