Get started with Raspberry Pi Pico-series and VS Code
In the latest issue of The MagPi, Raspberry Pi Documentation Lead Nate Contino shows you how to attach a Raspberry Pi Pico-series device and start development with the new VS Code extension.
The following tutorial assumes that you are using a Pico-series device; some details may differ if you use a different Raspberry Pi microcontroller-based board. Pico-series devices are built around microcontrollers designed by Raspberry Pi itself. Development on the boards is fully supported with both a C/C++ SDK, and an official MicroPython port. This article talks about how to get started with the SDK, and walks you through how to build, install, and work with the SDK toolchain.
To install Visual Studio Code (known as VS Code for short) on Raspberry Pi OS or Linux, run the following commands:
$ sudo apt update
$ sudo apt install code
On macOS and Windows, you can install VS Code from magpi.cc/vscode. On macOS, you can also install VS Code with brew using the following command:
$ brew install --cask visual-studio-code
The Raspberry Pi Pico VS Code extension helps you create, develop, run, and debug projects in Visual Studio Code. It includes a project generator with many templating options, automatic toolchain management, one-click project compilation, and offline documentation of the Pico SDK. The VS Code extension supports all Raspberry Pi Pico-series devices.
Install dependencies
On Raspberry Pi OS and Windows no dependencies are needed.
Most Linux distributions come preconfigured with all of the dependencies needed to run the extension. However, some distributions may require additional dependencies.
The extension requires the following:
- Python 3.9 or later
- Git
- Tar
- A native C and C++ compiler (the extension supports GCC)
You can install these with:
$ sudo apt install python3 git tar build-essential
On macOS
To install all requirements for the extension on macOS, run the following command:
$ xcode-select --install
This installs the following dependencies:
- Git
- Tar
- A native C and C++ compiler (the extension supports GCC and Clang)
Install the extension
You can find the extension in the VS Code Extensions Marketplace. Search for the Raspberry Pi Pico extension, published by Raspberry Pi. Click the Install button to add it to VS Code.
You can find the store entry at magpi.cc/vscodeext. You can find the extension source code and release downloads at magpi.cc/picovscodegit. When installation completes, check the Activity sidebar (by default, on the left side of VS Code). If installation was successful, a new sidebar section appears with a Raspberry Pi Pico icon, labelled “Raspberry Pi Pico Project”.
Load and debug a project
The VS Code extension can create projects based on the examples provided by Pico Examples. For an example, we’ll walk you through how to create a project that blinks the LED on your Pico-series device:
- In the VS Code left sidebar, select the Raspberry Pi Pico icon, labelled Raspberry Pi Pico Project.
- Select New Project from Examples.
- In the Name field, select the blink example.
- Choose the board type that matches your device.
- Specify a folder where the extension can generate files. VS Code will create the new project in a sub-folder of the selected folder.
- 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 five to ten 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.
The CMake Tools extension may display some notifications at this point. Ignore and close them.
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. 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.
Compile and run blink
To run the blink example:
- Hold down the BOOTSEL button on your Pico-series device while plugging it into your development device using a Micro USB cable to force it into USB Mass Storage Mode.
- 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 LED should blink twice every second.
Make a code change and re-run
To check that everything is working correctly, click on the blink.c file in VS Code. Navigate to the definition of LED_DELAY_MS at the top of the code:
#ifndef LED_DELAY_MS
#define LED_DELAY_MS 250
#endif LED_DELAY_MS
Change the 250 (in ms, a quarter of a second) to 100 (a tenth of a second): #ifndef LED_DELAY_MS #define LED_DELAY_MS 100 #endif LED_DELAY_MS
- Disconnect your device, then reconnect while holding the BOOTSEL button just as you did before.
- 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 LED should flash faster, five times every second.
Top tip
Read the online guide
This tutorial also features in the Raspberry Pi Datasheet: Getting started with Pico. It also features information on using Raspberry Pi’s Debug Probe.
The post Get started with Raspberry Pi Pico-series and VS Code appeared first on Raspberry Pi.