This tutorial demonstrates a simple way to make use of bluetooth for reading data in arduino projects.
The aim of this tutorial is to gather sensor data from a simple temperature sensor and transfer it to the PC using bluetooth communication. On the PC side, a simple arduino sketch is written, which will be uploaded onto the arduino board. We will then be able to see the sensor data and time on the serial monitor.
For this experiment, you will need the following things:
- Microcontroller (I am using an Arduino Uno)
- Bluetooth communication module (I am using the cheap and simple-to-use HC-05 module)
- USB cable
- Jumper Wires
- Temperature sensor (I am using the LM-35 temperature sensor)
Step 1: Setting Up the Circuit Connections
Before we begin, we need to setup the arduino board circuit connections. The diagram is given above.
Note that only after setting up these connections can you set up your bluetooth connections betweeen the PC and the bluetooth module.
The HC-05 bluetooth modules are usually connected to the voltage lines through a voltage divider circuit, as it may not be able to sustain a 5V line, and may get damaged. The voltage divider circuit connections are not given in this tutorial, you will have to look it up. However, my module seemed to be working fine on the 5V line. To stay on the safe side, connect it to the 3.3V line.
Also note that the RX pin of the bluetooth module is connected to the TX pin of the arduino, and the TX of the bluetooth module to the RX of the arduino.
Step 2: Setting Up the Bluetooth Connections
Once you have successfully set up the circuit connections, your bluetooth module should start blinking indicating that it is functioning properly and is ready to be paired with your PC.
If your PC does not have an in-built bluetooth, then you will have to use a bluetooth dongle and set it up. If your PC has an inbuilt bluetooth, then go to Control Panel -> Hardware and Sound -> Add a Device; or go to the bluetooth icon on the task bar and select "Add a Device". The HC-05 bluetooth modle should be detected by your PC. Once it has been detected, pair the HC-05 module with your PC. The pairing code will either be '1234' or '0000'.
Once you have done pairing the HC-05 with your PC, we can move on to the arduino codes.
Step 3: Arduino Code
Once the bluetooth module has been paired, open your arudino IDE.
Download, understand and verify the sketches given below.
The code is very simple and starightforward. You do not need to download or include any special libraries.
Step 4: Uploading Your Arduino Code
This is an important step in this tutorial. You need to get this part correctly for your experiment to work.
Before uploading, verify your sketches and make sure you have understood it so that you can change it according to your requirements.
Once you have verified the sketch, choose the correct port to which your arduino board is connected and select the correct board also, by going to Tools -> Port and Tools -> Board respectively. When I did my experiment, my Arduino Uno was connected to COM3.
Once you have chosen the correct port and board, disconnect your TX and RX pins from the arduino board. Only then should you upload your arduino code. If you do not disconnect your TX and RX pins, then you are likely to get an error saying that your COM port is busy. Once you have done the disconnection, upload your code. After uploading your code, you can connect the RX and TX pins back to their orginal positions.
Step 5: Receiving the Final Data
Once your arduino code has been uploaded, you can connect a power source to your arduino board and disconnect the USB cable connecting your arduino board to the PC. This will demonstrate that the Arduino board can remotely gather sensor data and tranfer it to the PC using bluetooth communication without being connected to it by a USB cable.
However, we are not done yet. To see the sensor data on the serial monitor, we need to first find out on which COM port your bluetooth module is connected. To find out, click the Bluetooth icon on the task bar, select 'Open Settings' and click on the 'COM Ports' tab. Under this tab, you will see your HC-05 module connected to some COM ports. Take note of the 'outgoing' port. In my experiment, my 'incoming' port was COM4 and my 'outgoing' port was COM5.
Once you have taken note of the 'outgoing' COM port, go to your Arduino sketch, click Tools -> Port and select the port which corresponds to the 'outgoing' port you had taken note of. Once you have done this, you can open the serial monitor for that port; and the temperature sensor data should be dislayed along with the time. To view the data from zero seconds onwards, press the 'reset' button on your arduino board.
Step 6: Troubleshooting
If all goes well, you should be getting the temperature and time data on your serial monitor at time intervals of 2 seconds. However, the data transfer may take a little longer, and small delays (few milliseconds) between successive readings is normal and expected.
Yet, you are likely to come across errors or problems during this experiment if you are new to using bluetooth communication with arduino.
Make sure of the following points to avoid any problems:
- Ensure that all your connections are firm and correct. Recheck your TX and RX pin connections.
- Make sure that your bluetooth module is connected properly. To stay on the safe side, you may use a voltage divider circuit (not given in this tutorial, you will have to look it up) or simply connect your HC-05 Vcc to 3.3V and not 5V.
- Your bluetooth module may stop working due to a loose connection. Make sure that the LED on the bluetooth module is continuously blinking, which indicates that it is powered.
- Choose the correct COM ports and boards while uploading your sketches. Also remember to choose the correct 'outgoing' COM port of the bluetooth module to view the data on the serial monitor.
- Upload your sketches in the correct procedure as described in step 4. Remember to do the disconnection/reconnection while uploading your sketch (refer to step 4).
- I have not tested the range of the HC-05 bluetooth module, so make sure it is well within range of your PC before experimenting with it.
Thats it for now. Bluetooth communication in arduino is simple and very frequently used. Once you are comfortable with it, you can use it to both send and receive data as well.
Thank you for going through this tutorial.