Go back

Serial Echo with Arduino: Learn USART Communication

Introduction

Serial communication is one of the most fundamental skills in embedded electronics. Whether you’re debugging a sensor, talking to a GPS module, or sending data to a PC, the humble USART (Universal Synchronous/Asynchronous Receiver/Transmitter) is your go-to tool. In this tutorial, we’ll build a simple Serial Echo project using an Arduino Uno in the Velxio simulator. Every character you type in the Serial Monitor is echoed back with its ASCII value and a running count. It’s the “Hello, World!” of serial communication.

Circuit diagram of serial-echo on the Velxio simulator canvas

Circuit Walkthrough

This project is remarkably simple—it uses only the Arduino Uno board itself. No external components are needed because the serial communication happens over the USB-to-serial converter built into the Uno. In Velxio, the Serial Monitor is integrated into the editor toolbar, so you can interact with your code immediately.

Components

ComponentQuantityNotes
Arduino Uno (wokwi-arduino-uno)1The brain of the operation

That’s it! The Arduino Uno’s TX (pin 1) and RX (pin 0) are internally connected to the USB interface, which Velxio emulates perfectly. No wiring is required.

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:

// Serial Echo — USART Protocol Test
// Open the Serial Monitor to send and receive data.
// Everything you type is echoed back with extra info.

void setup() {
  Serial.begin(9600);
  Serial.println("=============================");
  Serial.println("  Serial Echo Test (USART)");
  Serial.println("=============================");
  Serial.println("Type something and press Send.");
  Serial.println();

  // Print system info
  Serial.print("CPU Clock: ");
  Serial.print(F_CPU / 1000000);
  Serial.println(" MHz");
  Serial.print("Baud rate: 9600");
  Serial.println();
  Serial.println();
}

unsigned long charCount = 0;

void loop() {
  if (Serial.available() > 0) {
    char c = Serial.read();
    charCount++;

    Serial.print("[");
    Serial.print(charCount);
    Serial.print("] Received: '");
    Serial.print(c);
    Serial.print("' (ASCII ");
    Serial.print((int)c);
    Serial.println(")");
  }

  // Periodic heartbeat
  static unsigned long lastBeat = 0;
  if (millis() - lastBeat >= 5000) {
    lastBeat = millis();
    Serial.print("Uptime: ");
    Serial.print(millis() / 1000);
    Serial.print("s | Chars received: ");
    Serial.println(charCount);
  }
}

Source code for serial-echo in the Velxio Monaco editor

How It Works

  1. Setup: We initialize serial communication at 9600 baud using Serial.begin(9600). Then we print a banner and system info: the CPU clock speed (F_CPU / 1000000 gives MHz) and the baud rate.

  2. Global variable: unsigned long charCount = 0; keeps track of how many characters we’ve received.

  3. Loop:

    • if (Serial.available() > 0) checks if data is waiting in the receive buffer.
    • char c = Serial.read(); reads one character.
    • We increment charCount and print a formatted message: [count] Received: 'c' (ASCII 99).
    • Every 5 seconds, we print a heartbeat with uptime and total characters received.

Key Concepts for Students

Common Pitfalls and Debugging Tips

Suggested Extensions

Once you’ve mastered the basic echo, try these enhancements:

  1. Echo whole lines: Instead of echoing each character, accumulate characters until newline, then echo the entire line.
  2. Command parser: Interpret special commands like “LED ON” to turn on an LED (add an LED to the circuit).
  3. Binary output: Print the received character in binary format.
  4. Multiple baud rates: Let the user change baud rate via a command.
  5. Log to virtual EEPROM: Store received data in the simulated EEPROM.

Conclusion

You’ve just built your first serial communication project! The Serial Echo is a simple but powerful demonstration of how microcontrollers talk to the outside world. With Velxio, you can experiment without any hardware—just open the simulator, write code, and test instantly.

Ready to try it yourself? Open the live example in Velxio and start typing: Serial Echo on Velxio. Happy coding!


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
Serial LED Control with Arduino: Send Commands to Blink
Next Post
LCD 20x4 Display with Arduino: Hello World in Velxio