Friday, May 12, 2017

Arduino Two-Way Bluetooth with HC-05 module

Today we are going to learn how to establish two-way Bluetooth communications between an Arduino board and a PC. We will learn how to send data from the PC to the Arduino and from the Arduino to the PC using Bluetooth. In this step-by-step tutorial you will learn the basics of Bluetooth communications using the cheap and widely available HC-05 Wireless Bluetooth RF Transceiver Module.

HC-05 Bluetooth modules
The HC-05 is a Bluetooth V2.0+EDR (Enhanced Data Rate) SPP (Serial Port Protocol) module with 2.4GHz radio transceiver and baseband, designed for transparent wireless serial connection setup. It uses the CSR Bluecore 4-External single chip Bluetooth (BCR417) system with CMOS technology and AFH (Adaptive Frequency Hopping). It has a footprint as small as 12.7mmx27mm (without breakout board).

HC-05 Bluetooth modules (bare and w/ breakout board)

We must pay attention when buying these modules because there are several “flavors” of HC-05 Bluetooth modules. These modules cost about 5 USD and are available with or without a breakout board. The breakout board is a small “motherboard” where the Bluetooth module is soldered. Modules with a breakout board have a pin header, a led and sometimes also a button. Most HC-05 modules with breakout boards have also a voltage converter and can be powered from the Arduino 5V pin.

I recommend getting a version of HC-05 with a breakout board, because it’s much easier to work with when you are experimenting or testing. The most popular HC-05 modules appear to be the FC-114 and the ZS-40.

HC-05 Bluetooth module specs:
Bluetooth V2.0+EDR.
Typical -80dBm sensitivity.
Up to +4dBm RF transmit power.
Integrated antenna.
I/O: 1.8 to 3.6V.
PIO control.
UART interface with programmable baud rate.
Supported baud rates: 9600,19200,38400,57600,115200,230400,460800.
Auto-connect to the last device on power on as default.
Permits pairing device to connect as default.
Auto-reconnect in 30 min when disconnected as a result of beyond the range of connection.
Full HC-05 specs are available here.

5V<>3.3V level converter/shifterAll HC-05 modules seem to be using 3.3V logic level. Since most Arduino boards use 5V logic level, we need to use a logic level converter between the Arduino and the HC-05 module RX pin to convert 5V to 3.3V. We can use a level converter board or a simple 2 resistor voltage divider. In this tutorial I will use a 2 resistor voltage divider. There is no need to use a converter for the HC-05 TX pin because the Arduino works OK with the HC-05 3.3V logic.

Parts
Arduino UNO (others can be used)
HC-05 Wireless Bluetooth RF Transceiver Module
1x10K resistor
1x 22K (or 20K) resistor
Breadboard + Jumper wires

Step 1 – Connect the modules according to the circuit. 

Arduino + HC-05 Bluetooth circuit

Pay special attention to the supply voltage used by your module. If your HC-05 module uses 3.3V supply voltage you must connect it to the 3.3V pin on the Arduino, never the 5V. If you use excessive voltage you can destroy the module! 

The voltage divider is made with one 10K resistor and one 22K resistor, but you can use other values as long R2 is about 1/3 the sum of R1+R2. 

The HC-05 EN and STATE pins are not needed for this tutorial and are not connected. 

Step 2 – Power the Arduino board 
Apply power to the Arduino board by connecting to an USB connector on your PC or by connecting a power supply to the Arduino board DC input. The HC-05 module led should blink at a fast rate, signalling it is in data mode.

Step 3 – Pair the HC-05 Bluetooth module
Once the Bluetooth module is powered, it should be visible by other Bluetooth enabled devices like a cell phone, tablet or PC. For this tutorial we need the Bluetooth module to be detected by the PC, so your must enable or install a Bluetooth device (like a USB dongle). When Bluetooth is enabled the Bluetooth icon is shown in the O.S. system tray area. Click on it and select Add Devices. Your bluetooth module should be listed , usually as “HC-05”, click on it and then click Next. You will be prompted to enter the pairing code/password. The default code for most Bluetooth devices is either 1234 or 0000. Click Next. If the pairing is successful, you will see a confirmation message.

Remember that Bluetooth class 2 devices have a maximum range of about 30 ft (10m), but often reliable communication can only be made at distances shorter than 9 ft (3 m).

Step 4 – Upload the sketch file 
After the HC-05 module is paired, disable it by disconnecting from the power supply pin, otherwise you may not be able to upload the sketch. The project includes two files send_test and receive_test that can be downloaded from here. Open the send_test sketch on the Arduino IDE and upload it to the Arduino board. Once the upload is finished, reconnect power to the HC-05 Bluetooth module. 

Step 5 – Check if the Bluetooth module is transmitting 
The code on the send_test sketch will repeatedly send a text message via Bluetooth. Open a terminal program like RealTerm and check that this message is being received. Don’t forget to set the correct port and baud rate. The default baud rate for the HC-05 module is 38400. If you see garbled characters instead of text, you are most probably using the wrong baud rate. The ports being used can be seen on your PC’s device manager. RealTerm can be downloaded here.

HC-05 Bluetooth module send test with RealTerm

Step 6 – Check the Bluetooth module is receiving 
Disconnect power to the Bluetooth module. Open the receive_test sketch on the Arduino IDE and upload it to the Arduino board. This code will make the led on the Arduino board turn ON or OFF when you send 1 or 0 from the terminal program. Once the upload is finished, reconnect power to the HC-05 Bluetooth module.

HC-05 Bluetooth module receive test with RealTerm

Press the Send tab in RealTerm, type 1 and press the Send as text button. The led on the Arduino board should turn ON. Now type 0 and press the Send as text button. The led on the Arduino board should turn OFF. 

That’s it for now! We successfully tested bi-directional Bluetooth communication between the Arduino and a PC. Next time we will learn how to read and change default HC-05 Bluetooth module settings. 

Buy the parts
Buy the Arduino UNO from Banggood
Buy the HC-05 Bluetooth module from Banggood
Buy resistors from Banggood
Buy the Arduino UNO from Dealextreme
Buy the HC-05 Bluetooth module from Dealextreme
Buy resistors from Dealextreme

I may get a small fee if you use the links to buy the parts. Thank you for your support!

No comments:

Post a Comment