Bird
Raised Fist0
Arduinoprogramming~20 mins

Receiving commands from computer in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Serial Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when receiving a command?

Consider this Arduino code snippet that reads a character from the computer and responds:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    if (cmd == 'A') {
      Serial.println("Command A received");
    } else {
      Serial.println("Unknown command");
    }
  }
}

If the user sends the character 'A' from the computer, what will the Arduino print?

Arduino
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    if (cmd == 'A') {
      Serial.println("Command A received");
    } else {
      Serial.println("Unknown command");
    }
  }
}
ANo output
BUnknown command
CA
DCommand A received
Attempts:
2 left
💡 Hint

Check what the code prints when the character matches 'A'.

Predict Output
intermediate
2:00remaining
What happens if no data is sent?

Given this Arduino code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    Serial.print("Received: ");
    Serial.println(cmd);
  } else {
    Serial.println("Waiting for command...");
  }
  delay(1000);
}

If no data is sent from the computer, what will the Arduino print repeatedly?

Arduino
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    Serial.print("Received: ");
    Serial.println(cmd);
  } else {
    Serial.println("Waiting for command...");
  }
  delay(1000);
}
AError: no data
BWaiting for command...
CReceived: \n
DNo output
Attempts:
2 left
💡 Hint

Look at what the code does when Serial.available() is zero.

Predict Output
advanced
2:00remaining
What is the output after sending 'B' and 'C' commands?

Analyze this Arduino code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    switch(cmd) {
      case 'A':
        Serial.println("Alpha");
        break;
      case 'B':
        Serial.println("Bravo");
        break;
      case 'C':
        Serial.println("Charlie");
        break;
      default:
        Serial.println("Unknown");
    }
  }
}

If the user sends the characters 'B' then 'C' in sequence, what will be printed?

Arduino
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    switch(cmd) {
      case 'A':
        Serial.println("Alpha");
        break;
      case 'B':
        Serial.println("Bravo");
        break;
      case 'C':
        Serial.println("Charlie");
        break;
      default:
        Serial.println("Unknown");
    }
  }
}
A
Alpha
Bravo
B
Charlie
Bravo
C
Bravo
Charlie
D
Unknown
Unknown
Attempts:
2 left
💡 Hint

Check the switch cases for 'B' and 'C'.

Predict Output
advanced
2:00remaining
What error occurs with this code snippet?

Look at this Arduino code snippet:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    int cmd = Serial.read();
    if (cmd == 'A') {
      Serial.println("Got A");
    }
  }
}

What is the type of the variable cmd and what problem might it cause?

Arduino
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    int cmd = Serial.read();
    if (cmd == 'A') {
      Serial.println("Got A");
    }
  }
}
Acmd should be char; using int may cause unexpected behavior
BType mismatch error because cmd is int but compared to char
CNo error; cmd is int and comparison works fine
DCompilation error due to wrong variable type
Attempts:
2 left
💡 Hint

Consider the return type of Serial.read() and how it compares to char.

🧠 Conceptual
expert
3:00remaining
How to correctly read a full command string from computer?

You want to receive a full command string (like "START" or "STOP") from the computer via Serial and act only after the full command is received. Which approach is best?

ARead characters one by one in loop, append to a buffer until newline '\n' is received, then process the buffer
BUse Serial.readString() without checking if data is available
CRead only one character per loop and act immediately without buffering
DUse delay() to wait fixed time then read all available bytes at once
Attempts:
2 left
💡 Hint

Think about how to know when a full command is complete.

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

  1. Step 1: Understand Serial.begin()

    Serial.begin(9600); initializes the serial communication at a speed of 9600 bits per second.
  2. 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.
  3. Final Answer:

    It starts serial communication at 9600 bits per second. -> Option B
  4. 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

  1. Step 1: Identify function to check data availability

    Serial.available() returns the number of bytes available to read from the serial buffer.
  2. Step 2: Understand usage in condition

    Using if (Serial.available()) checks if there is any data to read (non-zero means data is available).
  3. Final Answer:

    if (Serial.available()) { } -> Option A
  4. 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"?
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    String command = Serial.readStringUntil('\n');
    Serial.println(command);
  }
}
medium
A. HELLO\n
B. H
C. HELLO
D. No output

Solution

  1. 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'.
  2. Step 2: Analyze the code output

    The input "HELLO" followed by Enter sends "HELLO\n". The code reads "HELLO" and prints it exactly.
  3. Final Answer:

    HELLO -> Option C
  4. 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

  1. Step 1: Check Serial.available usage

    Serial.available is a function and must be called with parentheses: Serial.available().
  2. Step 2: Verify other function calls

    Serial.read() correctly reads one byte without parameters; Serial.print() can print chars.
  3. Final Answer:

    Serial.available is used without parentheses -> Option A
  4. 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?
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    // What goes here?
  }
}
hard
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

  1. Step 1: Understand String comparison in Arduino

    Arduino String objects overload the == operator to compare contents with string literals like "ON".
  2. 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.
  3. Final Answer:

    if (cmd == "ON") digitalWrite(13, HIGH); else if (cmd == "OFF") digitalWrite(13, LOW); -> Option D
  4. Quick Check:

    Arduino String == compares content [OK]
Hint: Use cmd == "ON" to compare Arduino Strings [OK]
Common Mistakes:
  • Using = instead of == for comparison
  • Calling non-existent cmd.equal()
  • Using JavaScript === operator in Arduino