Skip to content

Arduino Serial.flush Hangs the Program

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Arduino Serial.flush Hangs the Program. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Calling Serial.flush() causes the program to freeze or behave unexpectedly.

Quick Fix

Wrong

Serial.print("Data");
Serial.flush();  // Waits for tx buffer to empty
Program appears frozen until all bytes are transmitted.
Serial.print("Data");
Serial.flush();  // Safe - waits for TX to complete
// On AVR: flush() waits for TX shift register too
// On SAMD/ESP32: flush() waits for TX FIFO
Serial.println("Done");
DataDone  (flush ensures Data is fully sent before Done prints).

Prevention

Use Serial.flush() only when you must confirm all data is sent before continuing (e.g., before going to sleep). On older Arduino cores, flush() also discards received data — check the core documentation. On modern cores, flush() only waits for TX completion.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Does flush() clear the receive buffer?

On older AVR cores (pre-1.0), Serial.flush() discarded received data. On modern cores, it only waits for TX to complete. Use Serial.end() to fully reset.

When should I use flush()?

Before sleep modes, before close() on SoftwareSerial, or when precise timing between sends is needed.

Is flush() blocking?

Yes. It blocks until the TX buffer is empty. For non-blocking writes, check availableForWrite() first.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro