Go back

Pico Serial Echo: Test UART on RP2040 with Velxio

Introduction

Welcome to another Velxio tutorial! Today we’re building a Serial Echo test for the Raspberry Pi Pico (RP2040). This project is perfect for beginners who want to understand how serial communication works on microcontrollers. You’ll learn how to send and receive data over UART, add a visual indicator with an LED, and implement a simple heartbeat timer.

Circuit diagram of pico-serial-echo on the Velxio simulator canvas

Circuit Walkthrough

The circuit is delightfully simple. We only need two components:

Wiring

  1. Connect the anode (A) of the yellow LED to the TX pin of the Pico. The TX pin is used for serial transmission, so every time the Pico sends data, the LED will blink.
  2. Connect the cathode (C) of the LED to GND (any ground pin on the Pico).

That’s it! The LED is wired in series with the TX line, so it lights up when the Pico transmits. This gives you a visual confirmation that serial data is flowing.

Velxio editor toolbar with compile and run controls

Code Walkthrough

Let’s dive into the code. Open the Velxio editor and you’ll see the following sketch:

// Raspberry Pi Pico — Serial Echo Test
// Echoes received characters and prints a heartbeat every 2 seconds

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("=== Pico Serial Echo ===");
  Serial.println("Type something and press Enter.");
  Serial.println();
}

unsigned long lastHeartbeat = 0;

void loop() {
  // Echo any incoming characters
  while (Serial.available()) {
    char c = Serial.read();
    Serial.print("Echo: ");
    Serial.println(c);
  }

  // Heartbeat every 2 seconds
  if (millis() - lastHeartbeat >= 2000) {
    lastHeartbeat = millis();
    Serial.print("[Heartbeat] Uptime: ");
    Serial.print(millis() / 1000);
    Serial.println("s");
  }
}

How It Works

The lastHeartbeat variable keeps track of the last time the heartbeat was printed, using millis() for non-blocking timing.

Key Concepts

Serial Communication (UART)

UART (Universal Asynchronous Receiver/Transmitter) is a simple serial protocol. The Pico’s TX pin sends data, and the RX pin receives data. In this example, we only use TX for output, but the echo function also uses RX (via the Serial Monitor).

Non-blocking Timing with millis()

Instead of using delay(), which pauses the entire program, we use millis() to check elapsed time. This allows the echo loop to run continuously while still printing the heartbeat at regular intervals.

LED as a Visual Indicator

By connecting the LED to the TX pin, we get a real-time visual of serial activity. Every time the Pico sends a character, the LED blinks briefly.

Common Pitfalls & Debugging Tips

Velxio canvas toolbar above the simulation viewport

Suggested Extensions

Once you have the basic echo working, try these enhancements:

  1. Add an RX LED: Connect another LED to the RX pin to show when data is received.
  2. Command parsing: Instead of just echoing, interpret commands like “LED ON” or “LED OFF” to control an external LED.
  3. Multiple baud rates: Allow the user to change baud rate via a serial command.
  4. Log to console: Store received data in a buffer and print it on request.

Velxio component picker modal browsing parts by category

Call to Action

Ready to try it yourself? Open the live example on Velxio and start experimenting: Pico Serial Echo on Velxio. You can modify the code, change the LED color, or add new components. Happy hacking!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
Pico Serial LED Control: Build Your First Command Interface
Next Post
Blink an LED on Raspberry Pi Pico with Velxio Simulator