Bluetooth HC-05 to HC-06: Not receiving correct data

Hello,

I’ve got a bluetooth project where I need to transmit data between two Arduino’s. I’m using one Arduino as a slave (HC-06 module) and another as the master (HC-05 module). I’ve configured the HC-05 module successfully with the AT commands and am able to have it pair with the HC-06 automatically.

The problem I’m having is getting the HC-06 to receive the correct data transmitted from the Arduino with the HC-05 module. I know my code on the HC-06 works because I can remove the HC-05 module from the Arduino and attach it to my TTL to RS-232 module, open up a terminal on my computer and send a number (1, 2 or 3) to command LED13 on the slave arduino to turn on, off and blink.

However, when I attach the HC-05 module back to the Arduino and program it to send a 1, 2 or 3, I am unable to get the slave Arduino to react. I debugged the slave Arduino to see if I am getting any data from the master using the Serial Monitor; however, I am getting incorrect numbers/values.

I have my code listed below for the two different arduino sketches and the output of the Serial Monitor of the slave Arduino.

Slave Arduino Code

#include <SoftwareSerial.h>
int receivedval;
int  LED = 13;
SoftwareSerial mySerial(3,4); // RX, TX
//RX on Bluetooth to TX on uC

void setup()
{
  pinMode(LED, OUTPUT);
  mySerial.begin(38400);
  Serial.begin(9600);
}

void loop() 
{
  while (!mySerial.available());      // stay here so long as COM port is empty
  receivedval = mySerial.read();   //Store received data
  Serial.println(receivedval);        //print received data in Serial Monitor
  if (receivedval == 1) {             //Series of if-statements will turn LED on (1), off(2) or flash(3)
    digitalWrite(LED, HIGH);
  }// if it's a 1 turn LED on
  if (receivedval == 2) {
    digitalWrite(LED, LOW);
  } // if it's a 2 turn LED off
  if (receivedval == 3) {
    for (int i = 0; i < 30; i++) {
      digitalWrite(LED, HIGH);
      delay(20);
      digitalWrite(LED, LOW);
      delay(20);
     }
  } 
 }

Master Arduino Code

#include <SoftwareSerial.h>
SoftwareSerial mySerialMaster(3,4); // RX, TX
//RX on Bluetooth to TX on uC

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

void loop() 
{
    mySerialMaster.println(1);
    delay(1000);
    mySerialMaster.println(2);
    delay(1000);
    mySerialMaster.println(3);
    delay(1000);

}

See attachement for Output from Slave Arduino

Any input/help is deeply appreciated.

Have you checked what Phliippe Cantin is doing?

Yes, I seen his name around the internet several times during my search. However, the only thing I had seen was configuring the AT commands of the modules for 2 BT to communicate with each other. While that is relevant, it doesn't seem to help with actually coding them in the Arduino IDE... unless I somehow missed something.

rsxdriver90:
. unless I somehow missed something.

That might be the case. You don't code the bluetooth devices in Arduino, you simply read/send data over serial. Arduino doesn't care about what is connected to the port.

Right... sorry for the confusion. Yes I do know that the BT modules are coded using the AT(Haye's) commands. You're right, my Arduino code is not supposed to be programming them but instead supposed to send/receive data between the two once they are paired.

My problem lies in the fact that I can't get receive the correct data on the Slave Arduino that was transmitted from the Master Arduino. I'm receiving random data when I monitor the Slave Arduino.

I just need help getting this to work properly.

Regards,
rsxdriver90

Does 38400 work with softwareserial??
Try 9600 instead…

You might also question why you are using software serial at all.

I figured out what was wrong. The Baud rate on my HC-05 module was incorrect. I needed to set the AT command on the HC-05 which would allow me to change the UART baud rate to 9600. 9600 is apparently the default baud rate of the HC-06 module. I didn't think to change this because I was able to transmit data from my HC-05 module when I had it connected to my laptop using a terminal (@ baud rate of 38400) and the RS-232 cable.

Once I changed the UART baud rate I was able to get the two modules sending data back and forth between each other using the Arduino IDE.

Now my 2nd problem is sending integers through serial between the two Arduino’s.

I am breaking integers up into a ‘high’ byte and a ‘low’ byte. I can transmit the data to the receiving Arduino, however when I read the low byte I’m receiving 255 before and after the ‘low’ byte when I try printing it in the Serial monitor. The integer I’m trying to send is 1000. If I try sending this I should get a high byte = 3 and a low byte = to 232.

What I’m seeing instead is something like this in my Serial Monitor (Slave/Receiver):
3
255
232
255

I notice that when I removed any delay from the code of the Master Arduino the 255 disappears. The only problem with this is again on my receiving end. I sometimes receive the high and low bytes out of order. I’m not sure how to solve this.

Master Arduino

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,4); // RX, TX
//RX on Bluetooth to TX on uC


    
void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
  
}

void loop() 
{
    int s = 1000;
    byte s1 = (s/256);    //High Byte
    byte s2 = (s%256);  //Low Byte
    mySerial.write(s1);  //Sending High Byte to Slave Arduino
    delay(20);              //Delay
    mySerial.write(s2);  //Sending Low Byte to Slave Arduino
    delay(20);              //Delay

}

Slave Arduino

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3,4); // RX, TX
//RX on Bluetooth to TX on uC

void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
}
  byte r1,r2;
void loop() 
{
  while (!mySerial.available());   // stay here so long as COM port is empty
      r1 = mySerial.read();     //reading 'High Byte' from Master
      r2 = mySerial.read();     //reading 'Low Byte' from Master
      mySerial.flush();           //attempting to remove 'random' 255 in Serial Monitor
      Serial.println(r1);
      Serial.println(r2);
      int x = r1*256 + r2;      //attempting to reconstruct original integer
      Serial.println(x);
      Serial.println("");
}

Thanks for the help!

rsxdriver90:
I am breaking integers up into a 'high' byte and a 'low' byte.

Why do you want to do that? You have now got me worried because (I think) you are a couple of steps ahead of me and I have no idea of what is going on. Now I am only interested in floats, but I would not have thought that would make any difference, and I expected to be able to send data from an HC-05 to an HC-06 in the same way as I send it to from HC-05 to the phone or a terminal on a laptop.

Serial.print(data1);

Nick_Pyner:
Why do you want to do that? You have now got me worried because (I think) you are a couple of steps ahead of me and I have no idea of what is going on. Now I am only interested in floats, but I would not have thought that would make any difference, and I expected to be able to send data from an HC-05 to an HC-06 in the same way as I send it to from HC-05 to the phone or a terminal on a laptop.

Serial.print(data1);

Hey Nick, I apologize for jumping all over the place - I really do appreciate your help! I have been working for a couple days trying to get the Bluetooth modules to send 'chars' between each other and read them successfully. Now, my problem is sending multi-digit integers. When I send a number greater than 9 (i.e. 10) the receiving end will output 1 and 0 on separate lines. (I'm sure you know this).

Thus, from what I've read on other threads on this forum, is that I need to break my multi-digit integer into high and low bytes and send them across the serial bluetooth and reconstruct them back on the receiving side. This seems to work but there is a high rate of error on my receiving end. I'm receiving some incorrect values and I'm not sure why.

If you know of a much simpler way I'm open to any suggestions :slight_smile:

Thanks in advance,
rsxdriver90

rsxdriver90:
(I'm sure you know this).

No, I don't

and

If you know of a much simpler way I'm open to any suggestions :slight_smile:

No, I don't.

I am concerned that all this has something to do with the fact you are receiving this on an Arduino via bluetooth, and I don't yet have that experience.

All my data is float. If I have a value: temp = 23.45

I just go

Serial.print(temp);

and the laptop receives that via bluetooth and prints on the terminal

23.45

This is exactly the same as when I send it to the serial monitor, and essentially the same as when I send it to display or SD card.

I suspect, and indeed hope, that this is not a bluetooth problem per se and is simply a matter of how the data is formatted.

Have you tried simply doing

int bigintnum = 546324;
Serial.print(bigintnum);

?

Can you do this just using serial monitor, with bluetooth out of the game?

i want to connect HC05 zs-040 with HC06.. I set up my HC05 as master mode with at command ..
normally when I powered both .. they will automatically connect .. but no .. I check the password .. they have the same .. if you got ideas to help me solve my problem
tell me

hi, if u having trouble in making into AT mode of HC-05 (ZS-040)(especiall if one having en/wakeup up pin instead of key pin). Follow they bellow procedure
Power off HC-05 module.
Press and hold small button above EN pin.
Power on and keep pressing small button.
Small LED should start to blink slowly about once every 2 seconds.

for more info visit below site:

I have a simillar problem.it was working just last week.but now it isn’t!.
I transmitted data between PC and android phone and printed them on Serial monitor and android device.but now just i could send data to android device and Serial monitor doesn’t print anything.

it’s code:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(12, 13);

void setup()
{
Serial.begin(9600);
BTSerial.begin(9600);
}

void loop()
{

if (BTSerial.available())
Serial.write(BTSerial.read());

if (Serial.available())
BTSerial.write(Serial.read());
}

I think it is common to use a small delay.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

It may just be a procedural problem.

Don't use (I repeat DON'T USE!!) Software serial. With speed higher than 19200 it loose bytes or change their value. You MUST connect the HC on pin O and 1 and debug using (eg) an LCD Display.
I just finish an Android App transmitting and receiving at 115200 bauds large amount of data and it works fine.

ansb:
You MUST connect the HC on pin O and 1 and debug using (eg) an LCD Display.

I have no idea how you would debug using an LCD display, but there is no need to do that anyway. If you are using Bluetooth on hardware serial with a Uno or the like, you simply do all the uploading, editing, and debugging using the serial monitor in the normal manner. This is yet another advantage of using hardware serial, pins 0,1. The only precaution is that bluetooth is disconnected while doing so, but there is never any need to have it connected during debugging anyway. You only connect bluetooth after the code is proven, and thereby ensuring that, if you have a problem then, it isn't code, it's wiring.

Nick_Pyner:
I have no idea how you would debug using an LCD display, but there is no need to do that anyway. If you are using Bluetooth on hardware serial with a Uno or the like, you simply do all the uploading, editing, and debugging using the serial monitor in the normal manner. This is yet another advantage of using hardware serial, pins 0,1. The only precaution is that bluetooth is disconnected while doing so, but there is never any need to have it connected during debugging anyway. You only connect bluetooth after the code is proven, and thereby ensuring that, if you have a problem, it isn't code, it's wiring.

If you have a communication system with checksum, time out and so on (which was my case) , using 9600 bauds communication is not very easy...
In order to dbug you just have to write what you want. If you can send data to the serial monitor, OK. But you can send them wherever you want. LCD Display is an option.
In all cases, Software Serial is not a good idea as it loose bits and so, give you wrong bytes values.
and as the first question was about "wrong values", this may came from the use of Software Serial.

While using software serial is never a great idea, it is OK up to 38400 if you are not really in a hurry and have the resources to spare. People do it all the time when configuring HC-05s with a Uno. The first questioner's "wrong values" problem was not because of software serial, he simply forgot to match the baud rates.