Tonight, I start working on a third project I have in my hack list for the moment, this one is based on bluetooth and I order some arduino stuff to make it live. The arduino itself is a leonardo and the bluetooth is a hc-05 attached to a larger shield to be connected with arduino board directly.
This article details how to use HC-05 to transfer data between arduino & mobile device based on Bluetooth. It includes HC05 source code for pairing and communicating.
Click on link to get more
Some reference link
As all that stuff is coming from China miniinthebox shop, for sure I have no documentation on all that stuff, so the starting point is to get some elements :
- http://mbed.org: also provides pinout with informations
- http://www.instructables.com : best pdf file with hc-05 AT command
- ftp://imall.iteadstudio.com : provides the complete datasheet
- http://www.usconverters.com : provides the BC417 (hc05 bt component) datasheet
- http://www.instructables.com : this site gives some elements on hc-05 pinout
- http://jimsrobot.com : provides some interfacing and reseting informations
- http://cu.rious.org : provides some sample code
- https://github.com/jdunmire/HC05 : is a github project with hc05 source code
- http://arduinobasics.blogspot.fr : some sample code
- http://bluetooth-pentest.narod.ru : to get device class computation
Basically, there are 4 important pin on hc-05
- Pin 34 – PIO11 – also named KEY is used to set the mode (must be set before switching on the device)
- High = AT command mode – used for chip configuration
- Low = Transparent mode – directly transfer data
- Pin 32 – PIO09 – also named LED2 indicates pairing
- Pin 31 – PIO08 – also named LED1 indicates status
- Pin 1 – TX – serial transmit
- Pin 2 – RX – serial reception
On the shield, here attached to a Leonardo card you can see the switch between AT command mode and DATA (transparent) mode. This has to be set before starting as it is taken into account at startup.
You can also see the UART PIN selection jumper, here configured to use the Serial1 interface.
To well understand the AT(CMD) mode and the DATA mode, in the CMD mode you manage manually the communication to the device and once the link is established the system automatically change to a data mode. The DATA mode is an automatic mode, you configure it once (with name, password…) in CMD mode then reset to DATA mode and it operates automatically and the serial communication with HC05 is used to transmit data. I issued some problem to connect to all devices and for iPhone for example I had to work as master to conect to iPhone when it work well with a macbook as slave or android. So I choose to use the CMD mode to be able to manage entirely the pairing and communication flow and this is what will be documented here.
After some later investigation, I’m not sure is was the best idea, as the generated code is complex and big for arduino. By the way, i made it mostly because of the iPhone and it seems that iPhone won’t work because iPhone does not support RFCOMM/SPP offered by this device (as much as I read in the forum). The solution for iPhone could be around BLE equipments, some are supported like this one, I’ll investigate later ; the problem in this case is that BLE does not work with most of android… so it’s hard to make it work !
So you can consider the following code as a way to understand what the HC05 is doing when you are in DAT (automatic mode) even if the real algorithm for pairing is I assume really different.
Another interesting learning is about the bluetooth rate we can obtain with such devices. as much as I understand, most of the bluetooth shields have two operation mode. The first one is a managed mode where the shield manage the bluetooth protocol and offer a serial interface to the master processor. This is usefull as you don”t have to manage the bluetooth stack, but the rate is limited by the capacity of the shield, generally to 100Kbps. The second mode is a transparent mode where the bluetooth protocol is bypassed and managed by the master CPU. In this mode the limitation are broken, but to manage the stack you must have a powerfull CPU. So do not imagine to proceed high rate transfer over bluetooth with an arduino. If any way you find a solution, i’m really interested in, so send me a comment with references.
Some elements about BlueTooth pairing process
As you know before being able to communicate over bluetooth, devices must be paired. I’ll describe my understanding of this process but for sure, you’ll find better documentation on the web with thousands of pages…
In the pairing process you have a first phase of discovery : slaves are sending inquiry at a regular basis. Master is listening to these inquiries.
Slaves have a class indicating the type of the device. Master can search for specific class. Each device have a specific address. In my different test, I found that HC-05 can be detected as a slave by my MacBook but only as a Master by my iPhone. So in the pairing algorithm I had to implement both systems :
When no pairing has yet been done the system must start by pairing. In a first step, the system is configuring as s