Introduction
In my previous article (Bluetooth Messenger: Learn to Use the HC-05 Bluetooth module with your Arduino[^]), I showed you how to begin using the HC-05 Bluetooth component with your Arduino.
However, you had to use the component with its default settings. In this article, I will show you how to configure your HC-05 so that it has a custom name and a pin code that you set. We will also take a look at the commands you can send to the HC-05 and investigate how the component responds to a number of them.
The component can be quite cranky and there seems to be various and confusing information on the web about it so I hope to pull it all together here.
It's actually wired up just the same as the one in the previous article with just four wires. However, to put the HC-05 into full command mode (more later), we also implement one additional wire from the Arduino 3.3V pin to the HC-05 EN pin. Here's a snapshot of the circuit so you can get an idea of how simple it is.
Background
If you read the first article and built the circuit, all you have to do is add that one wire (from 3.3V to the EN pin) and you are ready to upload the new sketch so you can configure the HC-05. Here's the new schematic so you can plainly see how the Arduino Uno will be wired up to the HC-05.
Arduino Sketch: Send Commands, See Responses
Once you've built that simple circuit, you are ready to upload and run the new sketch which will allow you to send commands to your HC-05 and see responses.
Reiterating Importance of Not Using Arduino Pins 0 and 1
Notice that we are not using Arduino Pins 0 and 1 which are the hardware Rx (receive) and Tx (transmit) pins.
Arduino pins 0 and 1 are used by the Arduino IDE when you hook your Arduino to your computer via the USB port. It uses pin 0 (Rx) to upload your sketches to the Atmega328 chip on your Arduino Uno board.
It also uses pins 0 and 1 when you open up the Serial Monitor program in the Arduino IDE. We are going to use Serial Monitor to send the commands to the Bluetooth module so that is why we use alternate digital pins for our HC-05 to communicate over.
Software Serial
With our circuit, we hook the Rx (receive) and Tx (transmit) pins of the Bluetooth component to the Arduino digital pins 10 and 11. We then use those pins as Software Serial pins -- basically just tell the Arduino sketch to use those pins for serial communication.
I am attempting to emphasize all of this, because when I started out and was trying to work with the HC-05 a couple of years ago, I suffered through some serious challenges because no one clearly explained these issues and I was stuck for quite some time.
Let's take a look at the simple Arduino sketch to see what it does.
Configuring the HC-05: The Code
Here's the entire code listing that you will need to communicate with your HC-05 in command mode.
#include <SoftwareSerial.h>
SoftwareSerial BT_Serial(10, 11);
void setup() {
Serial.begin(9600);
Serial.println("Enter AT commands:");
BT_Serial.begin(38400);
}
void loop()
{
if (BT_Serial.available()) {
Serial.write(BT_Serial.read());
}
if (Serial.available()) {
BT_Serial.write(Serial.read());
}
}
As you can see, we include the Arduino (standard) library called SoftwareSerial
. That library provides us with the functionality to turn the Arduino's digital pins into Serial receivers and transmitters.
Some Limitations of SoftwareSerial
There are some limitations to the Arduino Uno and other Arduino hardware. For example, the max serial data rate is 57600bps. The Arduino just cannot receive data faster than that. The additional details can be found at the official Arduino site here^.
Create SoftwareSerial Object For Our Use
On the second line of the sketch, we create a SoftwareSerial
object that we will use throughout the sketch. That is the Serial device that will receive and transmit bytes to and from the HC-05.
Next in the setup()
method, we initialize the hardware serial (referred to as Serial
in the sketch). You don't see the place where the Serial
object is created because it is part of the built-in libraries that you get as part of the standard Arduino libraries.
Next, you can see that on the first line in the setup()
method, we initialize the hardware data speed to 9600bps. Keep in mind that this is the serial port that will be transmitting bytes to and from the Arduino Serial Monitor.
After we initialize the hardware Serial
, we use that object to print a line to it using the println()
method. That just lets us know that things are progressing because when we open Serial Monitor in the Arduino IDE, we will see that line printed out.
Initializing SoftwareSerial Object
We actually initialize the SoftwareSerial
object to a different data speed (38400). Keep in mind that the SoftwareSerial
object is going to be talking to the HC-05 so we need to match the HC-05 requirements.
Very Important: HC-05 Command Mode Data Rate
The HC-05 defaults to 38400 when it is placed in command mode so we have to match that speed. This is the painful part. If you do not match the speed, then the sketch is going to fail to show you any data in the Serial Monitor. It will just simply fail. It won't do anything and it is very confusing. I suffered this problem for a long time and it's basically impossible to debug -- if you have an oscilloscope, maybe you could determine how data is being transmitted, etc.
According the data sheet, 38400 is the default value, but if you experience difficulties and you are 100% sure you've connected everything properly, you'll need to check the data sheet for your specific HC-05.
HC-05 Data Sheet With Commands
You can get the data sheet that I'm using at this link^.
That's from a particular instructable.com article that I had worked through at one point that is quite informative but misses some pieces for what I needed.
At this point in the sketch, the two serial devices we are using are both initialized and ready to do some work.
The loop()
code may look a bit confusing because it reads from on device and writes to the other and vice versa.
Read from BT_Serial, Write to Serial
Let's take a close look at the first if
block in the code.
if (BT_Serial.available()) {
Serial.write(BT_Serial.read());
}
The if
statement says, "if there are bytes available on the SoftwareSerial
object, then:
read those bytes (BT_Serial.read()
) off the SoftwareSerial
and write them (Serial.write()
) to the hardware serial device. This will occur when we use the Serial Monitor to type commands and send them (Serial.write()
) them to the BT_Serial
device. That's the second part of the loop()
code:
if (Serial.available()) {
BT_Serial.write(Serial.read());
}
How Serial Monitor Communicates with HC-05
When you type a command into Serial Monitor and hit <ENTER>, the command will be written to the Serial port and this second part of the loop will see that there are Serial bytes available. When it sees those bytes, it will write them to the BT_Serial
device (our HC-05) (send the bytes to the component). That will, in turn, cause the HC-05 to respond with bytes that are written to the Serial Monitor which it will display on the screen.
Now that we know how the code works, let's upload it and then put the HC-05 in command mode and try it out.
Get the Code
Download the Arduino sketch source (BT_Config_v001.zip) from the top of this article and unzip it and upload it to your Arduino.
Putting the HC-05 into Command Mode
However, be aware that when you power up your HC-05, it defaults to normal (message sending) mode. For this code to work, the HC-05 has to be forced into Command mode.
Partial Command Mode
These HC-05 modules come with a small momentary switch which allows you to put them into command mode. However, I've just learned that this is really only partial command mode: some commands will not work.
To put the HC-05 into (partial) command mode, you can simply hold the small button while applying power. This can be a bit of a trick with only two hands.
What Happens When You Hold the Button?
When you hold that button and apply power to the circuit, then pin 34 on the actual Bluetooth chip (EGBT-045MS) -- which is soldered on the HC-05 -- is held high (voltage is applied). The HC-05 pin that is marked EN is the one that is connected to pin 34 of the EGBT-045MS.
When pin 34 is held high, it places the chip into command mode. However, since this button only applies the voltage while the button is pressed, the chip is only in partial command mode. For the device to go into full command mode, the voltage must be applied continuously.
Partial Command Mode Drove Me Crazy!
Partial command mode actually drove me crazy for...oh...I'd say about a year. I kept wondering why the Bluetooth device was in command mode but not accepting certain commands like, AT+NAME?, which should return the name of the Bluetooth device but instead doesn't respond at all.
The entire issue is because they've included that simple little button on the side. In the past, you had to wire up the EN pin directly to voltage because there was no button so really there was no partial mode. Why would they add that button and create that kind of confusion? I'm not sure.
Full Command Mode
It's extremely easy to put the HC-05 into full command mode. All we have to do is connect a wire from the Arduino 3.3V socket to the EN pin on the HC-05.
How to Determine if HC-05 is in Command Mode
When the HC-05 is in default message sending / receiving mode, the LED on it will blink (very fast) 2 times every second. When it is in command mode, that LED will only blink (very slow) 1 time every 2 seconds.
If you've wired up the circuit properly with the Arduino 3.3V pin connected to the HC-05 EN pin, then you should be good to go and with no worries about partial mode.
Now, let's send some commands to this thing.
Sending Commands and Seeing Output
Once you've built the circuit and uploaded the sketch, just go ahead and open the Serial Monitor in the Arduino IDE.
When the Serial Monitor window opens, the Arduino will write an initial message (as shown above) so you'll know it is running properly. NOTE: Make sure your baud rate is at 9600 and all other settings match the ones shown in the images.
Now, I'll provide a list of commands and show you the session in an image that shows the values I got when I ran it.
Run Some AT Commands
Determine if it is responding => type: AT response: OK
Get the name of the device => type: AT+NAME? response: +NAME:superblue (yours may be different)
Get the password pin of device => type: AT+PSWD? response: +PSWD:1234
Set the name of the device => type: AT+NAME=<name you want> response: OK (Here I typed "icecoldbt")
Get the name of the device => type: AT+NAME? response: +NAME:icecoldbt
Get the bit speed when the device is in normal message sending mode => type: AT+UART? response: +UART:38400,0,0
That line also includes the number of stop bits (currently set to 0) and whether or not parity check is on (currently off)
Set the password => type: AT+PSWD=3321 response: OK
Check password again => type: AT+PSWD? response: +PSWD:3321
That's all there is to it. Once you know how this works, it is very simple.
Don't Forget to Remove 3.3V to EN Pin
Once you are done configuring your HC-05, simply remove the 3.3V to EN pin wire and disconnect and reconnect the power to the circuit and the HC-05 will start up in normal message sending mode again.
All the Other Commands
There are many other commands you can send, but I'll let you investigate those yourself. Just check out the previous links to the PDF that I provided and you'll have all the commands.
History
- 2018-08-04: First publication