Arduino Two Way Communication Via Bluetooth (HC-05)
by Aced Inventor in Circuits > Arduino
37618 Views, 21 Favorites, 0 Comments
Arduino Two Way Communication Via Bluetooth (HC-05)
In this project, we will be learning how to communicate between two Arduino boards by pairing two HC-05 Bluetooth modules connected to each of the Arduino Boards. Once you plugged both of your Arduino boards into a power source, the LED on the Bluetooth modules will blink fast until they have connected to each other. After they have connected, the LED will blink slowly, indicating the two have paired. For ease of understanding, we will name one of the Arduino boards "Arduino A" to which the master Hc-05module will connect. We also name the second Arduino board "Arduino B" to which the slave HC-05 will connect. There is one button and LED connected to both of the Arduino Boards. When you press the button on Arduino A, the LED on Arduino B turns on, and when you press the button on Arduino B, the LED on Arduino A turns on. And when you take your finger off the button, the LED turns off.
This project can still be improved.
Supplies
The components needed to make the project are:
HC-05 Wiring | AT Command
There are two ways you can get your HC-05 Bluetooth Module into AT command mode. If for some reason your HC-05 does not have a button or the button is not working you can wire as shown:
Arduino >> HC-05
GND >> GND
5V >> VCC
D3 >> TX
D2 >> RX
EN >> 3.3v
If your Bluetooth module does have a button all you have to do is to remove the EN pin from 3.3v. Now before you plug in the power cable hold the button and then plug it in.
Arduino >> HC-05
GND >> GND
5V >> VCC
D3 >> TX
D2 >> RX
To check if your Bluetooth module is in AT command mode you should see the Red LED blink slowly.
Uploading the Program | AT Command
The program for this project is not so long.
We are doing this in the first part of the program because if the Bluetooth module is available, they write to the serial monitor what the Bluetooth module is saying.
// Read from HC-05 and send data to the Arduino Serial Monitor if (Bluetooth.available()) Serial.write(Bluetooth.read());
If the serial monitor is available, we are writing to the Bluetooth module in the serial monitor.
// Read from Arduino Serial Monitor and send it to the HC-05 if (Serial.available()) Bluetooth.write(Serial.read());
Note: Make sure you have set the serial monitor to 9600 bauds and Both NL & CR
For the complete code, email me at:
acedinventor@gmail.com
Working With AT Commands
Now that we are wiring the HC-05, we will set it as a slave.
Slave:
Command | Response | Description
AT | OK | Check if the HC-05 is in AT Command mode
AT+ROLE=0 | OK | This makes the HC=05 a Slave
AT+ADDR? | <addr>, OK | Displays the HC-05's address
Make sure to copy the address because we will be using it later on. We are done setting up this HC-05 module, so take it off the breadboard and plug in a new one. We will set that HC-05 as a master.
Master:
Command | Response | Description
AT | OK | Check if the HC-05 is in AT Command mode
AT+ROLE=1 | OK | This makes the HC-05 a Master
AT+CMODE=0 | OK | This allows the HC-05 connect to a specified address
AT+BIND=00<addr> | OK | When the HC-05 turns on, it will look for this address
Make sure to replace the colins with commas when pasting the address of the slave HC-05 copied earlier.
HC-05 Wiring
Connect the HC-05 as following:
Arduino >> HC-05
GND >> GND
5V >> VCC
D3 >> TX
D2 >> RX
Note: The connection for both of the HC-05 modules is the same.
Button Wiring
Connect the Button as following:
Arduino >> Button
GND >> First pin of the Button
D4 >> Second pin of the Button
Note: The connection for both of the Buttons is the same.
LED Wringing
Connect the LED as following:
Arduino >> LED
GND >> Short Leg
D5 >> Long Leg
Note: The connection for both of the LEDs is the same.
Uploading the Program
Open both the programs named "Bluetooth_Master" and "Bluetooth_Slave". Once they have opened, compile the sketches to see if they are clear of error. Now make sure to select the correct COM port and then upload them. Make sure to upload the "Bluetooth_Master" to the Arduino board in which the HC-05 Master is connected and do the same for the HC-05 Slave. Once the programs have uploaded to the board the LED on the HC-05 will blink slowly until they have paired to each other. Once they have paired the LED will blink slowly.
For more information about this, please visit my Patreon:
https://www.patreon.com/acedinventor
Testing the Project
To test if the project is working properly, plug both Arduino boards into a power source. The LED on the HC-05 modules will blink slowly until they have paired. Once paired, the LED will blink slowly. As displayed in the image above, for ease of understanding, we will name one of the Arduino boards "Arduino A" to which the Master HC-05 will connect and the other Arduino board "Arduino B" to which the Slave HC-05 will connect. If you now press the button on Arduino A, the LED on Arduino B will turn on, and when you take your finger off the button, the LED will turn off. The same happens to the LED on Arduino A. When you press the Button on Arduino B, the LED on Arduino A turns on, and when you take your finger off, the LED turns off. The way this works is, for example, when you press the button on Arduino A, the Arduino boards send the character "b" to the Master HC-05, which then over Bluetooth sends the character to the Slave HC-05. Once the Slave HC-05 receives the character "b," it turns on the LED, and when you take your finger off the button, there is no data being sent to the HC-05, so the LED turns off.
If there is still something you don't understand, you can comment down below.