Arduino Bluetooth
Tutorial to help connect and configure Bluetooth modules to work with Arduino (in this case an HC-05 module that you can acquire here). This tutorial is designed for beginners to understand how to establish and test this communication.
I am confident that this tutorial is also useful for other microcontrollers.
Required Materials
- 1X HC-05 Bluetooth Module
- 1X Arduino (I use an UNO)
- USB Cable for connecting the Arduino (in case of the UNO, or other type of connection via a Serial port)
- 1X 1.2K Resistor
- 1X 2.2K Resistor
The idea is to use the resistors to create a voltage divider (for those who, like me, have an Arduino UNO where the voltage levels are 0-5V) to match the 0-3.3V levels present in the Bluetooth module.
- Mobile phone, tablet, PC, or any other device with Bluetooth and an application that provides a connection through the Bluetooth serial port.
Bluetooth SPP Pro For Android, I recommend this app. It needs no explanation, and it deserves ***** from me.
- SScom32 A useful and free application for Windows environment allows debugging of serial ports and includes some valuable options. (This application is what I use to develop some of my projects; if anyone knows similar options, I’d like to know.)
- Pinout HC-05 Modules It’s always handy when you start working to have the pinout of the components at hand. (Remember, the material is always right. We include some code)
- Datasheet HC-05 Bluetooth modules
Necessary ConnectionsCode
#include #define Reset 4 #define LED 13 SoftwareSerial BTserial(2,3); int count=0; // counter for buffer array String buffer; // buffer array for data received over serial port void setup() { // initialize the digital pin as an output. pinMode(LED, OUTPUT); pinMode(Reset, OUTPUT); Serial.begin(9600); BTserial.begin(9600); digitalWrite(Reset, LOW); digitalWrite(LED, LOW); } // the loop routine runs over and over again forever: void loop() { readBT(); count = 0; // set counter of while loop to zero buffer = ""; // clear all index of array with command NULL writeBT(); } void readBT(){ int i = 0; while (BTserial.available()) { // if data from softwareserial Bt module if(BTserial.available() > 0) { // reading data into char array count++; char c = BTserial.read(); buffer += c; } } Serial.print(buffer); } void writeBT(){ if (Serial.available()) // if data on hardwareserial port PC or notebook BTserial.write(Serial.read()); // write it to the BT module }
Test
To test the communication, make the connections as indicated in the diagram. Then connect the Arduino to the PC with the USB cable. Compile the code and upload it to the Arduino.
Now, just open the Arduino IDE Serial Monitor, and as soon as you connect to the Bluetooth with a device and start sending information to the Bluetooth, it will appear on your Serial Monitor.
The reverse is also true: everything you type in the Serial Monitor will be sent to the Bluetooth device paired with the Arduino’s Bluetooth.
Notes
Typically, Bluetooth modules come with a default serial baud rate of 9600 bps. This value can be changed, but you need to enter the Bluetooth module in AT mode.
To learn more about Bluetooth modules, how to configure them, and how to enter AT mode, click here. (coming soon)