Getting Started with the Raspberry Pi Pico Multicore Microcontroller Board Using C

By Jacob Beningo

Contributed By DigiKey's North American Editors

There is an inherent need in embedded systems to have a powerful, low-cost microcontroller unit (MCU). These devices play an important role not just in the product, but also in supporting tests, rapid prototyping, and capabilities such as machine learning (ML). However, getting started with MCUs generally requires an in-depth understanding of MCU technology and low-level programming languages. On top of that, development boards often cost between $20 and $1000, which can be too expensive for many developers. Also, it’s not always the case that a development board is available, and even when they are, designers often struggle to get a board up and running.

This article introduces the Raspberry Pi Pico (SC0915) as a low-cost development board for the RP2040 MCU that provides developers with a wide range of capabilities. The article explores the Pico and some expansion boards, examines the different software development kits that the Raspberry Pi Pico supports, and demonstrates how to create a blinky LED application using the C SDK.

Introduction to the Raspberry Pi Pico

The Raspberry Pi Pico was first introduced in 2021 as the development platform for the RP2040 microcontroller. The Pico can be used as a standalone development board, or it can be designed into a product directly due to edge connections that can be soldered to a carrier board (Figure 1). Between the Pico's sub $5 cost and its multipurpose use, it has become a popular solution for both makers and professional developers.

Image of Raspberry Pi Pico is a low-cost development boardFigure 1: The Raspberry Pi Pico is a low-cost development board that contains everything necessary to develop applications on the RP2040 microcontroller. (Image source: Raspberry Pi)

The RP2040 features a dual-core Arm® Cortex®-M0+ processor clocked at 133 megahertz (MHz) and includes up to 264 kilobytes (Kbytes) of SRAM. The RP2040 does not include flash on-chip. Instead, the Raspberry Pi Pico provides an external 2 megabyte (Mbyte) flash chip that interfaces with the RP2040 over a quad serial peripheral interface (QSPI). The board also provides a user LED, a crystal oscillator that the phase lock loop (PLL) uses to create a stable high-speed CPU clock, and a pushbutton to configure whether the processor boots normally or into a bootloader.

An extensive ecosystem

The Raspberry Pi Pico already has an extensive ecosystem that allows developers to choose between using MicroPython or C software development kits to write applications for the board. One interesting note about the Raspberry Pi Pico is that there is not just a single development board available. Instead, there are three; the original SC0915 with a standard configuration, the SC0917 which includes header connectors, and the SC0918 which includes a low-cost Wi-Fi chip for connected applications (Figure 2).

Image of Raspberry Pi Pico is available in three configurationsFigure 2: The Raspberry Pi Pico is available in three configurations. (Image source: Beningo Embedded Group, LLC)

For each of these versions, the general footprint of the board remains the same. The edge connections for the board consist of 40-pin edge connections for the peripherals and connection options shown in Figure 3. These include power, ground, a universal asynchronous receiver and transmitter (UART), general purpose input and output (GPIO), pulse width modulation (PWM), an analog-to-digital converter (ADC), a serial peripheral interconnect (SPI), an inter-integrated circuit (I2C) interface, and debugging.

Image of Raspberry Pi Pico edge-connected pinouts (click to enlarge)Figure 3: The Raspberry Pi Pico edge-connected pinouts provide a wide variety of peripheral access. (Image source: Raspberry Pi)

Breakout board options

When the Raspberry Pi is going to be used for rapid prototyping, there is a need to gain easy access to the board’s edge connectors. One option for accessing them is to populate the headers and use a breadboard. However, this solution can often result in a mess of wires that can lead to errors. So instead, there are several options for breakout boards that expand the edge connectors to more readily available interfaces.

For example, the MM2040EV Pico module board from Bridgetek breaks most of the edge connectors into pin and socket connections. Additionally, there is the 103100142 shield for the Pico from Seeed Studio that provides each peripheral interface as a connector. Each connector is pin compatible with expansion boards to add functions such as inertial sensors, motor drivers, and range finders.

To C or to MicroPython?

Embedded systems have traditionally been written in C because it balances low-level control with higher-level system application approaches. The problem with C today is that it’s an antiquated, fifty-year-old programming language that is rarely taught in universities. It is also too easy to accidentally inject bugs and cause damage. Despite these potential issues, C is the language of choice for the majority of embedded systems development.

An alternative to using C, provided by the Raspberry Pi Pico ecosystem, is MicroPython. MicroPython is a CPython port designed to run on MCU-based systems. While it is undoubtedly a heavier processor user than C, it is a modern language with which many developers are familiar and comfortable. MicroPython can abstract out low-level details of the MCU and hardware. Hardware accesses are through high-level application programming interfaces (APIs) that are easy to learn—an important feature with tight project deadlines.

When selecting which software development kit (SDK) to use—C or MicroPython—developers need to focus on specific needs. Compared to MicroPython, using C will provide low-level access to the MCU’s registers, have a smaller memory footprint, and be more efficient.

Setting up the C SDK

When using the C SDK to create a blinky LED application, there are several options. The first is to review the SDK documentation and follow the instructions. The second is to use a preset Docker container to automatically install all the tools necessary to get started. A third option is to install the toolchains and the Raspberry Pi Pico example code manually, including:

  • Git
  • Python 3
  • Cmake
  • gcc-arm-none-eabi \
  • libnewlib-arm-none-eabi

Retrieving the Raspberry Pi Pico example code can be done by cloning Raspberry Pi’s git repo using the following command:

git clone https://github.com/raspberrypi/pico-sdk /home/sdk/pico-sdk && \

     cd /home/sdk/pico-sdk && \

    git submodule update --init &&

Once these libraries and the source code are installed, the next step is to explore and compile a blinky LED application.

Writing a first blinky application

The C SDK comes with a blinky example that developers can use to build their first application. The Code Listing below uses the Pico’s onboard LED and the PICO_DEFAULT_LED_PIN directive to set up an I/O pin and blink it with a 250 millisecond (ms) delay.

Copy
	/**
	 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
	 *
	 * SPDX-License-Identifier: BSD-3-Clause
	 */
	

	#include "pico/stdlib.h"
	

	int main() {
	#ifndef PICO_DEFAULT_LED_PIN
	#warning blink example requires a board with a regular LED
	#else
	    const uint LED_PIN = PICO_DEFAULT_LED_PIN;
	    gpio_init(LED_PIN);
	    gpio_set_dir(LED_PIN, GPIO_OUT);
	    while (true) {
	        gpio_put(LED_PIN, 1);
	        sleep_ms(250);
	        gpio_put(LED_PIN, 0);
	        sleep_ms(250);
	    }
	#endif
	}

Code Listing: The Raspberry Pi Pico uses the PICO_DEFAULT_LED_PIN directive to set up an I/O pin and blink it with a 250 ms delay. (Code source: Raspberry Pi)

Per the listing, the LED_PIN is assigned the default pin; calls are then made to the C gpio APIs. gpio_init is used to initialize the pin, while gpio_set_dir is used to set the LED_PIN to an output. An infinite loop is then created that toggles the state of the LED every 250 ms.

Compiling the application is relatively straightforward. First, a developer needs to create a build directory in their Raspberry Pi Pico folder using the following commands:

mkdir build

cd build

Next, cmake needs to be prepared for the build by executing the following command:

cmake

Now, a developer can change to the blinky directory and run make:

cd blink

make

The output from the build process will be a blinky.uf2 file. The compiled program can be loaded on the Raspberry Pi Pico by holding down the BOOTSEL pin and powering up the board. The RP2 will then appear as a mass storage device. The developer needs to drag the blinky.uf2 file to the drive, at which point the bootloader will install the application. Once completed, the LED should begin blinking.

Conclusion

The Raspberry Pi Pico is an attractive solution for embedded developers looking for flexibility in their development cycle. Several options are available, including standalone solutions or boards with wireless connectivity. In addition, the ecosystem supports C and C++, as well as MicroPython. Developers can pick which language works best for their application and then leverage the corresponding SDK to accelerate software development.

DigiKey logo

Disclaimer: The opinions, beliefs, and viewpoints expressed by the various authors and/or forum participants on this website do not necessarily reflect the opinions, beliefs, and viewpoints of DigiKey or official policies of DigiKey.

About this author

Image of Jacob Beningo

Jacob Beningo

Jacob Beningo is an embedded software consultant. He has published more than 200 articles on embedded software development techniques, is a sought-after speaker and technical trainer, and holds three degrees, including a Masters of Engineering from the University of Michigan.

About this publisher

DigiKey's North American Editors