Getting started with Raspberry Pi Pico using c/c++

Chapter 1. Quick Pico Setup

If you are developing for Raspberry Pi Pico on the Raspberry Pi 4B, or the Raspberry Pi 400, most of the installation steps in this Getting Started guide can be skipped by running the setup script.

Download able PDF link is given at the end.

NOTE:

This setup script requires approximately 2.5GB of disk space on your SD card, so make sure you have enough free space before running it. You can check how much free disk space you have with the df -h command.

You can get this script by running the following command in a terminal:

$ wget https://raw.githubusercontent.com/raspberrypi/pico-setup/master/pico_setup.sh

  1. You should first sudo apt install wget if you don’t have wget already installed.

Then make the script executable with,

$ chmod +x pico_setup.sh

and run it with,

$ ./pico_setup.sh

The script will:

  • Create a directory called pico
  • Install required dependencies
  • Download the pico-sdk, pico-examples, pico-extras, and pico-playground repositories
  • Define PICO_SDK_PATH, PICO_EXAMPLES_PATH, PICO_EXTRAS_PATH, and PICO_PLAYGROUND_PATH in your ~/.bashrc
  • Build the blink and hello_world examples in pico-examples/build/blink and pico-examples/build/hello_world
  • Download and build picotool (see Appendix B), and copy it to /usr/local/bin.
  • Download and build picoprobe (see Appendix A).
  • Download and compile OpenOCD (for debug support)
  • Download and install Visual Studio Code
  • Install the required Visual Studio Code extensions (see Chapter 6 for more details)
  • Configure the Raspberry Pi UART for use with Raspberry Pi Pico

Once it has run, you will need to reboot your Raspberry Pi,

$ sudo reboot

for the UART reconfiguration to take effect. Once your Raspberry Pi has rebooted you can open Visual Studio Code in the “Programming”

Chapter 2. The SDK

IMPORTANT

The following instructions assume that you are using a Raspberry Pi Pico and some details may differ if you are using a different RP2040-based board. They also assume you are using Raspberry Pi OS running on a Raspberry Pi 4, or an equivalent Debian-based Linux distribution running on another platform. Alternative instructions for those using Microsoft Windows.

The Raspberry Pi Pico is built around the RP2040 microcontroller designed by Raspberry Pi. Development on the board is fully supported with both a C/C++ SDK, and an official MicroPython port. This book talks about how to get started with the SDK, and walks you through how to build, install, and work with the SDK toolchain.

TIP

For more information on the official MicroPython port see the Raspberry Pi Pico Python SDK book which documentsthe port, and “Get started with MicroPython on Raspberry Pi Pico” by Gareth Halfacree published by Raspberry Pi Press.

TIP

For more information on the C/C++ SDK, along with API-level documentation, see the Raspberry Pi Pico C/C++ SDK book.

2.1. Get the SDK and examples

The pico-examples repository (https://github.com/raspberrypi/pico-examples) provides a set of example applications that are written using the pico-sdk (https://github.com/raspberrypi/pico-sdk). To clone these repositories start by creating a pico directory to keep all pico related checkouts in. These instructions create a pico directory at /home/pi/pico.

$ cd ~/
$ mkdir pico
$ cd pico

Then clone the pico-sdk and pico-examples git repositories.

$ git clone -b master https://github.com/raspberrypi/pico-sdk.git
$ cd pico-sdk
$ git submodule update –init
$ cd ..
$ git clone -b master https://github.com/raspberrypi/pico-examples.git

NOTE

There are additional repositories: pico-extras, and pico-playground that you may also be interested in.

pdf download icon

Raspberry Pi Users Guide Free E-Book

2.2. Install the Toolchain

To build the applications in pico-examples, you’ll need to install some extra tools. To build projects you’ll need CMake, a cross-platform tool used to build the software, and the GNU Embedded Toolchain for Arm. You can install both these via apt from the command line. Anything you already have installed will be ignored by apt.

$ sudo apt update
$ sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential

  1. Native gcc and g++ are needed to compile pioasm, elf2uf2

Chapter 3. Blinking an LED in C

When you’re writing software for hardware, turning an LED on, off, and then on again, is typically the first program that gets run in a new programming environment. Learning how to blink an LED gets you half way to anywhere. We’re going to go ahead and blink the on-board LED on the Raspberry Pi Pico which is connected to pin 25 of the RP2040.

Pico Examples: https://github.com/raspberrypi/pico-examples/tree/master/blink/blink.c Lines 9 – 19

9 int main() {
10 const uint LED_PIN = 25;
11 gpio_init(LED_PIN);
12 gpio_set_dir(LED_PIN, GPIO_OUT);
13 while (true) {
14 gpio_put(LED_PIN, 1);
15 sleep_ms(250);
16 gpio_put(LED_PIN, 0);
17 sleep_ms(250);
18 }
19 }

3.1. Building “Blink”

From the pico directory we created earlier, cd into pico-examples and create a build directory.

$ cd pico-examples
$ mkdir build
$ cd build

Then, assuming you cloned the pico-sdk and pico-examples repositories into the same directory side-by-side, set the PICO_SDK_PATH:

$ export PICO_SDK_PATH=../../pico-sdk

Prepare your cmake build directory by running cmake ..

$ cmake ..
Using PICO_SDK_PATH from environment (‘../../pico-sdk')
PICO_SDK_PATH is /home/pi/pico/pico-sdk



— Build files have been written to: /home/pi/pico/pico-examples/build.


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Scroll to Top