Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Receiving commands from computer
📖 Scenario: You want to control an LED connected to your Arduino using commands sent from your computer through the serial monitor.
🎯 Goal: Build a simple Arduino program that listens for commands from the computer and turns an LED on or off accordingly.
📋 What You'll Learn
Create a variable for the LED pin number
Set up serial communication at 9600 baud
Read commands from the serial input
Turn the LED on when receiving the command 'ON'
Turn the LED off when receiving the command 'OFF'
Print confirmation messages back to the serial monitor
💡 Why This Matters
🌍 Real World
Controlling devices like LEDs, motors, or sensors from a computer is common in home automation and robotics.
💼 Career
Understanding serial communication and command handling is essential for embedded systems and IoT development jobs.
Progress0 / 4 steps
1
Set up LED pin and serial communication
Create an integer variable called ledPin and set it to 13. In the setup() function, initialize serial communication at 9600 baud and set ledPin as an output.
Arduino
Hint
Use Serial.begin(9600); inside setup() to start serial communication.
Use pinMode(ledPin, OUTPUT); to set the LED pin as output.
2
Create a variable to store incoming commands
Create a String variable called command inside the loop() function to store the incoming serial data.
Arduino
Hint
Use String command = ""; to create an empty string variable.
3
Read serial input and control the LED
Inside the loop() function, check if serial data is available using Serial.available(). If data is available, read the incoming string using Serial.readStringUntil('\n') and store it in command. Use if statements to turn the LED on if command equals "ON" and off if it equals "OFF".
Arduino
Hint
Use Serial.readStringUntil('\n') to read the command until a newline.
Use digitalWrite(ledPin, HIGH) to turn the LED on and digitalWrite(ledPin, LOW) to turn it off.
4
Print confirmation messages
Add Serial.println() statements inside the if and else if blocks to print "LED is ON" when the LED turns on and "LED is OFF" when it turns off.
Arduino
Hint
Use Serial.println("LED is ON") and Serial.println("LED is OFF") to send messages back to the computer.
Practice
(1/5)
1. What is the purpose of Serial.begin(9600); in an Arduino sketch?
easy
A. It reads data from the serial port.
B. It starts serial communication at 9600 bits per second.
C. It sends data to the computer automatically.
D. It stops the serial communication.
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 B
Quick Check:
Serial.begin() = start communication [OK]
Hint: Serial.begin() always starts communication at given speed [OK]
Common Mistakes:
Thinking Serial.begin() reads or sends data
Confusing Serial.begin() with Serial.read()
Assuming Serial.begin() stops communication
2. Which of the following is the correct way to check if data is available to read from the serial port?
easy
A. if (Serial.available()) { }
B. if (Serial.write()) { }
C. if (Serial.begin()) { }
D. if (Serial.read() > 0) { }
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
Using if (Serial.available()) checks if there is any data to read (non-zero means data is available).
Final Answer:
if (Serial.available()) { } -> Option A
Quick Check:
Serial.available() checks data presence [OK]
Hint: Use Serial.available() to check before reading [OK]
Common Mistakes:
Using Serial.read() to check availability
Calling Serial.begin() inside loop
Using Serial.write() to check data
3. What will be the output on the Serial Monitor if the following code receives the input string "HELLO"?
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 C
Quick Check:
readStringUntil('\n') returns string without newline [OK]
Hint: readStringUntil('\n') excludes newline from output [OK]
Common Mistakes:
Expecting newline character printed
Thinking only one character is read
Assuming no output without delay
4. Identify the error in this Arduino code snippet that tries to read a command from the serial port:
void loop() {
if (Serial.available > 0) {
char c = Serial.read();
Serial.print(c);
}
}
medium
A. Serial.available is used without parentheses
B. Serial.read() is missing a parameter
C. Serial.print() cannot print char variables
D. The if condition should check for Serial.read() instead
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 A
Quick Check:
Functions need parentheses to call [OK]
Hint: Always use parentheses when calling functions like Serial.available() [OK]
Common Mistakes:
Forgetting parentheses on function calls
Thinking Serial.read() needs parameters
Assuming Serial.print() can't print chars
5. You want to receive a command string from the computer and turn on an LED if the command is "ON" and turn it off if "OFF". Which code snippet correctly implements this?
A. if (cmd === "ON") digitalWrite(13, HIGH); else if (cmd === "OFF") digitalWrite(13, LOW);
B. if (cmd = "ON") digitalWrite(13, HIGH); else if (cmd = "OFF") digitalWrite(13, LOW);
C. if (cmd.equal("ON")) digitalWrite(13, HIGH); else if (cmd.equal("OFF")) digitalWrite(13, LOW);
D. if (cmd == "ON") digitalWrite(13, HIGH); else if (cmd == "OFF") digitalWrite(13, LOW);
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 D
Quick Check:
Arduino String == compares content [OK]
Hint: Use cmd == "ON" to compare Arduino Strings [OK]