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

Raspberry Pi Pico Serial Over USB - Serial Print Read Monitor Plotter Example C SDK

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:

pico_enable_stdio_usb ( main 1 ) pico_enable_stdio_uart ( main 0 )

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.

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.

#include "pico/stdlib.h" stdio_init_all ( ) ; printf ( "Hello World From Pi Pico USB CDC\n" ) ; sleep_ms ( 100 ) ;

Printing Text+Integer Numbers

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

#include "pico/stdlib.h" int counter = 0 ; stdio_init_all ( ) ; printf ( "CounterValue = %d\n" , counter ) ; sleep_ms ( 100 ) ;

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).

#include "pico/stdlib.h" float pi = 3.1415926 ; stdio_init_all ( ) ; printf ( "Pi Value = %f\n" , pi ) ; sleep_ms ( 100 ) ;

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.

#include "pico/stdlib.h" #define LED_PIN 20 int LED_STATE = 0 ; gpio_init ( LED_PIN ) ; gpio_set_dir ( LED_PIN , GPIO_OUT ) ; stdio_init_all ( ) ; printf ( "Enter 1 or 0 To Turn LED ON or OFF:\n" ) ; gpio_put ( LED_PIN , LED_STATE ) ; sleep_ms ( 100 ) ;

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).

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.

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.

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)