HC-05 AT commands

I have hooked up an HC-05 module to an Arduino Nano and am getting AT commands to go through when I type them in the serial output command console. Is there any way I could write code so that I don't have to type a command into the console? I have tried the only thing I know, that being Serial.print("AT"), but it simply says "AT" over and over.

There are a number of HC-05 tutorials on line that show you how to put the AT commands in code. The search phrase "arduino hc-05" will find them.

1 Like

You just put the AT commands in setup as a one-shot programmer.

/
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3); // RX | TX
String command = ""; // Stores response from HC-06

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //monitor
Serial1.begin(9600); //bluetooth27
Serial.print("AT ");
Serial1.print("AT"); //PING
if (Serial1.available()) {
while(Serial1.available()) { // While there is more to be read, keep reading.
delay(3);
char c = Serial1.read();
command += c;
}
}
delay(1000);
Serial.println(command);
command = ""; // No repeats

etc. No code in loop

The method in this tutorial that I wrote works on all of the HC05 modules that I have used it on. >>> Setting HC05 (ZS-040) to AT mode

Are you sure that the module that you have is HC05? Please post photos of the mofule(s), front and back.


The module is a clone but It behaves the way an HC-05 should. I can communicate between two of them as master and slaves. In the long run I want to be able to have one master module switch between multiple slave modules by changing the addresses bound to it, but I don't know if its possible to program an AT command to be entered automatically, rather than manually.

Here's the code I'm currently using:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {

  Serial.begin(38400);
  while (!Serial) {
    ;
  }

  mySerial.begin(38400);


}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }

}

They sure look like HC05. I ask because there have been instances, recently, where we went through many posts trying to get them to work only to find that they are something else.

HC-06 looks really similar to HC-05 but I believe HC-06 are slave-only modules. I can connect two HC-05 modules together using two Arduino Nanos, but my biggest question now is whether there is a way to enter Serial monitor commands through code. Usually you'd have to type them in manually, but is there a way to enter them digitally? Is the message bar for the serial monitor different than what Serial.print() does?

If I'm able to program AT commands into the serial monitor, I'd imagine I'll be able to simply input
AT+BIND=(new HC-05 address) so that the slave bound to the master changes to a new module.

Do you try to submit the AT-commands with a linebreak or by waiting a second?
The (clone) devices I got, are using both approaches randomly.

Serial 38400 won´t work right from the start. Those devices have 9600 initially.
But if you already got a communication between serial and bluetooth (terminal) you are fine with that.
But: Having a device connected, will deactivate the AT mode. This only works when nothing is connected via BT or you set the AT-Mode by using the pin on the HC-05 board.

You can do that but you have to have the master Arduino in control of the Master HC-05 power supply and 3.3v to the EN pin thereby enabling you to shut down and start again in AT mode for reconfiguration. You can then individually poll multiple slaves.

The clone devices I use only work on 38400 baud rate. I have already been able to get 2 of them into pairing mode (rapid LED flashing) where they then paired together. My question is, usually when setting up two HC-05 modules, you have to grab the address of the slave device with the AT command, AT+ADDR?, and then bind that address to the master module using AT+BIND=(slave address). Instead of typing these AT commands manually, is there any way to program the AT commands to be entered automatically using code?

Yes, but what would the process of reconfiguration entail? Even if the master module is put back into AT command mode, the master module would simply reconnect to the same slave module since the master module would have the same bind address.

Each time you put it into AT mode, you configure it to bind with a different slave address.

I've just had an idea for a solution that I'll try. Perhaps if I use the AT command, AT+CMODE=1, which will allow any address to be accepted, and then just modulate which slave module is in pairing mode, I can control which device the master is connected to. I forgot CMODE exists so I had it set to 0, meaning I needed a specific address for specific modules. Silly me

I believe that means it is unspecific, you just take the first cab off the rank. This is fine if you neither know nor care which Bluetooth is connected - a situation which is probably OK if only one slave is available at a time.

If I did care which Bluetooth module was connected and did have multiple slaves available at a time, do you know a way of selecting which slave is connected?

see replies #9, 12

I think I get what you mean in reply # 9 + 12. If I want to reconfigure the master to bind with a new slave, I'd have to re-engage AT mode by cutting and re-supplying power so that the required commands can be entered. What I don't know is how I can even enter the required AT commands via code. All I know is how to manually enter them into the serial monitor command console. Is it even possible to code commands into the serial monitor?

See reply #3

That was for a one-shot config. The same commands can be included in a polling loop. The responses on the monitor are barely relevant.

When I upload your code, I simple get AT on the monitor. Are you saying that this response doesn't mean anything and that the command actually went through?

Yes. That should be from
Serial.println(command);
which is the response from HC-05