Bluetooth
|
In this project, you'll learn how to connect and control a Bluetooth module. Bluetooth is a wireless technology for two devices to communicate with each other over short distances. One device is designated as the master and the second device is the slave. In order for a master and a slave to communicate with each other, they must first be paired. A Bluetooth device works like a serial modem. You can communicate with it either using the wireless Bluetooth signal or a wired serial connection. To configure the Bluetooth device, you must use the wired serial connection, and it must not be paired with another Bluetooth device. |
|
Parts needed:
|
HC-06 Slave Only |
HC-05 Master/Slave |
There is a slight difference between configuring the HC-06 and the HC-05 modules
Configuring the HC-06 (Slave Only) Bluetooth module | |||||||||||||||||||||||||||||||||||||
| In this section you will learn how to configure the HC-06 (Slave Only) Bluetooth module. | |||||||||||||||||||||||||||||||||||||
| 1 | Making the connections
| ||||||||||||||||||||||||||||||||||||
| 2 | Create a new program in your Arduino IDE and copy the code on the right. |
| |||||||||||||||||||||||||||||||||||
| 3 | Upload and run the program. | ||||||||||||||||||||||||||||||||||||
| 4 | Open the Serial Monitor. The monitor settings should be:
|
| |||||||||||||||||||||||||||||||||||
| 5 |
The following is a summary of some of the available AT commands.
The commands are CASE sensitive.
After typing the command in the Serial Monitor, you need to
click the Send button to send the command to the Bluetooth module.
The response will be printed in the monitor.
| ||||||||||||||||||||||||||||||||||||
Configuring the HC-05 (Master/Slave) Bluetooth module | |||||||||||||||||||||||||||||||||||||
| In this section you will learn how to configure the HC-05 (Master/Slave) Bluetooth module.
There are two versions of the HC-05: +VERSION:4.0-20190815 with the gold check mark is the newer version +VERSION:2.0-20100601 without the gold check mark is the older version. The description below is for the newer version. | |||||||||||||||||||||||||||||||||||||
| 1 | Making the connections
After powering up the module the status led should blink slowly (4 second intervals) telling you that it is now in AT command mode. For the older version (without the gold checkmark) of the HC-05 (+VERSION:2.0-20100601), you need to push down the button before you power up the module to bring the module into AT command mode. The AT commands also work a bit different from what is listed below. | ||||||||||||||||||||||||||||||||||||
| 2 | Create a new program in your Arduino IDE and copy the code on the right. |
| |||||||||||||||||||||||||||||||||||
| 3 | Upload and run the program. | ||||||||||||||||||||||||||||||||||||
| 4 | Open the Serial Monitor. The monitor settings should be:
|
| |||||||||||||||||||||||||||||||||||
| 5 |
The following is a summary of some of the available AT commands.
The commands are CASE sensitive.
After typing the command in the Serial Monitor, you need to
click the Send button to send the command to the Bluetooth module.
The response will be printed in the monitor.
| ||||||||||||||||||||||||||||||||||||
Using Bluetooth to Control a LED | |||||||||||||||||||||||||||||||||||||
| In this section you will learn how to control a LED wirelessly using the Bluetooth module.
We will connect the PC wirelessly using Bluetooth to the Bluetooth module that is connected to the Arduino. The PC Bluetooth is the master and the Arduino Bluetooth is the slave. On the PC side, we will run PuTTy, a terminal monitor program, to send commands wirelessly to the Arduino. The Arduino will execute the on/off commands received from the Bluetooth module to turn on or off the built-in LED. | |||||||||||||||||||||||||||||||||||||
| 6 | Setup
If you are using the HC-06, just power it up. If you are using the HC-05 (Master/Slave) then you need to first set its role to slave with the command AT+ROLE=0 and then power it up into data mode (i.e. not AT command mode). The PC or your mobile phone is always the master, so your HC-05 must be in the slave mode, otherwise it will not show up in the Bluetooth scan list. | ||||||||||||||||||||||||||||||||||||
| 7 | Create a new program in your Arduino IDE and copy the code on the right.
Make sure that the baud rate specified in the BTSerial.begin(9600) line matches the actual baud rate of your bluetooth module. If it is not 9600 then change the value to match. |
| |||||||||||||||||||||||||||||||||||
| 8 | Upload the program to the Arduino. | ||||||||||||||||||||||||||||||||||||
| 9 | You may want to power up your Bluetooth module and Arduino as a standalone, i.e. not using the USB cable connected to your PC but connected to a battery. This way, you will know for sure that the communication is through the wireless Bluetooth. The LED on the Bluetooth module should blink. | ||||||||||||||||||||||||||||||||||||
| 10 | Pair the Bluetooth module to your Windows PC. (Note that this is just pairing and not connecting the module with your computer.) The following instructions are for Windows 11. Click here for Windows 7. | ||||||||||||||||||||||||||||||||||||
| 10a | Make sure that the Bluetooth on your Windows PC is turned on. | ||||||||||||||||||||||||||||||||||||
| 10b | In the Search, type Bluetooth and select Bluetooth and other devices settings | ||||||||||||||||||||||||||||||||||||
| 10c | If the Bluetooth module name is already listed in the Other Devices then you can go to step 10h. | ||||||||||||||||||||||||||||||||||||
| 10d | Click on Add Devices. |
| |||||||||||||||||||||||||||||||||||
| 10e | In the Add a device window, click Bluetooth. |
| |||||||||||||||||||||||||||||||||||
| 10f | It will take a few seconds to scan for nearby Bluetooth signals. Select the name of your Bluetooth module (RobotsForFun-BT) and type 1234 for the pin. |
| |||||||||||||||||||||||||||||||||||
| 10g | You will see the word Connected for a few seconds and then it will change to Not connected.
This is correct because you are only pairing and NOT connecting the Bluetooth module with your computer.
Click Done. |
| |||||||||||||||||||||||||||||||||||
| 10h | Back in the Devices window, click on More Bluetooth settings. |
| |||||||||||||||||||||||||||||||||||
| 10i | In the Bluetooth Settings window, click on the COM Ports tab and note down the COM port number for the Outgoing RobotsForFun-BT line. This COM port number will be used in step 11. |
| |||||||||||||||||||||||||||||||||||
| 11 | Download and run PuTTY.
PuTTY is a terminal app that you can use to connect and communicate with the Bluetooth module.
In the configuration screen,
|
| |||||||||||||||||||||||||||||||||||
| 12 | In the PuTTY terminal, type in a 1 followed by Enter to turn on the LED on the Arduino, and type in a 0 and Enter to turn it off.
Observe the LED on the Arduino to see that it is responding correctly. |
| |||||||||||||||||||||||||||||||||||