ARDUINO TO ARDUINO USING HC-05 BLUETOOTH MODULE

Dear all,

Please, I am trying to communicate between two arduinos each having HC-05 bluetooth modules. I have been able to make one master and the other slave. I have also been able to pair the two of them and each can recieve byte, but the challenge I have is that the only data that is displayed is "128" no matter what I send, I still receive 128 and sometimes 248. I configured it in such a way that when you press a button on the master, the serial monitor of the slave prints a data that I am sending. I have changed the data type of the received data, and same problem exists. Here is the code for both master and slave. Your help will be appreciated.

//CODE FOR MASTER(SENDER)

#include<SoftwareSerial.h>
const int rxPin = 10;
const int txPin = 11;
const int button = 2;

SoftwareSerial mySerial(rxPin,txPin);//RX,TX.

void setup() {
 
pinMode(button, INPUT);

Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {
 
int buttonVal = digitalRead(button);
if(buttonVal == HIGH){

//mySerial.write("4");

mySerial.println('4');


}


}

[code]

//CODE FOR SLAVE(RECEIVER)[/b]

[code]
#include<SoftwareSerial.h>

const int rxPin = 10;
const int txPin = 11;


SoftwareSerial mySerial(rxPin,txPin);//RX,TX.


void setup() {
  
pinMode(13, OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);


void loop() {
  
while(mySerial.available()==0);
digitalWrite(13, HIGH);
int inData = mySerial.read();
delay(1000);
Serial.println(inData);
}

You have mySerial.println('4');
Is that exactly what you have? A numeral four between apostrophes instead of quotes. Is the SoftwareSerial syntax different?
I think you need quote marks --
mySerial.println("4");

? ?

I tried mySerial.println("4"); and it still gives me the same thing. But when I pair the HC-05 with my phone it can send and receive data appropriately.

void setup() {
  
pinMode(13, OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);

void loop()
{  
  while(mySerial.available()==0);
  digitalWrite(13, HIGH);
  int inData = mySerial.read();
  delay(1000);
  Serial.println(inData);
}

? ? ? There's nothing contingent on the while argument.

What if...

// receiver
#include<SoftwareSerial.h>
const int rxPin = 10;
const int txPin = 11;
SoftwareSerial mySerial(rxPin,txPin);//RX,TX.
byte packet;

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


void loop()
{
  if(mySerial.available != 0)
  {
    int inData = mySerial.read();
    Serial.print("# ");
    Serial.print(packet,DEC);
    Serial,print("  ");
    Serial.println(inData);
    packet ++;
  }
}

and

// transmitter
// there's no button, it just sends every 2 secs.
#include<SoftwareSerial.h>
const int rxPin = 10;
const int txPin = 11;
//const int button = 2;

SoftwareSerial mySerial(rxPin,txPin);//RX,TX.

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

void loop()
{
  mySerial.write("4");
  delay (2000);
}

Hi Thank you so much for your effort. The problem persists. what the receiver prints on the serial monitor is 255 every 2 seconds, even when I change the data sent from the transmitter. Though I modified your print syntax so as to get it to display well as shown in the attachment. Here is the modification on the receiver part of the code you sent to me:

void loop()
{
if(mySerial.available() != 0)
{
int inData = mySerial.read();
Serial.print("# ");
Serial.print(packet,DEC);
Serial.print("\t");
Serial.println(inData);
packet ++;
}
}

I really appreciate your effort. Any more suggestions will be appreciated.

serial.JPG

Thanks Runaway Pancake, I have been able to resolve the issue. I had to put my bluetooth master module in AT mode and changed the BAUD RATE to 9600 and it started working. This really gave me headache but thank God I resolved it. Thanks for your effort once again. So happy.

Perhaps you could note the steps to assert AT mode (what you did as preliminaries) so others might benefit.
( "Is that thing in 'AT mode'?" )

I probably did not understand this your question ( "Is that thing in 'AT mode'?" )

However, the steps on how to put HC-05 bluetooth module in AT command mode can be found here:

http://forum.hobbycomponents.com/viewtopic.php?f=39&t=1567

OR

Depending on the type you bought.

Oliver_Arduino:
I probably did not understand this your question ( "Is that thing in 'AT mode'?" )

I guess - it's a question that should be asked, not of you now, but anybody else in the future.

Probably a lot like our biggest dialog around here:
P - "blah, blah, blah... it not work."
R - "Did you connect the Grounds together?"
P - "Well, d'uh, of course I... I thought I did but I didn't - it's working now."

Oliver_Arduino:
However, the steps on how to put HC-05 bluetooth module in AT command mode can be found here:

HC-05 Master / Slave Bluetooth Module (HCMODU0061) - forum.hobbycomponents.com

OR

http://www.instructables.com/id/Modify-The-HC-05-Bluetooth-Module-Defaults-Using-A/step3/Steps-To-Switch-The-HC-05-Into-Command-Mode/

Just thought that you might tell everyone what you did for yours.

Waiting for your video about your "master" and "slave".

Oliver_Arduino:
Thanks Runaway Pancake, I have been able to resolve the issue. I had to put my bluetooth master module in AT mode and changed the BAUD RATE to 9600 and it started working. This really gave me headache but thank God I resolved it. Thanks for your effort once again. So happy.

Thank you so much i was having the same problem you solved it so relieved thank you again :slight_smile: