0
0
Arduinoprogramming~10 mins

Bluetooth with HC-05/HC-06 module in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bluetooth with HC-05/HC-06 module
Power ON Arduino
Initialize Serial
Initialize Serial Bluetooth
Wait for Bluetooth Connection
Read Data from Bluetooth
Process Data / Send Response
Loop back to Read Data
The Arduino powers on, sets up serial communication with the computer and Bluetooth module, waits for Bluetooth connection, reads incoming data, processes it, and sends responses in a loop.
Execution Sample
Arduino
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600); // HC-05/06
}

void loop() {
  if (Serial1.available()) {
    char c = Serial1.read();
    Serial.print(c);
  }
}
This code initializes serial communication and reads characters from the HC-05/HC-06 Bluetooth module, then prints them to the serial monitor.
Execution Table
StepActionSerial1.available()Read CharacterSerial Output
1Setup: Serial and Serial1 start at 9600 baudN/AN/AN/A
2Loop start: Check if data available from BluetoothFalseN/ANo output
3Loop start: Data arrives from BluetoothTrue'H'Print 'H'
4Loop start: Check againTrue'i'Print 'i'
5Loop start: Check againFalseN/ANo output
6Loop repeats waiting for more dataN/AN/AN/A
💡 Loop runs continuously; stops only if Arduino is powered off or reset.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
cundefined'H''i'undefinedundefined
Key Moments - 3 Insights
Why does Serial1.available() sometimes return false even if I expect data?
Serial1.available() returns true only when data has arrived and is ready to read. If no data has arrived yet, it returns false as shown in steps 2 and 5.
Why do we use Serial1 instead of Serial for the Bluetooth module?
Serial is usually connected to the USB for the computer. Serial1 is a separate hardware serial port connected to the HC-05/HC-06 module, so we use Serial1 to communicate with Bluetooth.
What happens if we don't check Serial1.available() before reading?
If we read without checking, we might get no data or garbage, causing errors. The execution table shows we only read when available is true (steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what character is read at step 4?
A'H'
B'A'
C'i'
DNo character
💡 Hint
Check the 'Read Character' column at step 4 in the execution table.
At which step does Serial1.available() return false after reading data?
AStep 5
BStep 3
CStep 4
DStep 2
💡 Hint
Look at the 'Serial1.available()' column after data has been read in the execution table.
If we remove the Serial1.available() check, what likely happens?
AProgram reads data safely
BProgram may read garbage or -1
CProgram stops running
DProgram sends data to Bluetooth
💡 Hint
Refer to the key moment about why checking Serial1.available() is important.
Concept Snapshot
Bluetooth with HC-05/HC-06 on Arduino:
- Use Serial1 for Bluetooth communication
- Initialize with Serial1.begin(9600)
- Check Serial1.available() before reading
- Read data with Serial1.read()
- Process or forward data as needed
- Loop continuously to handle incoming data
Full Transcript
This example shows how to connect an HC-05 or HC-06 Bluetooth module to an Arduino using the Serial1 port. The Arduino starts by setting up serial communication with both the computer and the Bluetooth module. In the loop, it checks if data is available from the Bluetooth module using Serial1.available(). If data is present, it reads one character with Serial1.read() and prints it to the serial monitor. This process repeats continuously, allowing the Arduino to receive and display Bluetooth data. Checking Serial1.available() is important to avoid reading when no data is present, which could cause errors. The code runs indefinitely until the Arduino is powered off or reset.