[SOLVED] HC-05, no response to "AT" command

Hello, Arduino Community!

This is my first post. I am trying to communicate with an HC-05 ZS-040 module with AT commands. The consistent problem is that no response comes back from the HC-05 module after sending the "AT\r\n" command. And, as far as I can tell (after adding blinking to the BT.available condition as described below), no data on the serial line from the HC-05 becomes available.

My setup includes:

  • Arduino Uno by Elegoo
  • Screw Shield 1.0
  • Two (2) 270 ohm resistors in parallel from 5V to LED annode
  • White 3.2-3.4V LED in series with resistors and State pin of HC-05
  • 2k ohm resistor from Rx of HC-05 to GND
  • 1k ohm resistor from Rx of HC-05 to D10 (alternatively, D11) of Uno
  • 3.3V of Uno to EN pin of HC-05 (alternatively 5v or D9 of Uno).

My code, although it has been changed many times during the testing I will describe, is as follows:

#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11); // RX | TX

char c;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);          // Enable on-board LED
  Serial.begin(9600);                    // baud rate for com with PC
  Serial.println("Enter AT commands:");  // indicates Serial is begun on PC
  BT.begin(38400);                       // baud rate for com with HC-05
  while (BT.available()) {               // end of fast blinking confirms
    digitalWrite(LED_BUILTIN, HIGH);     // HC-05 serial connection 
    delay(150);                          // is begun
    digitalWrite(LED_BUILTIN, LOW);
    delay(150);
  }
}

void loop()
{
  delay(1000);                       // Last attempt, delay until
  BT.write("AT\r\n");                // "AT" command is sent over software
  delay(200);                        // serial port. After delay,
  while (BT.available() > 0) {       // do nothing other than report chars
    c = BT.read();                   // from software serial port as long as
    Serial.write(c);                 // anything is available
  }
  while (Serial.available() > 0) {   // Left over from previous code
    c = Serial.read();               // to send ASCII from PC serial monitor
    Serial.write(c);                 // to HC-05 in case we get that far.
    BT.write(c);                     // [Spoiler Alert] We haven't.
  }
}
  • I started with a blank sketch and 5V to the EN pin as directed in some tutorials
  • I sent "AT" on the Arduino IDE Serial Monitor with Newline and Carriage Return as the method
  • I, alternatively, used the Arduino Create web-based Serial Monitor in the same way. This was done, in both cases, with the Rx and Tx pins both matching and reversed.
  • In all cases, both 9600 and 38400 baud rates were tried.
  • In all cases, pressing the button on the HC-05 (presumably connected to pin 34) was done during boot up of the Uno, continuously during boot and during attempt to send "AT" over serial and not at all.
  • Once using code to include the SoftwareSerial library, all iterations above were attempted multiple times.

Alterations to the code I've attempted, so far, include (and are not limited to)

  • Adding LED blinking of different rates to BT.available and Serial.available if statements
  • Echoing or not echoing BT to Serial and vice versa
  • Waiting in setup to confirm BT.isListening with a while statement and LED blinks
  • Changes in baud rate in BT.begin from 9600 to 38400 and back
  • Changing write commands to print and back
  • Writing read chars directly rather than first storing them as the char variable c
  • Using both "if" and "while" have been used for the *.available() conditions
  • Most recently, using a single BT.write command in code to send "AT\r\n" in case, as was suggested in one forum, sending the string one char at a time was too slow for the HC-05.

I have tried many other iterations of the code and wiring, as well.

In every instance, consistently, the red LED on the HC-05 (there is no other LED) blinks in a pattern of on for one second and off for one second any time the module is powered up while (or after) either 3.3v or 5v is applied to EN pin. Also, the same blink pattern occurs when holding down the button on the HC-05 during power up of the module or while continuing to hold it after power up. In all cases, this behavior is consisted when my phone lists this device as paired and when this device is deleted from by phone's Bluetooth device list.

I have successfully used a slight modification of the above code to echo serial data sent from my PC over the Arduino IDE and Arduino Create Serial Monitors to my phone using a Bluetooth serial terminal app. Both can send, both can read, and this occurs when both baud rates are set to 9600. Other baud rates cause the misreading of data one would expect.

I feel I've tried everything in every forum I've found with Google, so far. At this point in my post, I expect someone is thinking I fried my HC-05 module in some strange way. Buckle in, because all the tests I've listed above have been carried out on three HC-05 modules with the same markings. Two were ordered recently and another was ordered about a month ago from the same seller on Amazon. All three, by the way, still operate normally in another project (related to this one) as a slave device sending and receiving ASCII data to and from my phone (at 9600 baud rate over SoftwareSerial on pins D10 and D11).

All that said, I really hope there is some super simple solution I'm overlooking. I know this is an extremely common and simple module and there's no reason my module would be waiting for some command other than "AT\r\n" before it tells me "OK." I'll try anything suggested and provide code and pictures if asked.

To everyone who responds, thank you in advance for even reading this gigantic post!

Hey you are not alone i just posted about this same issue.

https://forum.arduino.cc/index.php?topic=676323.0

Try this code. And on serial monitor change the drop down to "Both NL & CR". I finally got a response.

Also
nano Rx -> HC05 Tx
nano Tx -> HC05 Rx drop 5v to about 3v - 3.3v to protect HC05. 1k and 2k work for me.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{

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

  Serial.println("Goodnight moon!");

  mySerial.begin(38400);

}

void loop()
{
  if (mySerial.available())
  {
    Serial.write(mySerial.read());
  }
  if (Serial.available())
  {
    mySerial.write(Serial.read());
  }
}
1 Like

Try this tutorial to make your BT working under the setup of UNO + HC05 + Phone:
1. Build the following circuit.
hc5Unox.png

2. Upload the following sketch.

#include <SoftwareSerial.h>
SoftwareSerial SUART(10, 11); // SRX, STX

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);  //L is OFF
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char x = SUART.read();
    if ( x == '1')
    {
      digitalWrite(13, HIGH);  //L  is ON
    }
    else
    {
      if (x == '0')
      {
        digitalWrite(13, LOW);  //L is OFF
      }
    }
  }
}

3. Launch BT terminal in your phone.

4. Pair your phone with BT.

5. Set the command mode of your phone's BT terminal to ASCII.

6. Send 1 from phone; check that L (built-in LED of UNO) has become ON.

7. Send 0 from phone; check that L of UNO s OFF.

hc5Unox.png

First, thank you both for responding as I did not expect anyone to notice my post and respond so quickly. :slight_smile:

GolamMostafa, thank you for you help :slight_smile: However, as stated in my (painfully long) original post, all three modules operated normally as a slave device in other projects. Your code did work as intended, but didn't really involve getting a response in AT mode.

To gen_tj, thank you as well. Although the code you sent is essentially the same as code (in structure) I tested originally (and I did, just to try "everything," change the Serial baud rate to 38400), I never thought to try the 19200 baud rate over the hardware Serial port. On first attempt, I got back the response of "Error (0)" which got me very excited! I had not had a response of any kind until then. I would have filed that under "mild success" if that's all I got tonight.

Happy with the "Error (0)" response to AT, I sent it a few more times. Just for thoroughness, I sent random (keyboard mashing) characters and got the Error (0) a couple more times. After this, I tried "AT" again and, voila, "OK" came back!

Since I must know what exactly did or didn't make a difference this time, just as I almost decided the 19200 hardware baud rate was a magic solution, I tried changing the baud rate in your code back to 9600. This also works (with the same "Error (0)" message on the first attempt in most trials) as well as my original code, now. Perhaps your code breathed life into my project or I've been way too tired during all my previous attempts. The "Error (0)" response, by the way, seems to not happen with a 100ms delay in setup after the mySerial.begin() command. I intend to filter out all non-alpha-numeric characters as I suspect this is due to some gibberish getting onto the SoftwareSerial port during setup.

For reference, the wiring in this working setup is similar to GolumMostafa's diagram with the 3.3v supply from the Uno connected to the HC-05 EN pin and a 3.4v white LED connected to the HC-05 State pin and 5v supply of the Uno, as described in my original post, with the equivalent of about 135 ohm current limiting resistor. This LED stays on in AT mode as no connection over Bluetooth is possible.

2 Likes