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

Chapter 1. Quick Pico Setup

When programming for Raspberry Pi Pico on Raspberry Pi 4B or Raspberry Pi 400, you can skip many installation steps in the Getting Started guide by running the setup script.

The downloadable PDF link is given at the end.

NOTE:

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

That being said, the instructions below are going to assume that you are using a Raspberry Pi Pico, though there might be differences if you are using another board with RP2040. Such advice also assumes that you are using Raspberry Pi OS on Raspberry Pi 4, or another Debian Linux distro on another platform. Different guidance for individuals utilizing Microsoft Windows.

The RP2040 microcontroller, designed by Raspberry Pi, serves as the core of the Raspberry Pi Pico. Support for development on the board is provided through both a C/C++ SDK and an official MicroPython version. This book covers the basics of getting started with the SDK and guides you in creating, setting up, and using the SDK toolchain.

TIP

For more information on the official MicroPython port see the Raspberry Pi Pico Python SDK book which documents the 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 create the applications in pico-examples, you must install additional tools. To create projects, you will require CMake, a versatile tool for software building, along with the GNU Embedded Toolchain for Arm. You have the option to install both of these using apt through the command line. Any previously installed items will not be recognized by the 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

For software created for hardware, the initial program usually involves switching an LED on, off, and back on in a new programming setting. Mastering the skill of blinking an LED will take you half the distance to any destination. We will proceed with flashing the built-in LED on the Raspberry Pi Pico, which is linked 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.