Bluetooth , role

Not able to set up hc05 as master, it is giving error 0. I have also reset the module, everything is working fine except that.
Even the binding and cmode all are working.
Any suggestion what I can do??

Are you manually holding down the HC-05 button during the right commands?

It's a pain in the neck and rather tricky without detailed instructions - I've had to pair bluetooth modules like this twice before and I know your pain.

Power_Broker:
Are you manually holding down the HC-05 button during the right commands?

It's a pain in the neck and rather tricky without detailed instructions - I've had to pair bluetooth modules like this twice before and I know your pain.

Yes I have manually hold it for getting it into commanding mode.

Funny you should reply to the thread when you did - I just finished a rough draft of a sketch that automatically pairs, binds, and links an HC-05 and an HC-06 module. Right now it only works for Arduinos that have several hardware serial ports (I'm running it on a Mega rn).

Check out the code and see if it helps. If you have any questions, let me know.

#define MASTER_PORT Serial1
#define SLAVE_PORT  Serial2




String rname = "";
String rnameListStr = "";
String ID = "";




void setup()
{
  pinMode(52, OUTPUT);
  pinMode(52, LOW);
  
  Serial.begin(115200);
  while(!Serial);
  MASTER_PORT.begin(38400);
  SLAVE_PORT.begin(9600);
  
  MASTER_PORT.setTimeout(100);
  SLAVE_PORT.setTimeout(100);




  Serial.println("<<<=====================================================================>>>");
  Serial.println("Put HC-05 into AT mode and send 'OK' through Serial Monitor");
  Serial.println("<<<=====================================================================>>>");
  Serial.println();
  while(Serial.read() != 'O');
  


  
  sendAT_master("AT");
  sendAT_master("AT+ROLE=0");
  sendAT_master("AT+UART=9600,0,0");
  sendAT_master("AT+CMODE=0");
  sendAT_master("AT+PSWD=1234");
  Serial.println();





  sendAT_slave("AT");
  sendAT_slave("AT+NAMEmyBT");
  sendAT_slave("AT+BAUD4");
  sendAT_slave("AT+PIN1234");
  Serial.println();




  sendAT_master("AT+RMAAD");
  sendAT_master("AT+ROLE=1");
  sendAT_master("AT+RESET");
  Serial.println();




  Serial.println("<<<=====================================================================>>>");
  Serial.println("Put HC-05 back into AT mode and send 'OK' through Serial Monitor");
  Serial.println();
  Serial.println("Note for after returning HC-05 into AT mode:");
  Serial.println("\tMAKE SURE THE HC-05's BUTTON IS PRESSED UNTIL TOLD OTHERWISE!!!");
  Serial.println("<<<=====================================================================>>>");
  Serial.println();
  while(Serial.read() != 'O');




  sendAT_master("AT");
  sendAT_master("AT+CMODE=0");
  sendAT_master("AT+INQM=0,5,9");
  sendAT_master("AT+INIT");
  sendAT_master("AT+INQ");
  Serial.println();



  Serial.println("<<<=====================================================================>>>");
  Serial.println("HC-05's button no longer needs to be pressed");
  Serial.println();
  
  byte endIndex = rnameListStr.indexOf(",1F00,");
  byte startIndex = endIndex - 14;
  if(rnameListStr.indexOf(",1F00,") != NULL)
  {
    ID = rnameListStr.substring(startIndex, endIndex);
    Serial.println("Address of HC-06: " + ID);
    ID.replace(':', ',');
  }
  else
  {
    Serial.println("ID not found...");
  }
  Serial.println();

  Serial.println("Put HC-05 back into AT mode and send 'OK' through Serial Monitor");
  Serial.println();
  Serial.println("Note for after returning HC-05 into AT mode:");
  Serial.println("\tMAKE SURE THE HC-05's BUTTON IS PRESSED UNTIL TOLD OTHERWISE!!!");
  Serial.println("<<<=====================================================================>>>");
  Serial.println();
  while(Serial.read() != 'O');




  sendAT_master("AT");
  sendAT_master("AT+INIT");
  sendAT_master("AT+RNAME?" + ID);
  sendAT_master("AT+PAIR=" + ID + ",20");
  sendAT_master("AT+BIND=" + ID);
  sendAT_master("AT+CMODE=1");
  sendAT_master("AT+LINK=" + ID);
  Serial.println();




  Serial.println("<<<=====================================================================>>>");
  Serial.println("HC-05's button no longer needs to be pressed");
  Serial.println("<<<=====================================================================>>>");
  Serial.println();
}




void loop()
{
  //do nothing
}




void sendAT_master(String command)
{
  String response = "";

  unsigned long timeBench;
  unsigned long currentTime = timeBench;
  unsigned int duration = 5000;
  
  Serial.println(command);
  MASTER_PORT.println(command);

  while(!MASTER_PORT.available());

  if(command.equals("AT+INQ"))
  {
    timeBench = millis();
    currentTime = timeBench;
    while((currentTime - timeBench) <= duration)
    {
      currentTime = millis();

      response = response + MASTER_PORT.readString();
    }

    rnameListStr = response;
  }
  else
  {
    response = MASTER_PORT.readString();
  }

  Serial.print(response);
  
  return;
}




void sendAT_slave(String command)
{
  String response = "";
  
  Serial.println(command);
  SLAVE_PORT.print(command);

  while(!SLAVE_PORT.available());

  response = SLAVE_PORT.readString();
  Serial.println(response);
  
  return;
}