How to Receive Commands from Serial Monitor in Arduino
To receive commands from the serial monitor in Arduino, use
Serial.available() to check if data has arrived and Serial.read() to read the incoming bytes. This lets your Arduino program listen and respond to commands typed in the serial monitor.Syntax
The basic syntax to receive data from the serial monitor involves two main functions:
Serial.available(): Returns the number of bytes available to read.Serial.read(): Reads one byte (character) from the serial buffer.
You typically check if data is available, then read it byte by byte.
arduino
if (Serial.available() > 0) { char incomingByte = Serial.read(); // process incomingByte }
Example
This example reads characters sent from the serial monitor and echoes them back. It also turns on an LED if it receives the character '1' and turns it off if it receives '0'.
arduino
const int ledPin = 13; void setup() { Serial.begin(9600); // Start serial communication at 9600 baud pinMode(ledPin, OUTPUT); Serial.println("Send '1' to turn LED ON, '0' to turn it OFF."); } void loop() { if (Serial.available() > 0) { char command = Serial.read(); Serial.print("Received: "); Serial.println(command); if (command == '1') { digitalWrite(ledPin, HIGH); Serial.println("LED is ON"); } else if (command == '0') { digitalWrite(ledPin, LOW); Serial.println("LED is OFF"); } else { Serial.println("Unknown command"); } } }
Output
Send '1' to turn LED ON, '0' to turn it OFF.
Received: 1
LED is ON
Received: 0
LED is OFF
Received: x
Unknown command
Common Pitfalls
- Not checking
Serial.available()before reading: Reading without checking can cause errors or read invalid data. - Reading only one byte when multiple bytes are sent: You may miss data if you don't read all available bytes.
- Not initializing serial communication with
Serial.begin(): Without this, no data will be received. - Ignoring newline characters: The serial monitor often sends newline or carriage return characters that may need handling.
arduino
/* Wrong way: reading without checking availability */ int c = Serial.read(); // May read -1 if no data /* Right way: */ if (Serial.available() > 0) { char c = Serial.read(); }
Quick Reference
Here is a quick cheat sheet for receiving serial commands in Arduino:
| Function | Description |
|---|---|
| Serial.begin(baudrate) | Starts serial communication at the given speed |
| Serial.available() | Returns number of bytes available to read |
| Serial.read() | Reads one byte from serial buffer |
| Serial.readString() | Reads characters into a String until timeout |
| Serial.flush() | Clears the serial buffer (legacy use) |
Key Takeaways
Always start serial communication with Serial.begin() in setup().
Use Serial.available() to check if data is ready before reading.
Read incoming data byte by byte with Serial.read() to process commands.
Handle extra characters like newline or carriage return from the serial monitor.
Test commands by sending characters from the serial monitor and observing Arduino responses.