Using the HC-05 Bluetooth

De Wiki do Bernardino
Revisão em 23h43min de 30 de março de 2016 por JPB (Discussão | contribs) (Module Configuration)
(dif) ← Revisão anterior | Revisão atual (dif) | Revisão seguinte → (dif)
Ir para: navegação, pesquisa

Module Configuration

The module default communication configuration use the device name HC-05 and communicate at 9600,N,8,1. We certainly want to change the speed and it would be nice to change the name as well. Thankfully - the chipset has an AT command mode you can enter which allows you to reconfigure the device settings. The changes you make using this method are persistent and will be preserved across power cycles.

To talk to the module for configuration I am using a 3.3V FTDI Friend cable to provide power as well as communications lines. The next diagram shows how to connect the devices together. HC05-FTDI232.png

According to the datasheet to enter AT command mode:

  • Press switch while power is applied to the module it will boot into command mode and set the serial port to 38400N81 regardless of the current serial port settings on the device.

For this example we are going to use the first method. First, connect all wires except the 3.3V ones. Plug the FTDI adaptor into your USB port and connect to it with your favourite terminal program at 38400N81 (I use Linux UBUNTU with CuteCom). Make sure your serial program is configured to send CR/LF (\r\n) line endings. Press switch and power on your module HC-05, the red LED on the board should start flashing slowly (about once every two seconds).


To test the connection, try the following command (in these examples the < and > characters indicate the data transfer direction - don't type them).

That example simply sends the basic *AT* (attention) command and the device should respond with a simple *OK* to indicate that it got it. If you don't get the *OK* response double check your wiring, the voltage level and your serial port settings.

Once you have access to command mode there are a wide range of options you can change (see [the datasheet]( for a full description of what is available). I have already download two, Ficheiro:DatenblattHC-05 BT-Modul.pdf or Ficheiro:HC05 reference.pdf. In this post I'm just going to concentrate on some of the most common options.

The first thing we will want to change is the serial configuration - 9600 is way to slow for most purposes (and my [software UART](/a-software-uart-for-the-attiny85/) implementation won't work at such low speeds anyway). The following commands will do the trick ...

> AT+UART?     
< OK     
< +UART:9600,0,0    
> AT+UART=57600,0,0     
< OK    
> AT+UART?     
< OK     
< +UART:57600,0,0

In this case we are querying the current settings ('AT+UART?') and the device responds with the currently active settings (9600 N81 in this case). Next, we tell the device to use 57600 baud, no parity and 1 stop bit ('AT+UART=57600,0,0' - the command format and parameters are described in the datasheet). Just to check that everything is saved away safely we query the current settings again to make sure they match what we just set.

The next thing to change is the device name - this is what appears when you browse for available Bluetooth devices. The default name for the module is HC-05 so something that better reflects what you are connecting to would be nice. Use this sequence of commands ...

Once again we query the device for the current settings, change it to the value we want (in this case '*BLUEBELL*' which is what I've decided to call the device I'm working on) and then query it again to make sure that our changes have gone through.

Finally, we want to change the pass-code used to pair with the device from the default value (1234) to something that is (presumably) secret

> AT+PSWD?     
< OK     
< +PSWD:1234    
> AT+PSWD=0000     
< OK    
> AT+PSWD?     
< OK     
< +PSWD:0000


On my system the pairing process results in a new serial port called /dev/rfcomm0 which I can connect to with my normal serial terminal (in my case I use CuteCom which is perfect for development tasks). Connecting on this port at 57600 now lets me talk to the code running on the ATtiny.

The sample program I'm using simply echoes back all the data it receives and converts lower case letters to upper case just to prove it is not going to some internal loop-back. The image to left shows the results in CuteCom.

The sample program is very simple - this is what the main function looks like:

void main() 
          uint8_t ch = uartRecv(); 
             ch = (ch - 'a') + 'A'; 
             ch = (ch - 'A') + 'a';