[Solved] HC-05 problem with HardwareSerial 1, 2, 3

Solved, see post #7

Hello :stuck_out_tongue:

So I've had this HC-05 Bluetooth since like a year, but never done something with it because I used HC-07 modules for my projects and didn't need the features of the HC-05.

Today I wanted to try it's capabilities, so the first thing I did is simply connect it to my Arduino Mega 2560's Serial1 port (with a 5V to 3.3V voltage divider from the Arduino's TX to the HC-05's RX). I didn't change any of it's settings, so it's default to Slave mode, 38400 bauds.

Theorically, it should work just like the HC-07, with which I didn't have any problems..

Practically, it doesn't work right. I can send data from it with Serial1.print (for example), this worked, but when trying to send data to it, it doesn't matter what I send: Serial1.available() will most of the time equal to 1, and sometimes 2, and Serial1.read() will most of the time read a 0, and sometimes a 0 followed by a random character...

This problem is happening with HardwareSerial ports Serial1, Serial2, Serial3. If I use HardwareSerial port Serial or SoftwareSerial, then it works as planned.

This is not a problem in my code, because if I just replace the HC-05 by a HC-07, it works. Also, I am able to enter AT mode and change settings without any problem. I also tried powering the Arduino from an external PSU, no changes.

To recap, the problem is only when trying to receive with additional HardwareSerial ports of my Arduino Mega 2560.

Not working:

void setup()
{
  Serial1.begin( 38400 );
}

void loop()
{
  if ( Serial1.available() > 0 )
  {
    Serial1.println( (char)Serial1.read() );
  }
}

Working:

void setup()
{
  Serial.begin( 38400 );
}

void loop()
{
  if ( Serial.available() > 0 )
  {
    Serial.println( (char)Serial.read() );
  }
}

Any idea what could be wrong ? :frowning:

Still not solved, I have no idea what is wrong. It's so strange :confused: I tried to change baud rate to 9600, 57600...

Someone with an HC-05 and an Arduino Mega 2560 to try and tell me if they have the same problem ?

My HC-05s work on Mega with no problems. The default baud rate for HC-05 is 9600. Using 38400 without changing the settings suggests it is on a shield of some sort and you need configure it to work on the other ports.

I think your your code is suss.

Try this

void setup()
{
    Serial2.begin(9600);// use your baud rate
    Serial2.println("OK then, you first, say something.....");
    Serial2.println("Go on, type something in the space above and hit Send,");
    Serial2.println("or just hit the Enter key");
}
 
void loop()
{
  while(Serial2.available()==0)
  {}

  Serial2.println("");
  Serial2.println("I heard you say:");
  while(Serial2.available()>0)
  {
    Serial2.write(Serial2.read());// note it is Serial.WRITE
  }
  Serial2.println("");
}

After entering command AT+ORGL (restore default status), the default baud rate is 38400. I just didn't say that I typed this command ( in the hope that it could have helped, but no luck) before posting this thread.

Anyway, as I said I tried different baud rates with no luck. I also tried to remove the voltage divider on Arduino's TX, no changes, as expected because the problem is on RX...

Your code is doing the same thing as mine, and I tried it, it doesn't work any better :S

I tested on 3 different Mega 2560, of 2 different brands.. :confused:

Termite's output of your code on port 1 2 or 3:

OK then, you first, say something.....
Go on, type something in the space above and hit Send,
or just hit the Enter key
hello

I heard you say:
[00]

I heard you say:
ÿ

Now on port 0:

OK then, you first, say something.....
Go on, type something in the space above and hit Send,
or just hit the Enter key
hello

I heard you say:
h

I heard you say:
e

I heard you say:
l

I heard you say:
lo

Why does it work on Serial port 0, but not on the other ports? What could be the possible reasons?

I won't quibble about the baud rate, the fact that you have tried several should suffice to show that that isn't the problem.

The voltage divider should not cause this. I have never used it but I recognise it is a good idea.

I can only conclude that the problem has something to do with the connection/configuration. I just use a plain vanilla JY-MCU board, and it's pretty hard to make a mistake with that. I routinely use Serial0 but it tested fine on the others.

Mine too is mounted on JY-MCU board, v1.06. I'm puzzled :frowning:

guix:
Mine too is mounted on JY-MCU board,

In that event, the default baud rate is more likely 9600, and is in 38400 only when "way 1" AT mode is used. I imagine the AT+ORGL fixes that and I assume you get the various confirmations.

What the Hell. I found a workaround:

void setup()
{
  digitalWrite( 19, HIGH ); // fix Serial1
  digitalWrite( 17, HIGH ); // fix Serial2
  digitalWrite( 15, HIGH ); // fix Serial3
  ...

Just adding these lines in my code, or yours, fixed it. If I remove them, it stops working.

I tried this because I was desperate. I can't understand how that fixed it, but it did :confused:. If someone have an explanation as to why I need to do that, I'm all ears! Or maybe I just found a sort of bug in the HardwareSerial library ?

Edit: another solution was to add a pull-up resistor to HC-05's TX. I used a 2.2 KOhms. Now I'm wondering why I didn't try this before :P. Still, a weird issue.

Edit: So after thinking a minute, why add an external pullup when the arduino already have one? Indeed, this also fixed the problem:

void setup()
{
  pinMode( 15, INPUT_PULLUP );
  pinMode( 17, INPUT_PULLUP );
  pinMode( 19, INPUT_PULLUP );
  ...

If the Arduino's pin 0 (RX0) is defaulted to this mode, that would explain why it worked on this serial port and not the others. Else, I'm still puzzled a bit :slight_smile:

In addition to that, I finally found the cause of the problem, and someone already made a webpage about it: Getting Bluetooth Working with JY-MCU BT_BOARD V1.06 | MCU on Eclipse

Nick, I bet that your board is NOT v1.06, right ?

Correct. All HC-05 are on 1.02, but I have never seen an instance where the board version has been called into question!

guix:
What the Hell. I found a workaround:

Edit: So after thinking a minute, why add an external pullup when the arduino already have one? Indeed, this also fixed the problem:

void setup()

{
 pinMode( 15, INPUT_PULLUP );
 pinMode( 17, INPUT_PULLUP );
 pinMode( 19, INPUT_PULLUP );
 ...

Fantastic! This fixed the problems I was having on Serial3 with the HC-05. All I needed was

  pinMode( 15, INPUT_PULLUP );

in my setup()

+1

Thanks!

Welcome,

Glad that it worked for you too :wink:

So I am a little stump right now. I have copied the code (from Post #2) and also place the Pullup code at the beginning and I still can't seem to get HC-05 talking back.

RX pin has a 4050 hex buffer.

Am I setting up the terminal window on? How to monitor Serial1?

The code in reply #2 is only relevant to using ports 1,2,3. If you need to use the serial monitor, you still need to have it set up on serial0 in the usual manner. No comment on the 4050 other than getting rid of it until you have good reason to use it might be a good idea.

How to monitor Serial1?

by echoing the data on serial0, just like you would for anything else.

If you use a DccDuino Mega, there is a typo, RX1 and TX1 are swapped

thanks!!
with...pinMode( 15, INPUT_PULLUP );
my serila3 on mega2560 working good!!!!