Learn, Implement and Share

Introduction

Raspberry Pi Pico is a tiny, fast, and versatile board built using RP2040, a brand new microcontroller chip designed by Raspberry Pi. RP2040 features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM and support for up to 16MB of off-chip Flash. A wide range of flexible I/O options includes I2C, SPI, and uniquely Programmable I/O (PIO).

In this article, we will connect a Raspberry Pi Pico to a computer and learn how to build the SDK and flash an example program in it using C/C++. We will also see how to write the code in Visual Studio Code IDE.

What You Will Learn

  • How to load the C/C++ firmware onto a Raspberry Pi Pico?
  • How to program a Raspberry Pi Pico using C/C++?
  • How to compile and build C/C++ SDK for Raspberry Pi Pico?
  • How to compile and build C/C++ programs for Raspberry Pi Pico in Windows PC?

Prerequisite Hardware

  • Raspberry Pi Pico
  • Micro USB Cable
  • Windows 10 Computer

Software Downloads

Download the Windows Executable Installer for each of the mentioned software in the list. The attached links will take you to the respective download pages.

Software Installation

Follow the detailed installation guide in the following sequence and make sure not to miss any steps, especially match the checkboxes as shown in the below screenshots.

Installing ARM GCC Compiler

  • Open the installer
  • Use the default installation folder
  • Add path to environment variable and add registry information

Installing CMAKE

  • Open the installer
  • Use the default installation folder
  • Add CMake to the system PATH for all the users

Installing Build Tools for Visual Studio 2019

  • Open the installer
  • Use the default installation folder
  • Select the C++ Build Tools only
  • Don’t change any other selection

Installing Python

  • Open the installer
  • Use the default installation folder
  • Use Customize installation option
  • Add Python 3.9 to PATH/environment variable
  • Install launcher for all users
  • Precompile standard library
  • Disable path length limit

Installing Git

  • Open the installer
  • Use the default installation folder
  • Select allow Git to be used from third-party tools
  • Select checkout as-is, commit as-is
  • Select use Windows’ default console window
  • Select Enable experimental support for pseudo consoles

Installing Visual Studio Code

  • Open the installer
  • Use the default installation folder
  • Add to PATH

Raspberry Pi Pico SDK

The Raspberry Pi Pico SDK (Software Development Kit), provides the headers, libraries, and build system necessary to write programs for the RP2040 based devices such as the Raspberry Pi Pico in C, C++, or assembly language.

The SDK provides higher-level libraries for dealing with timers, USB, synchronization, and multi-core programming, along with additional high-level functionality built using PIO. The SDK also has examples to help get started with basic functionality and hardware interfacing.

The Build System

The Raspberry Pi Pico SDK uses CMake to manage the build. CMake is widely supported by IDEs (Integrated Development Environments) and allows a simple specification of the build (via CMakeLists.txt files), from which CMake can generate a build system ( make ) customized for the platform. CMakeList files used in Raspberry Pi Pico SDK and projects define how applications are configured and built.

The SDK builds an executable that is bare metal and runs independently on RP2040.

Download the Raspberry Pi Pico SDK and Examples

Create a new folder and open Command Prompt (cmd.exe) at that location. Run the below commands in a sequence.

D:\RP2040> git clone -b master https://github.com/raspberrypi/pico-sdk.git
D:\RP2040> cd pico-sdk
D:\RP2040\pico-sdk> git submodule update --init
D:\RP2040\pico-sdk> cd ..
D:\RP2040> git clone -b master https://github.com/raspberrypi/pico-examples.git
Step 5 : Download the Raspberry Pi Pico SDK and Examples
Step 5 : Download the Raspberry Pi Pico SDK and Examples : Folder view After SDK Download

Building the Raspberry Pi Pico SDK and Examples

  • Open the Visual Studio 2019 Developer Command Prompt
  • Navigate to the SDK download location
  • Add the environment variable with the path of the pico-sdk
  • Close the Command Prompt
 ** Visual Studio 2019 Developer Command Prompt v16.9.6
 ** Copyright (c) 2021 Microsoft Corporation
 
 C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools>cd /D D:RP2040
 D:\RP2040>setx PICO_SDK_PATH "D:\RP2040\pico-sdk"
Add Environment Variable for Raspberry Pi Pico SDK
Add Environment Variable for Raspberry Pi Pico SDK
  • Open a new Visual Studio 2019 Developer Command Prompt
  • Navigate to the SDK download location
  • Navigate to pico-examples
  • Create a build folder
  • Navigate to build
 ** Visual Studio 2019 Developer Command Prompt v16.9.6
 ** Copyright (c) 2021 Microsoft Corporation
 
 C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools>cd /D D:RP2040
 D:\RP2040>cd pico-examples
 D:\RP2040\pico-examples>mkdir build
 D:\RP2040\pico-examples>cd build
 D:\RP2040\pico-examples\build>
Creating Build Folder for Raspbery Pi Pico SDK and Examples
Creating Build Folder for Raspbery Pi Pico SDK and Examples
  • Run cmake -G "NMake Makefiles" ..
  • Run nmake
  • This will produce ELF, bin, and uf2 file for each example project
D:\RP2040\pico-examples\build>cmake -G "NMake Makefiles" ..
D:\RP2040\pico-examples\build>nmake

Setting Up Visual Studio Code IDE

  • Open a new Visual Studio 2019 Developer Command Prompt
  • Run the below command to open Visual Studio Code
D:\RP2040\pico-examples\build>code
Opening Visual Studio Code from Developer Command Prompt
Opening Visual Studio Code from Developer Command Prompt
Visual Studio Code for RP Pico
Visual Studio Code
  • Open Extensions Ctrl+Shift+X
  • Install CMake Tools Extension by Microsoft
  • Install C/C++ Extension by Microsoft
  • Open CMake Tools Extension Settings
  • Scroll Down
  • Add Cmake: Configure Environment Item as PICO_SDK_PATH
  • Add Cmake: Configure Environment Value as D:\RP2040\pico-sdk
  • Scroll Down
  • Add Cmake: Generator as NMake Makefiles
  • Add Folder pico-examples to Visual Studio Code
  • Visual Studio Code will scan for Kits
  • Select ‘Yes’ when asked: Would you like to configure project pico-examples?
  • Select ‘Yes’ if you like to configure the project upon opening
  • Click CMake in the bottom bar to select the Kit for pico-examples
  • Select Debug / Release based on your preference
  • Visual Studio Code will save all file and configure the project
  • Click on Build to build all examples
  • If no error Build will finish with exit code 0
Successful Build Output of Blink Example Project of Raspberry Pi Pico SDK and Examples
Successful Build Output of Blink Example Project of Raspberry Pi Pico SDK and Examples
  • Navigate to the folder pico-examples\build\blink
  • Successful Build will generate the above artifacts

Flashing the Raspberry Pi Pico

  • Push and hold the BOOTSEL button (white colour near USB port)
  • Plug the Pico into the USB port of the Windows 10 Computer
  • Raspberry Pi Pico will mount as a Mass Storage Device called RPI-RP2
  • Drag and Drop or Copy and Paste the blink.uf2 UF2 Binary into the RPI-RP2 Drive
  • Pico will Reboot automatically, and the on-board Green LED should start Blinking


Crazy Engineer

MAKER - ENGINEER - YOUTUBER

27 Comments

Islam Sameh · April 3, 2022 at 9:32 pm

Perfect guide. I hope in future to make guide to integrate tinyusb with MicroPaython or CircuitPython.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.