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 learn how to create a new C/C++ project for Raspberry Pi Pico and build it using C/C++ SDK.

What You Will Learn

  • How to create a C/C++ project for Raspberry Pi Pico?
  • How to build a new C/C++ program for Raspberry Pi Pico?
  • How to compile and build C/C++ programs for Raspberry Pi Pico using the SDK?
  • 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

Prerequisite Software

Create Folder Structure for New C/C++ Project

  • Use Windows File Explorer to navigate to the folder containing pico-sdk and pico-examples
  • Create an empty folder pico-empty-project
New Project Folder for Raspberry Pi Pico
New C/C++ Project Folder for Raspberry Pi Pico
  • Navigate inside the folder pico-empty-project
  • Create an empty folder build
  • Create an empty folder main
Build and Source Folder Inside Raspberry Pi Pico Project Folder
Build and Source Folder Inside Raspberry Pi Pico C/C++ Project Folder
  • Create a file CMakeLists.txt
  • Add the below code to CMakeLists.txt
cmake_minimum_required(VERSION 3.12)

# Import Pico SDK
include(pico_sdk_import.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})

# Initialize the SDK
pico_sdk_init()

include(example_auto_set_url.cmake)
# Add main Folder
add_subdirectory(main)
  • Navigate to folder pico-examples
  • Copy the file example_auto_set_url.cmake and pico_sdk_import.cmake
  • Navigate to folder pico-empty-project
  • Paste the file example_auto_set_url.cmake and pico_sdk_import.cmake
Raspberry Pi Pico Project Folder Structure
Raspberry Pi Pico C/C++ Project Folder Structure
  • Navigate to folder main
  • Create a file CMakeLists.txt
  • Add the below code to CMakeLists.txt
add_executable(main
         main.c
         )

 # Import pico_stdlib which imports commonly used features
 target_link_libraries(main pico_stdlib)

 # Create map/bin/hex file etc.
 pico_add_extra_outputs(main)

 # Add path via pico_set_program_url CMAKE
 example_auto_set_url(main)
  • Create a file main.c
  • Add your own code or the provided blink code
/**
 * www.ArnabKumarDas.com
 *
 * On-Board LED Blinky
 */

#include "pico/stdlib.h"

int main() 
{
    const uint LED_PIN = PICO_DEFAULT_LED_PIN;
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    
    while (1) 
    {
        gpio_put(LED_PIN, 1);
        sleep_ms(250);
        gpio_put(LED_PIN, 0);
        sleep_ms(250);
        gpio_put(LED_PIN, 1);
        sleep_ms(250);
        gpio_put(LED_PIN, 0);
        sleep_ms(750);
    }
}
Inside main Folder of Raspberry Pi Pico C/C++ Project
Inside main Folder of Raspberry Pi Pico C/C++ Project

Building the New C/C++ Project

  • Open the Visual Studio 2019 Developer Command Prompt
  • Navigate to the new project build folder pico-empty-project\build
 ** Visual Studio 2019 Developer Command Prompt v16.9.4
 ** Copyright (c) 2021 Microsoft Corporation
 
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools>cd /D D:\Electronics\RP2040\pico-empty-project

D:\Electronics\RP2040\pico-empty-project>cd build
D:\Electronics\RP2040\pico-empty-project\build>
Navigating to build Folder
Navigating to build Folder
  • Execute the command cmake -G "NMake Makefiles" ..
D:\Electronics\RP2040\pico-empty-project\build>cmake -G "NMake Makefiles" ..
D:\Electronics\RP2040\pico-empty-project\build>
Executing cmake
Executing cmake
Successful cmake
cmake Successful
  • Execute the command nmake to build the project
D:\Electronics\RP2040\pico-empty-project\build>nmake
D:\Electronics\RP2040\pico-empty-project\build>
Executing nmake
Executing nmake
nmake Build Successful
nmake Build Successful
  • Navigate to the folder \pico-empty-project\build\main to view the build artifacts
Raspberry Pi Pico C/C++ Build Artifacts
Raspberry Pi Pico C/C++ Build 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 main.uf2 UF2 Binary into the RPI-RP2 Drive
  • Pico will Reboot automatically, and the on-board Green LED should start Blinking

Download

  • Download and Extract the file to the folder containing pico-sdk and pico-examples
  • Follow the build instructions
  • Edit the cmake files to add new source and header files
  • Edit the folder name to project name

Crazy Engineer

MAKER - ENGINEER - YOUTUBER

0 Comments

Leave a Reply

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.