Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived and stored in the serial receive buffer (which holds 64 bytes).
Serial.available() inherits from the Stream utility class.
- Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
- The number of bytes available to read.
Example Code 1
The following code returns a character received through the serial port.
- Type "HELLO" on Serial Monitor and click Send button:
- The result on Serial Monitor:
Example Code 2
Arduino Mega example:
This code sends data received in one serial port of the Arduino Mega to another. This can be used, for example, to connect a serial device to the computer through the Arduino board.
※ NOTES AND WARNINGS:
Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:
- An Arduino board and other Arduino board
- An Arduino board and other sensors/devices
- An Arduino board and computer (any Serial software on computer)