arduinoserial-communicationhc-05

Serial Communication is not working properly in Arduino connection with HC-05


I am new to Arduino Programming.

This code works fine. It gives the correct output and functioning well. But I wanted to write the commands automatically without typing repeatedly.

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

void setup()
{
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  Serial.begin(38400);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{
  if (BTSerial.available()) {
    Serial.write(BTSerial.read());
  }
  if(Serial.available()){
    BTSerial.write(Serial.read());
  }
}

So i tried doing the following change in the above code but i could not receive any response.

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN

void setup()
{
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  Serial.begin(38400);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  BTSerial.write("AT");
}

void loop()
{
  if (BTSerial.available()) {
    Serial.write(BTSerial.read());
  }
}

I also tried BTSerial.print("AT"); But still no response.


Solution

  • AT commands are case sensitive and should end up with a terminator like an enter keystroke or a \r\n.

    This should do:

    #include <SoftwareSerial.h>
    
    SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 11 PIN | CONNECT BT TX PIN TO ARDUINO 10 PIN
    
    void setup()
    {
      pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
      digitalWrite(9, HIGH);
      Serial.begin(38400);
      Serial.println("Enter AT commands:");
      BTSerial.begin(38400);  // HC-05 default speed in AT command more
      BTSerial.write("AT\r\n");
    }
    
    void loop()
    {
      if (BTSerial.available()) {
        Serial.write(BTSerial.read());
      }
    }