We use this to let the Arduino listen and respond to instructions sent from a computer. It helps the Arduino do different tasks based on what the computer tells it.
Receiving commands from computer in Arduino
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
// Use the command to do something
}
}Serial.begin(baud_rate) starts communication speed between Arduino and computer.
Serial.available() checks if there is data waiting to be read.
if (Serial.available() > 0) { char command = Serial.read(); if (command == 'a') { // Turn LED on } }
if (Serial.available()) { String input = Serial.readStringUntil('\n'); // Use the input string }
This program waits for commands from the computer. If you send '1', it turns the LED on. If you send '0', it turns the LED off. It also sends back messages to the computer to confirm.
const int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); Serial.println("Send '1' to turn ON LED, '0' to turn OFF."); } void loop() { if (Serial.available() > 0) { char command = Serial.read(); 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"); } } }
Always set the same baud rate in your computer program and Arduino.
Use Serial Monitor in Arduino IDE to send commands easily.
Commands are case sensitive; '1' is different from '1 ' (with space).
Use Serial.begin() to start communication.
Check Serial.available() before reading data.
Use Serial.read() or Serial.readStringUntil() to get commands.
Practice
Serial.begin(9600); in an Arduino sketch?Solution
Step 1: Understand Serial.begin()
Serial.begin(9600);initializes the serial communication at a speed of 9600 bits per second.Step 2: Identify its role in communication
This function sets up the Arduino to send and receive data through the serial port at the specified speed.Final Answer:
It starts serial communication at 9600 bits per second. -> Option BQuick Check:
Serial.begin() = start communication [OK]
- Thinking Serial.begin() reads or sends data
- Confusing Serial.begin() with Serial.read()
- Assuming Serial.begin() stops communication
Solution
Step 1: Identify function to check data availability
Serial.available()returns the number of bytes available to read from the serial buffer.Step 2: Understand usage in condition
Usingif (Serial.available())checks if there is any data to read (non-zero means data is available).Final Answer:
if (Serial.available()) { } -> Option AQuick Check:
Serial.available() checks data presence [OK]
- Using Serial.read() to check availability
- Calling Serial.begin() inside loop
- Using Serial.write() to check data
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
Serial.println(command);
}
}Solution
Step 1: Understand Serial.readStringUntil()
This function reads characters from the serial buffer until it finds the newline character '\n'. It returns the string without the '\n'.Step 2: Analyze the code output
The input "HELLO" followed by Enter sends "HELLO\n". The code reads "HELLO" and prints it exactly.Final Answer:
HELLO -> Option CQuick Check:
readStringUntil('\n') returns string without newline [OK]
- Expecting newline character printed
- Thinking only one character is read
- Assuming no output without delay
void loop() {
if (Serial.available > 0) {
char c = Serial.read();
Serial.print(c);
}
}Solution
Step 1: Check Serial.available usage
Serial.available is a function and must be called with parentheses: Serial.available().Step 2: Verify other function calls
Serial.read() correctly reads one byte without parameters; Serial.print() can print chars.Final Answer:
Serial.available is used without parentheses -> Option AQuick Check:
Functions need parentheses to call [OK]
- Forgetting parentheses on function calls
- Thinking Serial.read() needs parameters
- Assuming Serial.print() can't print chars
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
// What goes here?
}
}Solution
Step 1: Understand String comparison in Arduino
Arduino String objects overload the == operator to compare contents with string literals like "ON".Step 2: Check each option's correctness
if (cmd == "ON")correctly compares string contents.if (cmd = "ON")performs assignment, not comparison.cmd.equal("ON")fails--no such method (it's equals()).===is invalid C++ syntax.Final Answer:
if (cmd == "ON") digitalWrite(13, HIGH); else if (cmd == "OFF") digitalWrite(13, LOW); -> Option DQuick Check:
Arduino String == compares content [OK]
- Using = instead of == for comparison
- Calling non-existent cmd.equal()
- Using JavaScript === operator in Arduino
Ready to prove it?
Pass the test and this pattern moves unknown
