📖 Scenario: You want to control an LED on your Arduino board using a Bluetooth module like HC-05 or HC-06. This project will help you set up the Bluetooth communication and turn the LED on or off by sending simple commands from your phone or computer.
🎯 Goal: Build a simple Arduino program that reads data from the HC-05/HC-06 Bluetooth module and turns an LED on or off based on the received command.
📋 What You'll Learn
Create a variable for the LED pin number
Create a variable for the Bluetooth serial communication
Write code to read data from the Bluetooth module
Control the LED based on the received data
Print the LED status to the serial monitor
💡 Why This Matters
🌍 Real World
Bluetooth modules like HC-05 and HC-06 are commonly used to add wireless control to devices such as home automation systems, robots, and wearable gadgets.
💼 Career
Understanding Bluetooth communication and Arduino programming is useful for embedded systems developers, IoT engineers, and hobbyists working on wireless device projects.
Progress0 / 4 steps
1
Set up the LED pin
Create an integer variable called ledPin and set it to 13. Then, in the setup() function, set the ledPin as an output using pinMode.
Arduino
Hint
Use pinMode(ledPin, OUTPUT); inside setup() to prepare the LED pin.
2
Initialize Bluetooth serial communication
Create a SoftwareSerial object called bluetooth using pins 10 for RX and 11 for TX. In the setup() function, start the bluetooth serial communication at 9600 baud rate. Also, start the default serial communication at 9600 baud rate.
Arduino
Hint
Use bluetooth.begin(9600); and Serial.begin(9600); inside setup().
3
Read Bluetooth data and control LED
In the loop() function, check if bluetooth.available() is greater than zero. If yes, read the incoming byte using bluetooth.read() and store it in a variable called incomingByte. If incomingByte is the character '1', turn the LED on using digitalWrite(ledPin, HIGH). If incomingByte is the character '0', turn the LED off using digitalWrite(ledPin, LOW). Also, print "LED ON" or "LED OFF" to the serial monitor accordingly.
Arduino
Hint
Use bluetooth.read() to get the data and digitalWrite to control the LED.
4
Test and display LED status
Print the message "Waiting for Bluetooth command..." once in the setup() function to the serial monitor. This will help you know the program is ready.
Arduino
Hint
Use Serial.println("Waiting for Bluetooth command..."); inside setup().
Practice
(1/5)
1. What is the main purpose of using an HC-05 or HC-06 Bluetooth module with an Arduino?
easy
A. To enable wireless serial communication between Arduino and other Bluetooth devices
B. To increase the Arduino's processing speed
C. To provide power supply to the Arduino
D. To connect Arduino to the internet via Wi-Fi
Solution
Step 1: Understand the function of HC-05/HC-06 modules
These modules are designed to allow wireless serial communication using Bluetooth technology.
Step 2: Compare options with module purpose
Only To enable wireless serial communication between Arduino and other Bluetooth devices correctly describes enabling wireless serial communication; others describe unrelated functions.
Final Answer:
To enable wireless serial communication between Arduino and other Bluetooth devices -> Option A
Quick Check:
HC-05/HC-06 = Wireless serial communication [OK]
Hint: HC-05/HC-06 = Bluetooth serial communication module [OK]
Common Mistakes:
Confusing Bluetooth with Wi-Fi connectivity
Thinking it powers the Arduino
Assuming it speeds up Arduino processing
2. Which of the following is the correct way to create a SoftwareSerial object for HC-05 on pins 10 (RX) and 11 (TX)?
easy
A. SoftwareSerial BT(11, 10);
B. SoftwareSerial BT(10, 11);
C. SoftwareSerial BT(9, 8);
D. SoftwareSerial BT(0, 1);
Solution
Step 1: Recall SoftwareSerial constructor syntax
The constructor is SoftwareSerial(rxPin, txPin), so RX is first, TX second.
Step 2: Match pins to constructor
Given RX=10 and TX=11, the correct call is SoftwareSerial BT(10, 11);
C. BT.begin baud rate does not match HC-05 baud rate
D. BT.read() should be inside setup()
Solution
Step 1: Check baud rate consistency
HC-05 default baud is 9600, but BT.begin uses 115200, causing communication failure.
Step 2: Verify other code parts
SoftwareSerial pins and Serial.begin are correct; BT.read() is correctly placed in loop.
Final Answer:
BT.begin baud rate does not match HC-05 baud rate -> Option C
Quick Check:
Baud rates must match for communication [OK]
Hint: Match BT.begin baud with HC-05 baud rate [OK]
Common Mistakes:
Using different baud rates between Arduino and HC-05
Placing BT.read() in setup() instead of loop()
Confusing Serial and SoftwareSerial pins
5. You want to send the string "LED ON" from your phone to Arduino via HC-05. The Arduino should turn on an LED connected to pin 13 when it receives this exact string. Which code snippet correctly implements this behavior?
The correct code declares a global String input = ""; and uses while (BT.available()) { char c = BT.read(); input += c; } to accumulate all available characters into the string.
Step 2: Compare the full input string and control LED
After accumulating, if (input == "LED ON") { digitalWrite(13, HIGH); input = ""; } turns on the LED and clears the input for next message.
Step 3: Analyze other options
One option incorrectly compares a single BT.read() char to 'LED ON'; another turns on the LED on if (BT.available()) without checking content; the last captures only a single char per loop with input = BT.read().
Final Answer:
The code snippet that accumulates characters into a String using while(BT.available()) -> Option A
Quick Check:
Read full input string before comparing [OK]
Hint: Build full string from BT.read() before comparing [OK]
Common Mistakes:
Comparing single char to full string
Not accumulating all characters before checking
Turning LED on without input check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.