Bird
Raised Fist0
Arduinoprogramming~20 mins

Receiving commands over Bluetooth in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bluetooth 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 of this Bluetooth command receiver sketch?
Consider this Arduino code that reads characters from a Bluetooth serial connection and prints them to the Serial Monitor. What will be printed if the Bluetooth device sends the characters 'HELLO'?
Arduino
void setup() {
  Serial.begin(9600); // Serial monitor
  Serial1.begin(9600); // Bluetooth module
}

void loop() {
  if (Serial1.available()) {
    char c = Serial1.read();
    Serial.print(c);
  }
}
Ahello
BNo output because Serial1 is not initialized
CHELLO
DHELLOHELLO
Attempts:
2 left
💡 Hint
Think about what Serial1.read() returns and how Serial.print() works.
🧠 Conceptual
intermediate
1:30remaining
Which Arduino function checks if Bluetooth data is ready to read?
You want to read incoming data from a Bluetooth module connected to Serial1. Which function tells you if there is data waiting to be read?
ASerial1.read()
BSerial1.available()
CSerial1.begin()
DSerial1.print()
Attempts:
2 left
💡 Hint
This function returns how many bytes are ready to read.
🔧 Debug
advanced
2:30remaining
Why does this Bluetooth command receiver code not print anything?
Look at this Arduino code snippet. It should print characters received from Bluetooth on Serial1 to the Serial Monitor. But nothing appears on the Serial Monitor. What is the problem?
Arduino
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  if (Serial1.available() > 0) {
    char c = Serial1.read();
    Serial.print(c);
  }
}
ASerial1.available is missing parentheses, so it does not call the function
BSerial1.read() is called before Serial1.available()
CSerial.begin(9600) should be Serial1.begin(9600)
DSerial.print(c) should be Serial.println(c)
Attempts:
2 left
💡 Hint
Check how functions are called in Arduino code.
📝 Syntax
advanced
2:30remaining
Which option correctly reads a full command string from Bluetooth until newline?
You want to read a full command sent over Bluetooth ending with a newline character '\n'. Which code snippet correctly reads characters into a string until '\n' is received?
Arduino
String command = "";
void loop() {
  while (Serial1.available()) {
    char c = Serial1.read();
    if (c == '\n') {
      // command complete
      Serial.println(command);
      command = "";
    } else {
      command += c;
    }
  }
}
AReads characters but uses Serial.read() instead of Serial1.read()
BReads characters but never clears the command string
CReads characters but prints command before '\n' is received
DReads characters until '\n', then prints the command and clears it
Attempts:
2 left
💡 Hint
Look for the code that resets the command string after printing.
🚀 Application
expert
3:00remaining
How many commands will be stored in the array after this Bluetooth input?
This Arduino code reads commands separated by commas from Bluetooth and stores them in an array. If the Bluetooth device sends 'ON,OFF,TOGGLE,STATUS', how many commands will be stored?
Arduino
String commands[5];
int index = 0;
String current = "";

void loop() {
  while (Serial1.available()) {
    char c = Serial1.read();
    if (c == ',') {
      commands[index++] = current;
      current = "";
    } else {
      current += c;
    }
  }
  // After input ends, store last command
  if (current.length() > 0) {
    commands[index++] = current;
    current = "";
  }
}
A4
B3
C5
D1
Attempts:
2 left
💡 Hint
Count the number of commas and remember the last command after the last comma.

Practice

(1/5)
1. What does the available() function do when receiving Bluetooth commands on Arduino?
easy
A. It sends data to the Bluetooth device.
B. It checks if there is data ready to be read from the Bluetooth module.
C. It resets the Bluetooth connection.
D. It closes the Bluetooth communication.

Solution

  1. Step 1: Understand the role of available()

    The available() function returns the number of bytes available to read from the Bluetooth serial buffer.
  2. Step 2: Identify its use in receiving commands

    It is used to check if any data has arrived and is ready to be read, so the program knows when to read incoming commands.
  3. Final Answer:

    It checks if there is data ready to be read from the Bluetooth module. -> Option B
  4. Quick Check:

    available() checks data presence [OK]
Hint: Remember: available() means data is ready to read [OK]
Common Mistakes:
  • Thinking available() sends data instead of checking
  • Confusing available() with read()
  • Assuming available() resets connection
2. Which of the following is the correct way to read a single character from Bluetooth in Arduino?
easy
A. char command = Bluetooth.read();
B. char command = Bluetooth.available();
C. char command = Bluetooth.write();
D. char command = Bluetooth.begin();

Solution

  1. Step 1: Identify the function to read data

    The read() function reads one byte (character) from the Bluetooth serial buffer.
  2. Step 2: Confirm correct syntax

    Assigning Bluetooth.read() to a char variable correctly stores the incoming character.
  3. Final Answer:

    char command = Bluetooth.read(); -> Option A
  4. Quick Check:

    Use read() to get one char [OK]
Hint: Use read() to get data, not available() or write() [OK]
Common Mistakes:
  • Using available() to read data instead of read()
  • Using write() which sends data, not reads
  • Calling begin() which initializes, not reads
3. What will be printed on the Serial Monitor when the following Arduino code receives the character 'L' over Bluetooth?
if (Bluetooth.available()) {
  char cmd = Bluetooth.read();
  if (cmd == 'L') {
    Serial.println("LED ON");
  } else {
    Serial.println("UNKNOWN COMMAND");
  }
}
medium
A. LED ON
B. UNKNOWN COMMAND
C. L
D. No output

Solution

  1. Step 1: Check if data is available

    The code first checks if Bluetooth data is available, which it is since 'L' is sent.
  2. Step 2: Read and compare the character

    The character 'L' is read and compared to 'L', which matches, so it prints "LED ON".
  3. Final Answer:

    LED ON -> Option A
  4. Quick Check:

    Received 'L' triggers "LED ON" print [OK]
Hint: Match received char to command to print correct message [OK]
Common Mistakes:
  • Assuming it prints the character itself
  • Ignoring the if condition and expecting UNKNOWN COMMAND
  • Thinking no output if Bluetooth.available() is true
4. Identify the error in this Arduino Bluetooth command reading code:
if (Bluetooth.available > 0) {
  char cmd = Bluetooth.read();
  Serial.println(cmd);
}
medium
A. Bluetooth.read() should be Bluetooth.readChar().
B. The if condition should check for Bluetooth.read() instead.
C. Serial.println() cannot print characters.
D. Bluetooth.available should be called as a function with parentheses.

Solution

  1. Step 1: Check usage of available()

    The code uses Bluetooth.available without parentheses, but it is a function and must be called as Bluetooth.available().
  2. Step 2: Verify other parts

    Bluetooth.read() is correct to read a char, and Serial.println() can print characters, so no errors there.
  3. Final Answer:

    Bluetooth.available should be called as a function with parentheses. -> Option D
  4. Quick Check:

    available() needs parentheses [OK]
Hint: Remember functions need () to work [OK]
Common Mistakes:
  • Forgetting parentheses on available()
  • Thinking read() is readChar()
  • Believing Serial.println() can't print chars
5. You want your Arduino to turn an LED on when it receives the command '1' and off when it receives '0' over Bluetooth. Which code snippet correctly implements this behavior?
if (Bluetooth.available()) {
  char cmd = Bluetooth.read();
  switch (cmd) {
    case '1':
      digitalWrite(LED_PIN, HIGH);
      Serial.println("LED ON");
      break;
    case '0':
      digitalWrite(LED_PIN, LOW);
      Serial.println("LED OFF");
      break;
    default:
      Serial.println("Invalid command");
  }
}
hard
A. digitalWrite cannot be used with Bluetooth commands.
B. The code should use if-else instead of switch for Bluetooth commands.
C. This code correctly reads commands and controls the LED accordingly.
D. Bluetooth.read() returns an integer, so char cmd is wrong.

Solution

  1. Step 1: Analyze command reading and control

    The code reads a character from Bluetooth and uses a switch to check if it is '1' or '0'.
  2. Step 2: Verify LED control and feedback

    It sets the LED pin HIGH or LOW accordingly and prints the status, which matches the requirement.
  3. Final Answer:

    This code correctly reads commands and controls the LED accordingly. -> Option C
  4. Quick Check:

    Switch-case handles commands and controls LED [OK]
Hint: Use switch-case to handle multiple commands cleanly [OK]
Common Mistakes:
  • Thinking if-else is required instead of switch
  • Believing digitalWrite can't be used with Bluetooth input
  • Confusing data types returned by Bluetooth.read()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard