Skip to main content
I read over the original poster's code and noticed the differences and updated the answer to have the code that should be used to solve the problem
Source Link
raddevus
  • 442
  • 3
  • 20

###### Try This : Your Code Updated With Possible Fixes ######

After examining your code and comparing it to my working code I believe I have found a definitive answer. I'm altering your code so you can copy it and try it. I'll add comments where I've changed it.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(15, 14); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Enter AT commands:"); // a prompt in Serial Monitor
  // Deleted while loop, unnecessary
  
  // set the data rate for the SoftwareSerial port
  // notice that your comment on previous line says SoftwareSerial port
  // but you are setting the Serial3 port -- this is where we use mySerial
  // Serial3.begin(38400); // commented your line of code
  mySerial.begin(38400);
}

void loop() { // run over and over
  //if (Serial3.available()) { // we don't use Serial3 any more
  if (mySerial.available()) {
    //Serial.write(Serial3.read());
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    //Serial3.write(Serial.read());
    mySerial.write(Serial.read());
  }
}

I am confident that if you use this newly modified code it will solve your problem. I should've noticed this earlier.

Here's the rest of my original answer.

I have struggled with this myself. As a matter of fact it took me a few months of owning some HC-05s before I finally dug up an answer. It's been a while since I worked with it so I'll do my best to explain what to do.

I have struggled with this myself. As a matter of fact it took me a few months of owning some HC-05s before I finally dug up an answer. It's been a while since I worked with it so I'll do my best to explain what to do.

###### Try This : Your Code Updated With Possible Fixes ######

After examining your code and comparing it to my working code I believe I have found a definitive answer. I'm altering your code so you can copy it and try it. I'll add comments where I've changed it.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(15, 14); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Enter AT commands:"); // a prompt in Serial Monitor
  // Deleted while loop, unnecessary
  
  // set the data rate for the SoftwareSerial port
  // notice that your comment on previous line says SoftwareSerial port
  // but you are setting the Serial3 port -- this is where we use mySerial
  // Serial3.begin(38400); // commented your line of code
  mySerial.begin(38400);
}

void loop() { // run over and over
  //if (Serial3.available()) { // we don't use Serial3 any more
  if (mySerial.available()) {
    //Serial.write(Serial3.read());
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    //Serial3.write(Serial.read());
    mySerial.write(Serial.read());
  }
}

I am confident that if you use this newly modified code it will solve your problem. I should've noticed this earlier.

Here's the rest of my original answer.

I have struggled with this myself. As a matter of fact it took me a few months of owning some HC-05s before I finally dug up an answer. It's been a while since I worked with it so I'll do my best to explain what to do.

Source Link
raddevus
  • 442
  • 3
  • 20

I have struggled with this myself. As a matter of fact it took me a few months of owning some HC-05s before I finally dug up an answer. It's been a while since I worked with it so I'll do my best to explain what to do.

Arduino Nano

Also, I use an Arduino Nano to do the programming of the HC-05.

This Setup Lets You Alter HC-05

Once you wire it up and run the program (included here) you will be able to send any of the HC-05 commands and they work great. You can change the name of the device, the 4-digit pass-code and everything.

Crude Snapshot, But Enough To Build Simple Circuit

I also have a crude snapshot* of the circuit I used (at the time just for documenting it for my own memory) but I think we can work you thru it. and I know those wires are all crammed together and it almost looks like they are in the same breadboardholes but they are actually hooked up properly. I was so excited it finally worked I snapped the pic too fast.

*see image below

Here's the code I use:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX

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(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
}

void loop()
{

  // Keep reading from HC-05 and send to Arduino Serial Monitor
  
  if (BTSerial.available())
    {
     //Serial.println("bt available...");
    Serial.write(BTSerial.read());
    }

  // Keep reading from Arduino Serial Monitor and send to HC-05
  if (Serial.available())
    BTSerial.write(Serial.read());
}

Does Your HC-05 Have the Button?

Okay, so the first thing is to check to see if your HC-05 has the little button on it? If it does that has to be held when you apply power to the circuit. The HC-05 will blink and then it will be in COMMAND mode and you can let go.

If No Button

You can see that mine has the button. That is why the first two lines the setup() method are commented out. I don't need D9 to go high (it is not connected to the pin) because mine has the button. If yours has the button (most likely does) then you don't even have to worry about it.

Looking Back At Your Question

Now that I look back at your sample code it could be that you're not aware that you have to hold that button when you power up. Or you don't know that you need to connect that wire to keep the HC-05 in command mode.

Pins For TX / RX

That's probably the problem you are having, because everything else looks the same -- except you are using 15 and 14 for TX / RX. I am using D10 and D11 for mine and I remember something about those pins in the back of my mind so if yours still doesn't work, try switching to those and try using my code.

I remember the first time I finally got this working and I was dancing around the room. Good luck and I hope you are dancing soon. :)

hc-05 via nano