We use Serial.available() to see if there is any data waiting to be read from the serial port. It helps us know when to read data safely.
When you want to read data sent from your computer to the Arduino.
When you need to check if a user typed something in the serial monitor.
When your Arduino is communicating with another device over serial and you want to wait for incoming data.
Syntax
Arduino
Serial.available()
This function returns the number of bytes available to read.
If it returns 0, there is no data to read yet.
Examples
Check if there is at least one byte available before reading.
Arduino
if (Serial.available() > 0) {
// read data
}
Store and print how many bytes are waiting to be read.
Arduino
int bytes = Serial.available();
Serial.print("Bytes available: ");
Serial.println(bytes);
Sample Program
This program waits for you to type characters in the serial monitor. When you type a character, it reads and prints it back with a message.
Arduino
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
Serial.println("Send some characters:");
}
void loop() {
if (Serial.available() > 0) {
char incomingChar = Serial.read();
Serial.print("I received: ");
Serial.println(incomingChar);
}
}
OutputSuccess
Important Notes
Always check Serial.available() before reading to avoid errors.
Reading from serial without checking may cause your program to wait or read wrong data.
Summary
Serial.available() tells you how many bytes are ready to read.
Use it to know when to read data safely from the serial port.
It helps your Arduino communicate smoothly with other devices or your computer.
Practice
(1/5)
1. What does Serial.available() return in an Arduino program?
easy
A. The baud rate of the serial communication
B. The number of bytes available to read from the serial buffer
C. The total size of the serial buffer
D. The number of bytes sent to the serial port
Solution
Step 1: Understand the function purpose
Serial.available() checks how many bytes are waiting in the serial buffer to be read.
Step 2: Compare options with function behavior
It does not return buffer size, baud rate, or bytes sent, only bytes ready to read.
Final Answer:
The number of bytes available to read from the serial buffer -> Option B
Quick Check:
Serial.available() = bytes ready to read [OK]
Hint: Remember: available means ready to read bytes [OK]
Common Mistakes:
Confusing available bytes with buffer size
Thinking it returns baud rate
Assuming it counts bytes sent, not received
2. Which of the following is the correct way to check if there is data to read using Serial.available()?
easy
A. if (Serial.available() != 0) { /* write data */ }
B. if (Serial.available() == 0) { /* read data */ }
C. if (Serial.available() < 0) { /* read data */ }
D. if (Serial.available() > 0) { /* read data */ }
Solution
Step 1: Identify the condition to read data
You should read data only if there is at least one byte available, so check if > 0.
Step 2: Analyze each option
if (Serial.available() > 0) { /* read data */ } correctly checks if bytes are available. if (Serial.available() == 0) { /* read data */ } checks for zero (no data), if (Serial.available() < 0) { /* read data */ } is impossible (available() >= 0), if (Serial.available() != 0) { /* write data */ } mixes reading and writing.
Final Answer:
if (Serial.available() > 0) { /* read data */ } -> Option D
Quick Check:
Read only if available > 0 [OK]
Hint: Check if available is greater than zero before reading [OK]
Common Mistakes:
Checking for zero instead of greater than zero
Using negative checks which never happen
Confusing reading with writing conditions
3. What will be the output of this Arduino code snippet if 3 bytes are waiting in the serial buffer?
The code checks if bytes are available, then reads the count of available bytes.
Step 2: Given 3 bytes waiting, the count variable will be 3 and printed
The output will be "Bytes available: 3".
Final Answer:
Bytes available: 3 -> Option A
Quick Check:
Serial.available() = 3 bytes, prints count [OK]
Hint: Serial.available() returns actual bytes waiting to read [OK]
Common Mistakes:
Assuming it prints zero or one byte always
Thinking no output if bytes exist
Confusing print and println effects
4. Identify the error in this Arduino code snippet that uses Serial.available():
void loop() {
if (Serial.available = 0) {
int data = Serial.read();
Serial.println(data);
}
}
medium
A. Serial.read() is called without checking Serial.available() first
B. Serial.println() cannot print integers
C. The assignment operator '=' is used instead of '==' in the if condition
D. The loop function is missing Serial.begin() initialization
Solution
Step 1: Check the if condition syntax
The code uses '=' which assigns 0 instead of comparing with '=='. This causes a logic error.
Step 2: Understand the impact of this error
Because of assignment, the condition always evaluates to false (0), so data is never read.
Final Answer:
The assignment operator '=' is used instead of '==' in the if condition -> Option C
Quick Check:
Use '==' to compare, not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Not initializing Serial in setup() (not shown here)
Assuming Serial.println can't print integers
5. You want to read all bytes sent to your Arduino via serial and store them in a string until no more bytes are available. Which code snippet correctly uses Serial.available() to do this?
A)
String data = "";
while (Serial.available() == 0) {
data += (char)Serial.read();
}
B)
String data = "";
while (Serial.available() > 0) {
data += (char)Serial.read();
}
C)
String data = "";
if (Serial.available() > 0) {
data += (char)Serial.read();
}
D)
String data = "";
while (Serial.available() < 0) {
data += (char)Serial.read();
}
hard
A. Reads all bytes while available is greater than zero (correct)
B. Reads data only when no bytes are available (wrong logic)
C. Reads only one byte if available, not all bytes
D. Checks for negative available bytes, which never happens
Solution
Step 1: Understand the goal to read all bytes until none left
You must keep reading while bytes are available, so loop while Serial.available() > 0.
Step 2: Analyze each option
The snippet using while (Serial.available() == 0) skips reading if data is present (and infinite loops if none). The snippet using if (Serial.available() > 0) reads only one byte. The snippet checking Serial.available() < 0 never triggers. The snippet using while (Serial.available() > 0) correctly reads all bytes.
Final Answer:
Reads all bytes while available is greater than zero (correct) -> Option A
Quick Check:
Loop while available > 0 to read all bytes [OK]
Hint: Loop while Serial.available() > 0 to read all bytes [OK]
Common Mistakes:
Looping when no bytes are available
Reading only once instead of all bytes
Checking for negative available bytes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.