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

Interfacing the MPU6050 with Raspberry Pi Pico W in C++

Interfacing the MPU6050 with Raspberry Pi Pico W in C++

Interface with the MPU6050 using the Raspberry Pi Pico W in C++.

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

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

Write your first C++ Program on the Pico W in a few simple steps.

How to Use ThingSpeak with the Raspberry Pi Pico W

How to Use ThingSpeak with the Raspberry Pi Pico W

Learn how to create a real-time environmental monitoring system with the Raspberry Pi Pico W and ThingSpeak!

How to Use ADS1115 with the Raspberry Pi (Part 1)

How to Use ADS1115 with the Raspberry Pi (Part 1)

Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

How to Install Pip Packages in AWS Lambda Using Docker and ECR

How to Install Pip Packages in AWS Lambda Using Docker and ECR

Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

Create Tabular Product Descriptions on Your Shopify Store

Create Tabular Product Descriptions on Your Shopify Store

Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

SSH Into Raspberry Pi with Tailscale VPN

SSH Into Raspberry Pi with Tailscale VPN

Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

Send Email with Lua and the ESP32

Send Email with Lua and the ESP32

In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

How to Code with Lua on ESP32 with XEdge32

How to Code with Lua on ESP32 with XEdge32

Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

Stream Audio From Raspberry Pi to Local Computer

Stream Audio From Raspberry Pi to Local Computer

Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

Back to blog

Leave a comment

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