HOWTO · Arduino
Arduino Serial Flush
Learn what Arduino Serial.flush() does, how to use it in a sketch, and how PySerial's ser.flush() differs when sending serial data.
On this page
On Arduino, Serial.flush() waits until outgoing serial data has finished transmitting. It does not erase incoming data or confirm that another device received or acted on the message. Use it when the next step depends on the Arduino finishing its current transmission.
What does Serial.flush() do on Arduino?
Serial.flush() blocks the sketch while queued output is sent through the serial interface. Once it returns, the Arduino can continue with the next operation. This is useful when output must finish before a later action, such as changing a transceiver’s direction pin from transmit to receive.
The call is about the outgoing transmit path. It does not clear the receive buffer, and it does not tell you whether a connected sensor or computer understood the data. If you need confirmation, define a message exchange and wait for an acknowledgment from the other device.
Use Serial.flush() in an Arduino sketch
This Arduino call sequence sends a line, waits for outgoing transmission to finish, and then continues. Change the baud rate and message to match the device connected to your board. The complete C++ program below uses a small local stand-in for Serial, so it can demonstrate the sequence on a computer; replace that stand-in with the board’s Arduino core when adapting the sketch.
#include <iostream>
class SerialPort {
public:
void begin(unsigned long baud) { (void)baud; }
void println(const char* message) { std::cout << message << '\n'; }
void flush() {}
};
SerialPort Serial;
void setup() {
Serial.begin(9600);
Serial.println("Starting transmission...");
Serial.flush();
Serial.println("Transmission finished; continuing.");
}
int main() {
setup();
return 0;
}
Starting transmission...
Transmission finished; continuing.
On a board, use the Arduino-provided Serial object and the appropriate board core. The computer stand-in only demonstrates where the call appears in the sequence; it does not model electrical transmission or verify that a receiver processed the first line. For a hardware check, compile and upload the sketch for your board, then observe it with a serial monitor set to 9600 baud.
How PySerial’s flush() differs
In a Python program using PySerial, ser.flush() also waits for pending output to be written. It belongs to PySerial’s Serial object, while Serial.flush() belongs to an Arduino serial interface. Both concern outgoing data; neither method by itself waits for an application-level acknowledgment or clears received input.
For reliable confirmation, have the Arduino send a response after it has parsed and handled a command. The Python program can then read and validate that response. Keep this protocol step separate from ser.flush(): flushing output ensures the host has finished writing its pending data, while the response communicates what the Arduino did. A serial port name such as COM3 is operating-system-specific, so configure the port and baud rate for your setup.
Common questions about serial flushing
Does Serial.flush() clear the receive buffer? No. It waits for outgoing data to finish transmitting. Use the appropriate receive-reading logic to consume incoming bytes.
Does a completed flush mean the other device received the message? No. It means transmission from the local serial interface has completed; it cannot establish that the remote device received, parsed, or acted on the data. Use an acknowledgment for that confirmation.
When should I call it? Call Serial.flush() when subsequent code must wait for pending output to finish, for example before switching a half-duplex transceiver to receive mode. Ordinary logging usually does not need an explicit wait after every line.
Does PySerial use the same method name? PySerial exposes ser.flush() on its serial connection object. It has a similar outgoing-write purpose, but it is a separate API from Arduino’s Serial.flush().