Initialise stdio over USB or UART with stdio_init_all
Turn on printf output on a Pico with stdio_init_all, choosing USB or UART in CMake, and wait for the USB host before printing. Pico SDK 2.1.0.
stdio_init_all starts every stdio driver that CMake enabled, and from then on printf goes to it. Which one is active is decided at build time, with pico_enable_stdio_usb and pico_enable_stdio_uart on the target, so the same C source works for both.
pico_enable_stdio_usb(app 1)
pico_enable_stdio_uart(app 0)#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/stdio_usb.h"
int main(void) {
stdio_init_all();
while (!stdio_usb_connected()) {
sleep_ms(100); // wait for a terminal to open the port
}
printf("hello from the pico\n");
while (true) tight_loop_contents();
}hello from the picoThe wait loop is the part people miss. A USB serial port only exists once the host opens it, so a printf straight after stdio_init_all is lost, and a program that exits quickly shows nothing at all. With UART output enabled instead, the default pins are GPIO 0 (TX) and GPIO 1 (RX) at 115200 baud, which suits a board with no USB data lines. Leave the wait loop out for a headless device, because it would block forever. stdio_usb_connected is declared in pico/stdio_usb.h. See the Pico C SDK book.