Arduino Clear Serial Buffer

This tutorial will discuss clearing the serial buffer using the Serial.begin() function in Arduino.

Arduino Clear Serial Buffer

Every Arduino board comes up with pins that can be used for serial communication, and we can use these pins to send and receive data through serial communication. The serial class of Arduino has a function that can read and write data to a serial pin.

Use the read() function to read data from a serial pin, and the received data is stored inside a buffer before it is used inside the code. The buffer has a specific memory to store the data, and in case of a large amount of data, we have to clear the serial buffer so that it won’t run out of memory.

We can use the Serial.begin() function to begin the serial after receiving data from the serial.

The Serial.begin() function is used to initialize the serial with the given data rate, like 9600 bits per second, but when we initialize the serial, the previously stored data inside the serial buffer will be removed from the memory.

For example, let’s consider we want to read a string through a serial pin and after receiving a chunk of data, we want to clear the serial buffer.

We can use the Serial.available() function to check if the data is available in the serial buffer and if it is available, we will store the data in a string array, and then we will clear the serial buffer using the Serial.begin() function.

See the example code below.

String value;
void setup() {

void loop() {
   if (Serial.available()){
      value = "";
		 char data=;

We used the value variable to store the character data we received from the serial. If data is available, we will add it to the array and end the serial communication using the Serial.end() function and then initialize the serial to clear the serial buffer.

We used a loop to store all the available data before ending the serial communication and clearing the buffer.

If we want to send data, we can use the Serial.flush() function to wait for all the data to be transmitted and then we can end the serial communication and clear the buffer.

Check this link for more details about the Serial and its functions.

Related Article - Arduino Serial

  • Baud Rate in Arduino Serial Communication
  • Arduino Serial Flush
  • Arduino Serial Port Read String