Friday, October 25, 2013

Tutorial: Programming the HC-05 - AT mode - with Arduino

Tutorial: Programming the HC-05 AT commands - done easily with Arduino


What This Tutorial Covers

In this tutorial I will show you how to wire the HC-05 to Arduino UNO and get into the AT mode all done with CODE. This way there is no need for pulling pins high and low by hand and it is quick and convenient and easy. Also, I will go through the process of changing the bound rate, the name and demonstrate a few other useful AT commands. 

*Please note that this tutorial is for the HC-05.


What You Need

 For this tutorial you will need a few things, here is the list:

- Arduino UNO, but any equivalent should do just fine
- HC-05 with a breakout board, unless you know what your doing. 
- a breadboard and some wires

Set Up

Here are some pictures of the Set Up. Included is also a diagram of the connections made with Fritzing. (Thanks Fritzing!)






A quick run through the connections verbally as they are very simple and easy.

- Arduino pin A1 is connected to the TX pin of the BT.
- Arduino pin A0 is connected to the RX pin of the BT
- Arduino pin GND is connected to GND pin of the BT.
- *** Arduino pin 7 is connected to 5V pin of the BT. ***
- Arduino pin 8 is connected to the KEY pin of the BT. 

Simple, no? A few wires and we are set to upload the code! 

Now, before I slap the code here I want to take the time to explain why we are connecting the 5V of the Bluetooth to pin 7 of the Arduino. This is very simple, in order to get into the AT mode we have to follow a few simple steps and connecting the 5V pin to a variable pin of the Arduino allows us to bring it High and Low as we please. In order to get into the AT mode we have to first pull the 5V and the KEY pins to Low. This is important and often skipped by all the other tutorials and thus it introduces problems for the people trying to follow in the long run. Once they are Low we have to pull the KEY pin High, after a small delay and only then, we pull the 5V pin to HIGH. This results in the BT going into the AT mode and this can be confirmed by sending a serial command "AT" via serial and/or by looking at the HC-05 indication LED; it should be blinking at one second intervals.

The Code

Now finally for the code. I commented it thoroughly and thus I will not explain much of it here in text. I want people to find it easy to program the HC-05 this I'm posting the code as a whole. 

Simply copy this code and upload it to the Arduino. 

/*
This code is in the public domain.
written by Damian Glinojecki
*/

#include 

#define bt_power 7
#define bt_key_power 8
#define indication_led 13

SoftwareSerial BT(A0, A1); // RX | TX

void setup()
{
  // set the pins to OUTPUT
  pinMode(bt_power, OUTPUT);  
  pinMode(bt_key_power, OUTPUT);
  pinMode(indication_led, OUTPUT);
  
  // set the pins to LOW
  digitalWrite(bt_power, LOW);
  digitalWrite(bt_key_power, LOW);
  digitalWrite(indication_led, LOW);
  
  /************************************************
  Setting the pins to low is important because 
  in order for us to get into AT mode the key pin
  has to be set to Ground FIRST. Many tutorials out
  there fail to mention this important fact and 
  therefore many people have problems with getting 
  into the AT mode of the HC-05
  ************************************************/
  
  // make sure the key has been LOW for a bit
  delay(100);
  
  // set the key pin to High
  digitalWrite(bt_key_power, HIGH);
  
  // small delay
  delay(100);
  
  // now power on the BT
  digitalWrite(bt_power, HIGH);
  
  // start our serial so we can send and recieve
  // information from the BT module
  Serial.begin(9600);
  // initiate the BT serial at 38400 which is the default 
  // speed at which the BT AT mode operates at
  BT.begin(38400);
  
  // self explanatory
  Serial.write("For a list of commands, visit: \n");
  Serial.write("Type AT commands  \n\n");
  
  // process complete turn on led 13
  digitalWrite(indication_led, HIGH);
  
  // Send an "AT" command to the AT (without quotes)
  // if response is OK, then we are connected
  // and ready to program the BT module
 }

void loop()
{

  // listen for a response from the HC-05 and write it to the serial monitor
  if (BT.available())
    Serial.write(BT.read());

  // listen for user input and send it to the HC-05
  if (Serial.available())
    BT.write(Serial.read());
    
}


Now to the Programming of the HC-05

In your Arduino sketch you have a very useful tool called the Serial Monitor. Click Tools -> Serial Monitor.



Before we do anything, lets make sure our Serial Monitor settings are correctly set. This is IMPORTANT! 

First off make sure that the boxes outlined in red match yours. 



***Please note that I am using version 1.0.1 of the Arduino software but it should be the same for all of them. 

It is important that your bound rate is set at 9600 or else we wont be able to talk the same "language" as the Arduino is set to understand. Also, make sure that the line editing is set to "Both NL & CR". This is another important detail that many tutorials miss. (I was ripping out my hair not understanding why I cant communicate with the HC-05)

Now for a test type "AT" without the quotes and press enter. You should get a response "OK" as follows:



This is GOOD! This means that we are in AT mode and we are talking with the HC-05 and it hears us! Now we can change some of the settings. You can find the complete list of commands on the bottom of this page.

For starters I will change the name of the Bluetooth. To do this all I need to do is type in the following command: "AT+NAME=QMBTv1" (again, without the quotes) You can substitute "QMBTv1" for anything you like. This is the name I am giving my BT though. Now to confirm that the name was changes lets send another command "AT+NAME?" The HC-05 should respond with the name that we just assigned.



Side Note: Some of you might wonder what does the QMBTv1 stand for. As I mentioned in an earlier post, I want to build a quadcopter. This BT module is going to be the main means of communication with the quad. :) 

Now lets change the bound rate to 115200,1,0. This is just as simple. Send this command "AT+UART=115200,1,0" and then "AT+UART?" to confirm that it was changed successfully. 


If you followed all the steps and got the name and the bound rate to change, then you have done everything right. Congratulations you changed the HC-05 settings! 

Commands for the HC-05

You can find all of them here. Courtesy of linotux, Thanks for listing them! 




35 comments:

  1. sketch_sep22c.cpp:1:10: error: #include expects "FILENAME" or
    sketch_sep22c.cpp:10:1: error: 'SoftwareSerial' does not name a type
    sketch_sep22c.cpp: In function 'void setup()':
    sketch_sep22c.cpp:50:3: error: 'BT' was not declared in this scope
    sketch_sep22c.cpp: In function 'void loop()':
    sketch_sep22c.cpp:68:7: error: 'BT' was not declared in this scope
    sketch_sep22c.cpp:73:5: error: 'BT' was not declared in this scope

    ReplyDelete
    Replies
    1. you need the software serial library for this to run, google around and you will find it without problems

      Delete
    2. As in the example here: http://arduino.cc/en/Reference/SoftwareSerial

      Just add this line at start:
      #include

      Delete
  2. Hi.

    I followed this tutorial and I think my HC-05 is in AT mode, in that it flashes with a 2 second interval.

    I have however not been able to receive data back from the module. Whenever I send "AT" or indeed any other AT command, there is not an "OK" sent back in my serial monitor.

    Any ideas?

    ReplyDelete
    Replies
    1. Hi, make sure your tx and rx pins are not flipped, you can always swap them to make sure. If that's not the problem, make sure that the line ending is set to NL & CR.
      Cheers! Damian

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Same for me, here I had to do this:

      - Arduino pin A1 is connected to the RX pin of the BT.
      - Arduino pin A0 is connected to the TX pin of the BT

      PS: I'm using this module:
      http://www.banggood.com/HC-05-Wireless-Bluetooth-Serial-Transceiver-Module-Slave-And-Master-p-908621.html?p=PL2609737699201408VA

      Delete
    4. Hi Damian.

      Thanks for your speedy response!
      I tried your suggestions, but it still doesn't work. Can you suggest anything else? Could it be my module?

      Delete
    5. This comment has been removed by the author.

      Delete
    6. Here, is a test of a master HC-05 connecting in two slaves HC-05...
      https://www.youtube.com/watch?v=idP97bg9AuQ

      Delete
    7. Apart from shifting A1 to RX and A0 to TX, I had to solder a wire to pin 34 of the HC-05 and connected it to pin 8 of the Arduino in order to enter AT mode. Now it works.

      Delete
  3. Interesting.
    The fault seemed to lie in my choice of operating system! It didn't work in Linux, yet it did in windows. Very strange!
    I'm all sorted now. Thank you very much for all your help.

    ReplyDelete
    Replies
    1. Hi! That is indeed very strange. What version of the arduino were you using under Linux? I have found the arduino ide to indeed have a few problems under Linux - such as device detection problems.

      Delete
    2. First of all Thank you very much for the tutorial and for you both for the discussion. I have the same issue as EGeek and i want to know what he did exactly to solve the problem please help

      Delete
  4. You rock! I had to switch the rx and tx connections then all was well. Thanks for making a great and elegant solution.

    ReplyDelete
  5. Im using HC-05 BT "FC-114" (State, Rxd, Txd, Gnd, Vcc), same problem the BT cant response AT-Command.
    I've tried a tutorial on this blog, youtube, etc.. but have not succeeded

    thank tutorial, greetings from Indonesia

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
  6. Wonderful. Instanct success. Despite the one mentoined above from instructables.
    Thank you so much

    ReplyDelete
  7. what are the commands required to communicate between two hc 05 module...????

    ReplyDelete
  8. Am using a HC-05 FC-114 bluetooth module, there is no key pin there is an EN pin. I folloed the same steps but i couldn't get the AT mode what is the problem

    ReplyDelete
  9. hello,
    please let me know the AT command used to send the data or file from HC-05 to mobile...

    ReplyDelete
  10. Hello,
    please let me know the AT command to send data or file from HC-05 to mobile...

    ReplyDelete
  11. Only for HC05: some models of the breakout board do not have the key pin, instead they have an EN (enable) pin or WAKE UP pin which may or may not be connected to any pin just like the STATE pin. In that case you'll have to solder a wire to the 34th pin of the bt module. I soldered a male header to mine but if you can't solder or don't want to, then you can just hold a wire (the pointy end of a male to male jumper wire would be convenient ) while connecting the Vcc to 5volt. Once the bt module is in AT mode, you can release the wire.

    ReplyDelete
  12. Isn't the fritzing diagram incorrect? It shows the RX line (the yellow one) going to A1, yet the text below the diagram says - Arduino pin A0 is connected to the RX pin of the BT.
    My HC-05 never comes up with the slow flashing LED. On alternate resets of the Arduino Uno the LED is either completely off or it is flashing quickly.
    I don't know if this inability to get it into programming mode is the problem but my serial monitor just displays a continuous stream of 'y" characters with two dots above the y. AT commands get no response.
    The indicator led on pin 13 goes high and stays high which I think it is supposed to.
    I don't understand why your code did not complete the #include statement to be #include
    Not having much success here.

    ReplyDelete
  13. Hi, here I share the code for 1 master connecting with 2 slaves: https://www.saibatudomt.com.br/2018/01/conectando-3-dispositivos-arduino-utilizando-o-modulo-bluetooth-hc-05.html

    ReplyDelete
  14. in ZS-040 pin 34 is csolder or not

    ReplyDelete
  15. Hello, i have a problem with AT mode,
    I check so many times my connections and its true i swear..
    My code is same with yours, but i type AT to console it response like "xxxx⸮" all other commands is the same start with AT+... . How can i fix this is any one help me ?

    ReplyDelete
  16. Hello, i have a problem with AT mode,
    I check so many times my connections and its true i swear..

    ReplyDelete
  17. The alternative is to put your bug zapper up large. For this reason, you ought to avoid choosing super-cheap options which make big claims. Discover here for more information about parasite zapper.

    ReplyDelete