Raspberry Pi Pico Serial Over USB C/C++ SDK (Serial Print)

In this tutorial, we’ll discuss the Raspberry PI Pico Serial Over USB functions using C/C++ SDK. We’ll also create a Raspberry Pi Pico Serial Print & Read Projects (C/C++ SDK). We’ll touch on the usage of the Serial Monitor and Serial Plotter with Raspberry Pi Pico as well. This tutorial is compatible with the Raspberry Pi Pico W board and any RP2040-based board.

This tutorial will address the Raspberry Pi Pico Serial Print & Read functions using the Pico C/C++ SDK programming. You can find navigation buttons on the left sidebar to go to the Raspberry Pi Pico tutorials series using the programming language you prefer instead.

Table of Contents

  1. Raspberry Pi Pico Serial Over USB
  2. Raspberry Pi Pico Serial Print (C/C++ SDK)
  3. Raspberry Pi Pico Serial Monitor (C/C++ SDK)
  4. Raspberry Pi Pico Serial Plotter (C/C++ SDK)
  5. Raspberry Pi Pico Serial Print Example (C/C++ SDK)
  6. Raspberry Pi Pico Serial Print Example Simulation
  7. Raspberry Pi Pico Serial Plotter Example (C/C++ SDK)
  8. Concluding Remarks

Raspberry Pi Pico Serial Over USB

The Raspberry Pi Pico (RP2040) microcontroller has 2x UART modules for serial communication: UART0 and UART1. The UART (RX & TX) pins are remappable, which means you can route the (RX or TX) signals internally to different GPIO pins of the microcontroller. However, the Default UART0 Pins: GPIO0 (TX) and GPIO1 (RX). We can definitely use UART0 or UART1 for serial communication with a PC.

However, the Raspberry Pi Pico (RP2040) microcontroller also has a native hardware USB module that we can use (in CDC class) to act as a serial port which will be detected by any PC as a Virtual COM Port. That’s what we’re going to use in this tutorial, since using a UART module will require a USB-TTL converter to communicate with a PC which is a little bit less convenient to use compared to the USB port.


Raspberry Pi Pico Serial Print (C/C++ SDK)

1. Enable stdio Over USB

To enable serial print (over USB CDC) in your Raspberry Pi Pico C/C++ SDK project, you need to edit the CMakeLists.txt file by adding the following two lines of code:

Which will therefore enable the stdio functions over USB instead of UART. main here is the name of the main.c executable c-code file, you can definitely change it to suit your main executable file name. But it’s recommended to name your main executable c-code file as main.c which is the standard naming convention in Embedded-C projects.

Check the tutorial linked below to help you get started with Raspberry Pi Pico with C/C++ SDK, create a new project from scratch, and learn more about the CMakeLists.txt file that we’ve just mentioned.

Raspberry Pi Pico C C++ SDK Programming (And Pico W)

This tutorial is the ultimate guide for getting started with Raspberry Pi Pico using C/C++ SDK. You’ll learn how to install the Pico C/C++ SDK toolchain, create a project from scratch, and build/flash your Embedded-C/C++ projects to the Raspberry Pi Pico (RP2040-based) boards.

2. stdio printf() Function

After enabling the stdio functions over USB in the CMakeLists.txt file as we’ve done in the previous step, we’ll now be able to use the printf() function to send formatted strings of text over the USB serial port to the PC.

The printf() function can be used to print only text messages and/or formatted strings of text that include a mixture of characters, integer numbers, floating-point numbers, etc.

Printing Text-Only Messages

Here is an example of printing a text-only message string over the USB serial port.

Printing Text+Integer Numbers

Here is another example for printing (text + integer) messages over the USB serial port.

Printing Text+Float Numbers

You can also use it to print (text + floating-point numbers) as in the following example (using the %f float-specifier flag).

3. stdio scanf() Function

Similar to the printf() function, the scanf() function is used to read a formatted string from the stdin. You should use this function only if the incoming serial data is sent in a fixed format. Otherwise, you can use the gets() function to read a line string of unknown text format that you can parse or process thereafter according to your application’s needs.

Here is an example of reading a numeric value over the USB serial port and using that value to control an LED to turn ON/OFF.

4. stdio gets() Function

The gets(char* str) function reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when an EOF is reached, whichever comes first.

5. stdio puts() Function

The puts(char* str) function writes a string to stdout up to but not including the null character. A newline character is appended to the output string.


Raspberry Pi Pico Serial Monitor (C/C++ SDK)

A serial monitor is a GUI application that you can run on your PC to communicate with embedded microcontroller devices over the serial port. You can use the integrated Serial Monitor in Arduino IDE or any other serial terminal application that runs on your operating system (like TeraTerm or Putty).

Arduino IDE Serial Monitor Tool

Using a serial monitor is really helpful for debugging Raspberry Pi Pico projects by sending the value of variables and flags to check them in real time using the serial monitor.

❕ Note

Using the Raspberry Pi Pico’s USB for serial print/read operations is not affected by the Baud Rate setting in your Serial Monitor application. You can leave it as 9600bps, 115200, or anything else. It just doesn’t matter while using USB CDC (Virtual COM Port).


Raspberry Pi Pico Serial Plotter (C/C++ SDK)

The Arduino Serial Plotter is another handy tool that’s also built into the Arduino IDE. It enables you to plot variables sent over the serial communication port and have a graphical visualization of the variable’s value over time. However, you can still use any other serial plotting application instead in case you don’t have the Arduino IDE installed on your PC.

Arduino Serial Plotter GUI Tool

Using the serial plotter with Raspberry Pi Pico can be really helpful for debugging or monitoring the behavior of your code. Especially things that show weird timing behavior or if you’re trying to catch some runtime bugs. Showing a graphical plot for different values & flags over time can be extremely helpful.


Raspberry Pi Pico Serial Print Example (C/C++ SDK)

Let’s now create a Raspberry Pi Pico Serial Print/Read example project with the C/C++ SDK. In this example, we’ll set a GPIO pin as an output to an LED. And we’ll enable stdio operations over USB instead of UART. A string message prompt will be printed out to the user and we’ll get an integer input from the user (0 or 1) to control the output LED status (ON/OFF).

It’s highly recommended to check out this tutorial (Getting Started With Raspberry Pi Pico Using C/C++ SDK), especially for setting up the development environment and creating a new project from scratch.

Step #1

Create the project folder HELLO_WORLD and a main.c source code file. This is the source code to paste into the main.c file.

(This code can be used on both Raspberry Pi Pico & Pico W boards)

Step #2

Create and add the CMakeLists.txt file to the project’s folder. This is the CMake code to paste into the CMakeLists.txt file.

(Copy the CMake code that suits your board, whether it’s a Raspberry Pi Pico or Pico W board)

Raspberry Pi Pico

Raspberry Pi Pico W

Step #3

Copy the file named ( pico_sdk_import.cmake ) CMake source file from the pico-sdk/external folder into your new project’s folder. The new project’s folder should now look like the one shown below.

Raspberry-Pi-Pico-Read-Digital-Input-Write-Digital-Output-C-SDK-Example.jpg

Step #4

Open the “Pico – Visual Studio Code” shortcut launcher from the desktop or from the Windows start menu (type in search: “Pico – Visual Studio Code”). Select File > Open Folder. Navigate to where your new project’s folder is located and choose that folder.

VS Code will prompt you at the bottom of the IDE’s window to configure the project’s folder for you, select ‘yes’, and let it do that for you.

Raspberry-Pi-Pico-C-SDK-Programming-Standalone-Project-VS-Code-1

Step #5

Start the build process using the build button or from the top menu select Terminal > Run Build Task. Wait tell completion and you should get an output message like the one shown below, it also shows you the path for the output files. Ideally, you should find everything in the build folder inside the project folder that we’ve created.

Standalone-Raspberry-Pi-Pico-C-SDK-Project-Build

Step #6

Let’s now flash the new project firmware to the Raspberry Pi Pico board. Hold the BOOTSEL button of the Raspberry Pi Pico board before connecting it to the USB port of your PC. While holding the BOOTSEL button, connect the Raspberry Pi Pico to your PC’s USB port.

It’ll boot in the bootloader mode and your PC will detect it as a USB storage device, so you’re now free to release the BOOTSEL button.

Drag and drop the main.uf2 output file to the Raspberry Pi Pico USB drive. It’ll take a couple of seconds to load, then it’ll automatically reboot and start running the new firmware that we’ve flashed to the RP2040 microcontroller.

❕ Note

While testing this demo project, you may not catch the first printed message on your serial monitor and it’ll show up as a blank window screen. That’s fine, it’s just waiting for your input value “1” or “0” to set the LED’s pin, print the prompt message again, and keep repeating.


Raspberry Pi Pico Serial Print Example Simulation

This is the simulation result for the previous Raspberry Pi Pico (And Pico W) HELLO_WORLD serial print/read example project (C/C++ SDK) using the Wokwi Simulator. You can find the Project Simulation files linked below, so you can save a copy of it into your Wokwi projects dashboard and play around with the simulator environment.

Raspberry-Pi-Pico-Serial-Print-Read-Example-Simulation

Simulating your Raspberry Pi Pico projects can be really helpful especially when you’re just getting started. This step is not mandatory at all, however, running your projects in a simulator environment will help you catch and fix some logic errors in the code or in the circuit wiring connections. Below is a complete guide tutorial for simulating your Raspberry Pi Pico (And Pico W) projects using the Wokwi simulator.

Raspberry Pi Pico Simulator Guide (Wokwi) - Arduino MicroPython CircuitPython C SDK

This article will provide more in-depth information about simulating Raspberry Pi Pico projects using the online Wokwi simulator tool. You’ll learn how to simulate MicroPython, CircuitPython, Arduino C++, and C SDK projects on Raspberry Pi Pico.


Raspberry Pi Pico Serial Plotter Example (C/C++ SDK)

In this example project, we’ll use the serial plotter with Raspberry Pi Pico to plot 3x floating-point values that represent 3-phase sine waves with different amplitude & 120° of phase shift between them.

Note: The variables need to be comma-separated and line termination needs to be added at the end of the data points string to be plotted each time.

The project creation steps are exactly the same as the previous “ HELLO_WORLD” project, however, this new “ SERIAL_PLOTTER” project will have the following source code ( main.c).

Here is the result after building this project and flashing it to my Raspberry Pi Pico Board. Note that I’m using the Arduino Serial Plotter GUI tool.

You can also use the Wokwi Simulator’s Serial Plotter Tool to get the same results (in a simulation environment) if you’d like to simulate your project before doing a real-life test on your hardware Raspberry Pi Pico board. Here is the simulation result for this “ SERIAL_PLOTTER” project.

Raspberry-Pi-Pico-Serial-Plotter-Example-C-C-SDK

The link below will direct you to the Wokwi simulation file for this Raspberry Pi Pico Serial Plotter project example (C/C++ SDK).

(Raspberry Pi Pico) Serial Plotter Project Simulation (C/C++ SDK)


Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link below. Every small donation helps to keep this website up and running and ultimately supports our community.

Raspberry Pi Pico Hardware Kit

If you’re looking forward to playing around with the Raspberry Pi Pico board to test its capabilities and features, you may just need to get a single board. Just like this one here. However, if you’d like to follow along with the Raspberry Pi Pico series of tutorials published here on our website, you may consider getting the following items:

2x Pi Pico Boards, 2x Pi Pico W Boards, 4x BreadBoards, Resistors, LEDs, Buttons, Potentiometers, etc. The reason behind getting multiple boards is that we’ll be using one board as a PicoProbe debugger for SWD debugging activities. And at least 2x Pico W boards for IoT wireless applications.

The full kit list of the Raspberry Pi Pico series of tutorials is found at the link below, as well as some test equipment for debugging that you may consider getting for your home electronics LAB.


Concluding Remarks

To conclude this tutorial, we’ve discussed how to enable serial over USB for the Raspberry Pi Pico microcontrollers. We’ve also created a serial print & read project example with Raspberry Pi Pico using C/C++ SDK, and we’ve tested the usage of Serial Monitor & Serial Plotter with the Raspberry Pi Pico for debugging and testing various projects.

Follow this Raspberry Pi Pico Series of Tutorials to learn more about Raspberry Pi Pico Programming in different programming languages’ environments.

If you’re just starting with Raspberry Pi Pico, you should check out the following getting-started guides with the programming language you favor the most. There are 4 variants of the Raspberry Pi Pico tutorials to match your needs and help you along the path that you’re going to choose.

Pico SDK (C/C++)

Raspberry Pi Pico C C++ SDK Programming (And Pico W)
Get Started

Arduino

Raspberry Pi Pico Arduino IDE Programming (And Pico W)
Get Started

MicroPython

Getting Started With Raspberry Pi Pico (And Pico W)
Get Started

CircuitPython

Raspberry Pi Pico CircuitPython Programming (And Pico W) Tutorial Getting Started
Get Started

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

2 thoughts on “Raspberry Pi Pico Serial Over USB C/C++ SDK (Serial Print)”

Leave a Comment