SHARE

What is Bluetooth?

Bluetooth is a wireless communication protocol. It was born in the late 1990s and that has really caught on in the 2000s it has undergone many revisions and changes to a current version 4.1 since late 2013.
Originally, the Ericsson company was seeking to develop a way to use a serial port without using a wire. Gradually, its uses have expanded to reach what we know today, a way to connect many devices without wire, ranging from Arduino and its route set to a computer, or to use headphones or a joystick on a home console.

Let’s go back a little in detail. This protocol is a WiFi cousin because they respect both the same IEEE specification and use the same frequency range. 2.4 GHz (like mobile phones and ZigBee for example). This is a bidirectional communication, two modules can communicate simultaneously. The behavior used is “master / slave”. A slave may speak with a single master, but a master can communicate with several slaves.
For its use, it happens in several stages:
  1. The master goes into recognizable” mode.
  2. The slave find the master and asks to connect to it.
  3. The master accepts the connection.
  4. The devices are then paired (or associated).
    Communication can begin
Then, depending on the type you use component (a Bluetooth headset, a video game controller etc.) the communication can be done according to a protocol or another. In our case it simply consist of a series connection. In the end, we will have the same operation that a link usual series (Part 3 of the Arduino tutorial) but the whole “wired” part of the communication will be encompassed within the frames managed by the Bluetooth module. This is totally transparent to us (or almost). So do a little more acquaintance with the HC-05″, the small Bluetooth module.

 Overview, the HC-05 Bluetooth module.

bluetooth moduleAs explained earlier, the module used is called HC-05 and is easily findable for a few dollars (via import from China sites). It is as big as the thumb and is actually an assembly of a Bluetooth module on a small PCB. This helps to overcome some constraints such as welding of the module (which is very difficult), the conversion 5V -> 3.3V, the regulation of supply (3.3V again) or the addition of signal LEDs . All this is already built!

what we find on this small module?
First, a set of pins. VCC and GND for the power supply (5V), Rx / Tx for communication. We also see there a pin “Key” to be used to send configuration commands to the module (discussed later). The last pin named “Led” to connect a LED to get a signal on the module status.

Connect the HC-05 Bluetooth module to the Arduino.

Let’s see how to connect everything. First, the supply. It is quite common, the VCC will be connected to the Arduino 5V and GND to ground. Then come the data transmission pin, Rx and Tx. You can choose to connect to the Arduino serial connection pins (0 and 1) to use the native serial port, and then plug them into any other pin to keep the serial port available for debug with computer (which I recommend). We return to this point. He then left the Key and LED pins. Led used to connect a status LED, you can leave it disconnected it will not influence the module behavior. Key used to use the “command” of the module. With the latter you can reconfigure the serial port (speed, parity etc …) and other Bluetooth related options (module name, password pairing, slave / master mode …). This pin is to connect to any digital output of the Arduino.

Communication between HC-05 Bluetooth Module and Arduino.

 Now that the module is connected, it’s time to implement it with our Arduino! To keep things simple, we’ll just make a connection that is used to return all the way on the computer’s serial. Throughout this tutorial, only the mode “slave” will be used but the “master” mode is not much more difficult to achieve.
As has been repeated throughout the presentation, the HC-05 module works via a serial port. On an Arduino Uno we can find only one that we will keep to the debugging with the computer the time to when implementing the module. So there is a problem, how will we do to communicate with the module if the serial port is already taken?
 The answer is quite simple, we are going to emulate a serial port! Thus, we can use two conventional digital pins to create a serial port on top. This is a fairly common method of circumvention, it is equivalent to find a software solution to a hardware problem.
To implement this there are two schools: Encode oneself the new serial port or use an existing library. If you have time, the first solution is really interesting and formative. For me, time is a commodity that rarely in doing so I will use an existing library that was encoded by professionals and tested by thousands of people around the world: SoftwareSerial.

Using the Library

To implement this library is pretty simple. It will initially have to include it in your project. Choice or by clicking on “Library / Import / SoftwareSerial” in the Arduino IDE (base included) and then adding the following line to the top of your program:

#include <SoftwareSerial.h>

Once the library is there, we will use it. This library gives us access to an object named SoftwareSerial. We can create an instance of this object by writing:

SoftwareSerial myserialpath(Rx, Tx);

Rx and Tx will be the pin numbers on which are connected the transmit / receiving pin of your Bluetooth module. For example, I myself have the module connected Rx pin on pin 10 of the Arduino and the Tx pin connected to a 11. So I have in my program:

SoftwareSerial myserialpath(10, 11);

Once this is done, everything is ready! Indeed, this library provides the same functions as the usual serial port, the only difference will be that instead of using Serial we will use myserialpath as shown in the listing below:

myserialpath.begin(speed);// the path starts at the given speed range
myserialpath.available(); // returns the number of characters to read
myserialpath.read();     // returns the next character received
myserialpath.write(val);  // sends the char "val" to the serial port

By default Our module operating at 9600 baud, we can prepare it by writing:

#include <SoftwareSerial.h>
SoftwareSerial myserialpath(10, 11);
void setup() {
myserialpath.begin(9600);
}

Then to test this, we will make a short code which will just do a “echo” between the computer linked to the serial port and the one linked to Bluetooth module. Thus, each time a character / text will be received on the series of one way, it will be returned on the series of the other channel. Our Arduino then serve as relay / adapter between the computer USB and bluetooth module. This small program then gives you everything you need to know to make your first applications. Just one thing: connect pin “Key” to ground to make the module is in “communication” mode and not in command mode.

#include <SoftwareSerial.h>
SoftwareSerial myserialpath(11, 10); // (RX, TX) (pin Rx BT, pin Tx BT)
void setup()
{
// Opens the serial port with the computer
Serial.begin(9600);
// Opens the serial path with the BT module
myserialpath.begin(9600);
}
void loop() // run over and over
{
if (myserialpath.available()) {
Serial.write(myserialpath.read());
}
if (Serial.available()) {
myserialpath.write(Serial.read());
}
}

Going further, the configuration (AT)

If you want to use more features with your Module, you will have to change its internal parameters. To be set, the module uses a command set more or less standard called “AT command” (ATtention) or Hayes Command the name of their inventor. To use them, will require that the module is placed in a particular mode, the control mode. In this mode, all information sent to the module will be performed for the configuration, nothing will be sent to another Bluetooth device.

AT commands are pretty good name. Indeed, all configuration commands begin with the characters “AT + followed by the order in question and then return to the line via the characters “\ r \ n”. Let’s see it in more detail.

Switch to command mode

For the module decides that what he receives should be treated as a configuration order and not as a piece of text to be transmitted, it will take place in “command”. In this mode, text is no longer transmitted.
To establish this communication, there are two methods.
The first is to start the module with its pin “Key” to the UP state (connected to 5V). The LED on the module should then flash quickly, indicating that the module is ready to receive AT commands. In this configuration, all sent messages will be interpreted as commands and the only way to escape will be to restart the module (by removing its power supply). Attention in this mode the connection is made at a speed of 38400 baud! Remember to take into consideration in your program. I recommend the first method is actually quite constraining.

A second, simpler method is to use the pin “Key” in our program. It is therefore in a first time declare it as output and put it in the low state by default. It will be connected to pin 12 for me. In this situation, the speed remains at 9600 baud.

const int key = 12;
void setup() {
pinMode(key, OUTPUT);
pinMode(key, LOW);
}

Now every time you want to pass in the “control” mode you have to put this pin to the high state and hold in this state until you send commands.

send commands

As I wrote earlier, each command sent must start with “AT + …” and end with a carriage return “\ r \ n”. There are several commands that are generally found in the documents provided by the manufacturer. You can find this command set by doing a search like “HC-05 AT commands in your favorite search engine or by downloading this file.

What do we see here? From page 3, we can see a series of commands and their return. Let’s try this.
To do this, first pass your module to command mode by putting his Key pin 1.
Now, repeating the earlier program that making an echo in the serial port, send the following string “AT + NAME”. This command provides parameter passed without visible module name. By default you should get HC-05″ in return. Now if we wanted to change this name, we would do “AT + NAME = TEST” to call “TEST” for example. If all goes well, the module should respond “OK.” It’s that simple!
In general, for reading a parameter we will do “AT + [parameter]” and it will change the AT + [parameter] = [value].

When using the module with no external serial connection to check, it may be appropriate to ensure that all goes well, making sure the module returns OK” when you change a setting. If at the beginning of command mode you want to verify that the module is present and ready to respond, you can simply send “AT” and he will answer “OK”!

You should now be able to use your Bluetooth HC-05. It remains only to encode a small application on your PC or phone and you can go completely autonomous remote control, connected wirelessly to a computer to receive orders!

 

Leave a Reply