How to Write your First C++ Program on the Raspberry Pi Pico W

 

Welcome to this comprehensive tutorial on setting up, building, and flashing a C++ project for the Raspberry Pi Pico W on macOS. The Raspberry Pi Pico W is a powerful microcontroller board based on the RP2040 microcontroller, featuring dual-core ARM Cortex-M0+ processors, flexible I/O options, and built-in Wi-Fi connectivity. This tutorial will guide you through the entire process, from installing the necessary tools to running a “Hello, World!” program that communicates over USB serial.

In this guide, you will learn how to:

  1. Set up the development environment on macOS, including installing Homebrew, CMake, and the ARM GCC toolchain.
  2. Clone and initialize the Pico SDK, which provides essential libraries and tools for developing applications for the Raspberry Pi Pico W.
  3. Create a simple C++ project that prints “Hello, World!” to the serial console.
  4. Build and flash your project to the Pico W.
  5. Connect to the Pico W’s serial output using terminal applications such as screen and minicom.

Whether you’re a seasoned developer or just getting started with microcontrollers, this tutorial will provide you with the knowledge and skills to begin developing applications for the Raspberry Pi Pico W on macOS.

— — -

Before we delve into the topic, we invite you to support our ongoing efforts and explore our various platforms dedicated to enhancing your IoT projects:

  • Subscribe to our YouTube Channel: Stay updated with our latest tutorials and project insights by subscribing to our channel at YouTube — Shilleh.
  • Support Us: Your support is invaluable. Consider buying me a coffee at Buy Me A Coffee to help us continue creating quality content.
  • Hire Expert IoT Services: For personalized assistance with your IoT projects, hire me on UpWork.

ShillehTek Website (Exclusive Discounts):

https://shillehtek.com/collections/all

ShillehTekAmazon Store:

ShillehTek Amazon Store — US

ShillehTek Amazon Store — Canada

ShillehTek Amazon Store — Japan

Step 1: Set Up the Environment

Install Prerequisites:

  • Homebrew: Install Homebrew if you haven’t already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • CMake and ARM GCC Toolchain:
brew install cmake gcc-arm-none-eabi

Clone the Pico SDK:

mkdir -p ~/pico
cd ~/pico
git clone -b master https://github.com/raspberrypi/pico-sdk.git
cd pico-sdk
git submodule update --init

Set the Environment Variable:

  • Set the PICO_SDK_PATH environment variable in your shell configuration file (~/.zshrc for Zsh or ~/.bashrc for Bash):
echo 'export PICO_SDK_PATH=~/pico/pico-sdk' >> ~/.zshrc
source ~/.zshrc

Step 2: Create a C++ Project

Create a Project Directory:

mkdir -p ~/pico/my_project
cd ~/pico/my_project

Create a CMakeLists.txt File:

cmake_minimum_required(VERSION 3.13)

# Include the Pico SDK initialization script
include($ENV{PICO_SDK_PATH}/pico_sdk_init.cmake)

project(my_project)

# Initialize the Pico SDK
pico_sdk_init()

# Add your executable and source files
add_executable(my_project
main.cpp
)

# Enable USB stdio and disable UART stdio
pico_enable_stdio_usb(my_project 1)
pico_enable_stdio_uart(my_project 0)

# Link the Pico SDK to your project
target_link_libraries(my_project pico_stdlib)

# Create map/bin/hex/uf2 files
pico_add_extra_outputs(my_project)

Create a main.cpp File:

#include "pico/stdlib.h"
#include <cstdio> // Include the C standard IO functions

int main() {
stdio_init_all(); // Initialize standard IO

while (true) {
printf("Hello, World!\n");
sleep_ms(1000);
}
}

Step 3: Build and Flash the Project

Navigate to the Build Directory and Clean it:

mkdir -p build
cd build
rm -rf *

Run CMake to Generate Build Files:

cmake ..

Build the Project:

make

Flash the Firmware:

  • Unplug the Pico W from your Mac.
  • Hold down the BOOTSEL button.
  • While holding the BOOTSEL button, plug the Pico W back into your Mac. The Pico should appear as a mass storage device (RPI-RP2).
  • Copy the generated .uf2 file to the Pico:
cp my_project.uf2 /Volumes/RPI-RP2/

Now that your UF2 file is on the device, your Pico W should start running it and logging to serial output. The next step shows you commands you can utilize to view the output of the program!

Verify Serial Connection

Using screen

Set the TERM Environment Variable:

If you encounter issues with the $TERM too long - sorry. error in screen, set the TERM environment variable to vt100 to ensure compatibility with screen:

export TERM=vt100

Check for Serial Device:

ls /dev/tty.*

Look for a device like /dev/tty.usbmodemXXXX.

Connect Using screen:

screen /dev/tty.usbmodemXXXX 115200

View the Output:

  • If the Pico W is running your program correctly, you should see the “Hello, World!” messages being printed to the terminal every second.

Exit screen:

  • To exit screen, press Ctrl+A followed by K, and then confirm by pressing Y.

Using minicom (Alternative)

Install minicom:

brew install minicom

Run minicom:

minicom -b 115200 -o -D /dev/tty.usbmodemXXXX

Exit minicom:

  • Press Ctrl+A to enter command mode.
  • Then press Z to bring up the help menu.
  • Finally, press X to exit minicom.

Conclusion

Congratulations! You have successfully set up your development environment, created and built a C++ project, and flashed it to your Raspberry Pi Pico W. You also learned how to connect to the Pico W’s serial output using screen and minicom, ensuring you can monitor and interact with your running programs.

With these foundational skills, you’re now ready to explore the full potential of the Raspberry Pi Pico W. Whether you want to build IoT applications, create interactive devices, or experiment with embedded systems, the knowledge gained from this tutorial will serve as a solid starting point.

Continue experimenting and building more complex projects, and don’t hesitate to explore the extensive documentation and resources available for the Raspberry Pi Pico W. Happy coding!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Implementing Google reCAPTCHA in a Simple React and Node.js App

Implementing Google reCAPTCHA in a Simple React and Node.js App

Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.