HC-05 to HC-05 Bluetooth communication with Arduino uno and nano

I have configured both the HC-05 as a master-slave pair and they are able to pair as well. However, when I try to send out any data through a simple code and receive it, the received data is always a number. First I thought it was a random number then I realized the outputs repeat for a specific input.

Eg: I send out 123 then, I receive 255
I send out "hi" I receive 247
I send out "hello" I receive 206 and 255

Note that these outputs are always the same for the mentioned inputs, and I have tested both with numbers and string

For testing, I connected the module to my mobile phone and received it through a serial Bluetooth app on phone. The data was received perfectly as was sent. The above garbling of values was not happening.

Note that I have also tried avoiding the usage of <SoftwareSerial.h> library and directly connecting to rx and tx of the arduino uno and nano, with and without a voltage divider at the receiver, so that is not the issue.

Receiver Code:
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx, tx); //RX, TX
void setup()
{
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}
void loop()
{
  if(bt.available()>0)
  {
    Serial.println(bt.read());
  }
}

Sender Code:
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx,tx); //RX, TX
void setup() 
{
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}
void loop() 
{
  bt.write(123);
  delay(5000);
}

I am using the above codes for receiver and sender respectively
Can you please help me debug this problem?
Note: I am using this Bluetooth connection to send accelerometer data from an MPU6050 module connected to Arduino nano (Sender) to a robotic arm servo motors connected to Arduino uno (receiver).

Receiver Code:
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx, tx); //RX, TX
void setup() {
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}
void loop() {
  if (bt.available() > 0)Serial.print(bt.read());
}

Sender Code:
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx, tx); //RX, TX
void setup() {
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}
void loop() {
  bt.println(123);
  delay(5000);
}

By default, the above statement prints the numeric value of the single character received.

Please post the send and receive code for this case:

I send out "hello" I receive 206 and 255

Good tutorials on HC-05 and HC-06 here:
https://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-in-slave-mode/

Sender Code:
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx,tx); //RX, TX
void setup() 
{
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}
void loop() 
{
  bt.write("hello");
  delay(5000);
}

As you have said Serial.println(bt.read()); numerical value but it's gives me numbers like 255 and 247 when I send out 123.
Thank you for the reply.
This is the code I used to send hello

Please post the receive code, as that is where the problem lies.

I used the same receive code.
Also, note that the end result I want is to send out numerical angle values from the accelerometer to the servo motors. The string was just to troubleshoot.

Please post a wiring diagram (hand drawn is preferred, with pins and connections clearly labeled) and state which Arduino you are using.

If you are using a 5V Arduino, do you have the required logic level converter between Arduino TX and the 3.3V Bluetooth RX? If not, you will damage both boards.

This is the basic circuit diagram

@roadhog31
Check if your Bluetooth Devices' connections with UNO/NANO are similar to Fig-1:


Figure-1:

You can create test skecthes to see that whenever LEDbut switch (Fig-1) is pressed at the sender/Master side, the blueLed at the receiver/Slave side turns on.

The diagram is difficult to interpret and lacks pin designations on the radios, but it might be OK.

When sending the string "hello", five characters should have been sent, and five numbers representing those characters should have been printed, specifically 104 (for ASCII h), 101 (for ASCII e), etc. but I don't see any obvious explanation for your observations.

did you tested my sketches?

Maybe the two BT modules are not actually talking to each other, and you are receiving random noise. Take a look at the tutorials on Martyn Currey's site, linked above.

I found out that 1 of the modules is version 2.0 and the other is version 4.0 using the AT+VERSION command in command mode.
Is that why the data is getting garbled, but also has a pattern?
Another observation I have made is that, whenever I make the new one as the master and give the old one's address to bind, the pairing is not happening. But, when I give the new one's address to the old one to bind and make the old one the master, the pairing is happening.
Does anyone have a suggestion apart from replacing the module?

I believe v4 are fake/dud/crippled but work fine if you just use them as slaves and don't do much configuration.

That still doesn't explain why the data is getting corrupted when setting up v4 as the slave and v2 as the master and they both have successfully paired.

You need to isolate the problem by doing seperate standalone tests for your BT Modules before operating them as Master/Slave.

1. Check V2 alone usong UNO and Android phone for correct data exchange.
2. Check V4 alone using UNO and Android phone for correct data exchnage.

I tried both modules individually with my phone, keeping their configurations unchanged, that is, v2 is still the master and v4 is still the slave and master is bound to the address of the slave.
The v4 module is working perfectly in both receiving data sent out through phone (only numerical values according to code) and sending data (123 is sent out though v4 module) and receiving through the phone.

However, the v2 module is not, it's is receiving 0 always, and when it sends out data, the phone receives corrupted data
Sender code:

#include <SoftwareSerial.h>

#define tx 2
#define rx 3

SoftwareSerial bt(rx, tx); // RX, TX

void setup() {
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}

void loop() {
  int dataToSend = 123;
  String dataString = String(dataToSend); // Convert integer to string
  bt.print(dataString);
  Serial.println(dataString);
  delay(5000);
}

Receiver code:

#include <SoftwareSerial.h>

#define BT_RX_PIN 3
#define BT_TX_PIN 2

SoftwareSerial bt(BT_RX_PIN, BT_TX_PIN); // RX, TX

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

void loop() {
  if (bt.available()) {
    String data = bt.readStringUntil('\n');  // Read entire message until newline
    int value = data.toInt();               // Convert string to integer
    Serial.println("Received value: " + String(value));
  }
}

Should I try AT+ORGL on the v2 module and try again?

1 Like

So, the initial guess is that V2 is probably bad.

Not your issue but you do not need a String to print an integer

Just do

  int dataToSend = 123;
  bt.print(dataToSend);

As print() does generate the ASCII representation of the number already.


On the receiver side if you don’t want to see the ascii codes, use Serial.write()

Hello everyone, turns out the issue was the version difference or with the module, because now I tried the same master-slave configuration with 2 version 4 HC-05 modules and they worked perfectly.

Thank you all for your support and help!