Home ARDUINO Getting Start with Arduino and Bluetooth Module HC-06/ HC-05

Getting Start with Arduino and Bluetooth Module HC-06/ HC-05

Arduino Bluetooth: Bluetooth is perhaps the most common type of connection for short distances, which is used by most modern electronic devices. Telephone headsets, headphones, keyboards and mice, printers, and God knows what else gadgets.

Some time ago, Bluetooth was distributed as a means of transferring files between phones.

arduino bluetooth

The main advantages of BT can be called a good resistance to broadband interference and ease of implementation. The first means that a bunch of devices in the same place can simultaneously communicate with each other, without interfering with each other. The second helped the widespread adoption of Bluetooth in the DIY environment, and in general in all environments.

Personally, I use BT to control robots from a smartphone. In Google Play, there are already several applications with a user-friendly interface for exactly this purpose.

The most accessible Bluetooth modules today are HC-05 and HC-06. They are in abundance in Chinese online stores and on Amazon or Aliexpress. The differences between them are that the first can work in both master (slave) and slave (master) mode. The second is a pure slave device (but it is fixable!). In other words, the HC-06 cannot detect the paired device itself and establish a connection with it; it can only obey the master.

Both devices are based on the CSR BC417 chip, which supports Bluetooth version 2.0 at speeds up to 3 Mbps. It is about these modules will be discussed further.

Collect Hardware For Bluetooth Module HC-06/ HC-05

1.Execution options

Typically, modules are sold as two boards that are soldered together. The smaller of them is the factory module, widely used in various electronic devices. Large – a special design scarf for DIY. This is the smaller board with the BC417 chip:

 

arduino bluetooth

And so DIY modules HC-05 and HC-06:

Getting Start with Arduino and Bluetooth Module HC-06/ HC-05

 

For my insidious goals, I usually take the HC-05, since they do not differ much in price from the HC-06, and allow us to connect two devices together without any problems. For example, you can make a robot and remote to it. From China, I usually get modules, exactly like in the left picture, but sometimes without a button.

Another variant of the design scarf is very common. Unlike those shown above, they do not have an EN foot, but there is a KEY foot, which is a bit more comfortable.

Getting Start with Arduino and Bluetooth Module HC-06/ HC-05

Pinout

So, for what the legs are responsible for all variants of the modules.

  • EN – on/off module;
  • VCC – power + 5V;
  • GND – land;
  • TXD, RXD – UART interface for communication with the controller;
  • STATE – status indicator;
  • KEY – foot to enter AT mode commands.

The module is configured in the AT command mode, which is enabled using the KEY foot. From here, many happy owners of a module may have a question: what to do if I got a module without KEY?

Getting Start with Arduino and Bluetooth Module HC-06/ HC-05

In fact, of course, there is this leg on the small board, it’s just not divorced on the big board. The devil knows why the Chinese did it, but the problem is solved easily. KEY-foot grows from here:

2.Connection to Arduino Uno

The legs are not so many, so it will not work. We connect to Bluetooth to Arduino Uno as follows:

Bluetooth GND VCC Txd Rxd KEY
Arduino uno GND + 5V RX TX

TX and RX are connected to GPIO feet 10 and 11, as the hardware UART on legs 0 and 1 will be busy communicating with the computer.

In the operating mode, the KEY does not connect anywhere, so in the table, I did not connect it with the Arduino’s GPIO.

3.Conjugation of voltage levels

The small band of the Bluetooth module has a logic voltage of 3.3 Volts. This means that Arduino Uno can either burn ports at it, or simply transmit signals incorrectly. Fortunately, in most cases, a large board has everything on board to avoid it. The above modules are easily connected to the Arduino, without unnecessary problems. Even despite the fact that the manufacturers themselves wrote a 3.3-volt signal level warning on the shawl.

However, in some situations it may be necessary to put a voltage divider on the Arduino TX – BT RX line to lower the logic levels. In fact, even if everything works without a divider, it is better to install it, for order. The divider scheme is below:

arduino bluetooth

4.Arduino preparation

In order to start customizing, we need to sew an auxiliary sketch in Arduino. In essence, a repeater program that will exchange data between a terminal on a computer and a Bluetooth module. The program is extremely simple:

#include <SoftwareSerial.h>

#include <Time.h>

 

int gLedPin = 13;

int gRxPin = 10;

int gTxPin = 11;

 

SoftwareSerial BTSerial (gRxPin, gTxPin);

 

void setup () {

# 38400 – for method number 1, 9600 – for method number 2

BTSerial.begin (38400);

Serial.begin (9600);

delay (500);

}

 

void loop () {

if (BTSerial.available ()) {

Serial.write (BTSerial.read ());

}

if (Serial.available ()) {

BTSerial.write (Serial.read ());

}

}

Load the sketch on Arduino and proceed to the next step.

5.Terminal preparation

As a terminal, you can use the port monitor built into the Arduino IDE, or any other monitor. I used a third-party terminal TeraTerm. Before connecting, you must make two important settings:

exchange rate: 9600;

Line feed character: CR + LF.

Here are the settings in TeraTerm:

arduino bluetootharduino bluetooth

When everything is properly configured, connect to the COM port and check the connection. All further manipulations are rationally divided into two parts: for the slave module HC-06 and for the master HC-05.

6. Setting up the HC-06

Connection check

After energizing the module, the LED will blink vigorously:

arduino bluetooth

 

Now go to the terminal and write the command: AT

In response, the module should tell us: OK

Our next team will ask the module for its version: AT + VERSION?

We get something like this: OKLinvor1.5

Happened? If not, check:

  1. The connection speed with the module, which is set in the program-translator: for communication with the slave, the speed must be 9600.
  2. Newline character settings: don’t forget about CR + LF!
  3. Do we press Enter after each command? You never know …

Useful commands

The slave Bluetooth module has only three parameters to configure:

  • The command to change the password on the device: AT + PIN <password>
  • speed change: AT + BAUD <speed>
  • change device name: AT + NAME <name>

For example, to change the password, we write: AT + PIN4321

In general, ready! Now you can connect with the module, for example, from a smartphone. This will be discussed in the second part of my story.

Related Post How to Build game using Arduino UNO

7. Configure HC-05

Switch to AT mode

To access the configuration of the master module, it must be switched to AT command mode. To enter this mode, you can use two methods (I always use the first one).

First method:

  1. Disconnect the module from the power supply.
  2. Serve on foot KEY signal + 3.3V.
  3. Turn on the module.

In this embodiment, the exchange rate with the module is set to 38400.

Second method

  1. Turn on the module.
  2. Serve on foot KEY signal + 3.3V;

In this case, the exchange rate with the module will be equal to the standard settings. The default is 9600.

After a successful transition to the AT command mode, the LED on the module will flash once every two seconds.

arduino bluetooth

HC Connection Test

Check the command mode of the HC-05 as in the case of the slave module:

>>: AT
<<: OK
>>: AT + VERSION? 
<<: + VERSION: 2.0-20100601

Setting up the HC-05 as a slave

To turn a module into a slave, you need to run several commands:

Reset previous settings: AT + ORGL

Reset paired devices: AT + RMAAD

Password setting: AT + PSWD = 1234

Enable slave mode: AT + ROLE = 0

Additionally, you can find out the address of the device (you need to configure the paired module): AT + ADDR?

In response, we get the address itself: ADDR = 12: 6: 143117

After setting, disable the leg KEY from + 3.3V, and restart the module. Done!

Setting up the HC-05 as a master

From the factory, the module goes as a slave, and in order to make it the master, you will need to execute the following commands.

Reset previous settings: AT + ORGL

Reset paired devices: AT + RMAAD

Enable master mode: AT + ROLE = 1

Restart after changing roles: AT + RESET

If we want to connect the slave and the leader, we write the following commands:

Setting the slave password: AT + PSWD = 1234

Specify the pair device: AT + PAIR = <address>, <timeout> (example: AT + PAIR = 12,6,143117, 5 )

We associate with a specific address: AT + BIND = <address> (example: AT + BIND = 12,6,143117 )

Forbid to connect to other addresses: AT + CMODE = 0

Done! Now the module is connected with the slave, and each time it is turned on, it will try to connect with it.