Arduino serial echo

This page presents a simple Arduino sketch that echo a character on the serial device. User send a character via USB (serial), the Arduino read the character and echo the same character back to the user. This page has been tested with the following versions (software and hardware):

Seeeduino / Arduino used for serial echo

Simple echo

The following code is the simplest version of the echo sketch:

#define BAUD_RATE   115200

// Setup, initialize 
void setup() 
{
  Serial.begin(BAUD_RATE);  
}

// Loop forever
void loop() 
{
  // Serial data is pending
  if (Serial.available()) 
  { 
    // Echo serial data on serial device
    Serial.write( Serial.read() );
  }  
}

Turn on LED when echoing

The following version turn on the built-in LED everytime a char is echoed.

#define BAUD_RATE   115200

// Setup, initialize 
void setup() 
{
  Serial.begin(BAUD_RATE);  

  // Initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// Loop forever
void loop() 
{
  // Serial data is pending
  if (Serial.available()) 
  { 
    // Turn built in LED on
    digitalWrite(LED_BUILTIN, HIGH); 

    // Echo serial data on serial device
    Serial.write( Serial.read() );

  // Turn built in LED off
    digitalWrite(LED_BUILTIN, LOW); 
  }  
}

Uppercase when echoing

The following version is an improvement of the previous one. Each low case character sent is uppercased before echoing:

#define BAUD_RATE   115200

// Setup, initialize 
void setup() 
{
  Serial.begin(BAUD_RATE);  

  // Initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// Loop forever
void loop() 
{
  // Serial data is pending
  if (Serial.available()) 
  { 
    // Turn built in LED on
    digitalWrite(LED_BUILTIN, HIGH); 
    // Get serial data
    char c=Serial.read();

    // Tranform low case characters to upper case
    if (c>='a' && c<'z') c=c-32;

    // Echo data on serial device
    Serial.write( c );

  // Turn built in LED off
    digitalWrite(LED_BUILTIN, LOW); 
  }  
}

About Seeed Studio

The hardware presented on this page was kindly donated by Seeed.

Seeed is the IoT hardware enabler providing services over 10 years that empower makers to realize their projects and products. Seeed offers a wide array of hardware platforms and sensor modules ready to be integrated with existing IoT platforms and one-stop PCB production and cheap pcb assembly. Seeed Studio provides a wide selection of electronic parts including Arduino, Raspberry Pi and many different development board platforms. Especially the Grove System helps engineers and makers to avoid jumper wires problems. Seeed Studio has developed more than 280 Grove modules covering a wide range of applications that can fulfill a variety of needs.

See also


Last update : 12/25/2019