Leonardo Serial1 problems with HC-05 bluetooth

Hi guys, I am clearly doing something wrong here, but I can’t for the life of me figure it out. I’m just not getting anything on Serial1

Here’s how I have the pins set up on the module

  • Key 5v output on Leonardo through a voltage divider to bring it down to 3.3v (tested at around 3.1 on multimeter)
  • Tx Rx pin on Leonardo
  • Rx Through another voltage divider from the Tx pin on Leonardo (tested at around 3v on multimeter)
  • 5v empty
  • 3.3v To 3.3V power out on Leonardo
  • grnd to ground on Leonardo

I can leave the Key input with nothing going in and am able to pair the device with my PC and see it as a com port. When I apply the 3.3V to the Key, the led indicator on the HC-05 flashes slowly and evenly, unlike the rapid flashes of the device when no power is applied.

Here is the very basic code I am using:

int BRate;

void setup(){
  BRate = 38400;
  Serial.begin(BRate);
  Serial1.begin(BRate);
  delay(1000);
  
}

void loop(){
  Serial1.print("AT+VERSION?");
  //Serial1.println("AT");
  delay(200);
  while(Serial1.available()>0)Serial.print(Serial1.read());
  Serial.println(" ");
  Serial.println("processed");
  delay(1000);
}

I don’t get anything from Serial1 at all. The Serial monitor just shows a blank line followed by “processed” as would be expected from no Serial1 activity. Can anyone point me in the direction of what I am doing wrong? I’m finding it difficult to find any examples online that use a module with the same pin names that mine uses.

PS-Sorry if this post is for some reason large letters. I have no idea what caused that. It looks really odd in the preview.

Try:

Serial1.print("AT+VERSION?\r\n");
or
Serial1.println("AT+VERSION?");

Thanks, pito. I tried both of those initially with no luck, but read that this module doesn't need it, so I tried it without too. I just tried it again to double check, and no dice.

I've used a terminal program to connect to it via bluetooth in slave mode, and it sends and receives data fine, so it's connected right and everything appears to work. The test sketch I used has most of the same syntax as in the one above, so I'm at a loss. The Serial1.read() command just continuously returns -1 when using the sketch pasted above, which is to be expected if there is no data.

I've also tried looping through various baud rates to see if that could be the issue, but had no luck.

Isn't the default baud speed for the HC-05, 9600 ?

Anyway, I had the same problem with the HC-07, I couldn't configure it when plugged on the Arduino (Mega in my case). Hopefully when I bought the HC-07 I spent few more money for one of those:

http://www.ebay.com/itm/USB-To-RS232-TTL-PL2303HX-Auto-Converter-Module-Converter-Adapter-For-arduino-/321128857188?pt=LH_DefaultDomain_0&hash=item4ac4c57e64

And I was able to configure the HC-07 without any problems... the only thing to know, is that you must send the command very fast (by copy/pasting)

Maybe someone can find a solution to configure it on the Arduino :/

Isn't the default baud speed for the HC-05, 9600 ?

He is trying to communicate with it in "AT" mode - that is usually 38k. If it works with XP's Terminal, it should have worked with arduino as well..

So it works somehow (atmega1284p):
With this:

void setup(){
  //BRate = 38400;
  Serial.begin(115200); //do not use variable here
  Serial1.begin(38400); //do not use variable here
  delay(1000);
  
}

void loop(){
  Serial.println("AT+VERSION?");
  Serial1.println("AT+VERSION?\r");  // mind println and "\r" here
...

the module returns (captured with a third serial dongle at HC’s tx pin) following response:

+VERSION:1.0-20090818
OK

But it does return only ONCE after reset, and it does not send the above response via Serial, so the issue is somewhere in your code:

 while(Serial1.available()>0)Serial.print(Serial1.read());

here the C experts may help us how to receive data in such setup as above… :~
PS: you need here something like serial.read_line()…

Excellent! I am getting a result with this! Thank you.

As you said, it only returns a result once, but that's one more than I had before. A baby step is better than no step at all. :)

I tried issuing "AT/r" followed by "AT+VERSION?/r" thinking maybe it was refusing to respond to duplicate commands, but only the AT portion replied. The response I got was "79751310" though.

PS-Thanks. I was just trying to check the first bit to see if anything was there. In the actual code, I have the while loop.

Ok, I got somewhere.

Serial1.write("AT+VERSION?\r\n");

That seems to be working better. It returns a result multiple times without needing to restart anything. I am still getting gibberish results though.

ok got it.....it was the Serial.print(). I just need to use write on both serial streams.

apparently print does an ascii conversion, and write just sends the data. That seems to have fixed it.

Thanks so much for your help!

This works fine with me (IDE 1.5.2 and atmega1284p mighty, HC-05_1 on Serial, and HC-05_2 in AT mode on Serial1):

String readString;     // this string contains the response from the BT module

void setup(){
  Serial.begin(115200);     // Serial to PC
  Serial1.begin(38400);     // Serial to HC-05 BlueTooth module in "AT" mode always @38k4
  delay(1000);
}

void loop(){
  Serial.println("AT+VERSION?");
  Serial1.print("AT+VERSION?\r\n");      // Send an AT command and \r\n to the BT - must be in this sequence
  delay(200);

  readString = "RETURNED: ";

  while (Serial1.available()) {                 // read the response from the BT module

    delay(3);  //delay to allow buffer to fill 
    if (Serial1.available() >0) {
      char c = Serial1.read();     // gets one byte from serial buffer
      readString += c;     // makes the string readString
    } 
  }

  Serial.println(readString);      // print the from BT returned response to PC
  readString = "";     // clean the return string
  
  delay(3000);
}

And the output on the IDE serial window:

AT+VERSION?
RETURNED: +VERSION:1.0-20090818
OK

AT+VERSION?
RETURNED: +VERSION:1.0-20090818
OK

AT+VERSION?
RETURNED: +VERSION:1.0-20090818
OK

weird…print seems to work now too on Serial1 using that code. I still have to use write() to get the data to display properly on Serial though.

But hey. That’s enough. Now I can finish coding the controller portion to program the HC-05 so I can switch modes on it and configure settings without having to pull it from the Leonardo.

You’ve been a great help. Thank you.

In order to switch between the AT mode and the serial mode on fly you must be able to power on/off the module (and set the key to log1 as well) somehow from the code..

I was thinking I may run the 3.3v line going to the HC through a transistor so I can activate and deactivate it. That should work shouldn't it?

Yes, use an high-side switch - ie. a p-channel mosfet (or pnp transistor). The max current HC-05 draws is 40mA (when pairing).