Hi ! Can anyone help me to solve problems between 2 bluetooth HC-05 connection

Im doing a project to communicate between 2 HC-05

  1. HC-05 master and connect with Arduino UNO
    TX HC-05 ------> RX pin 0
    RX HC-05 ------> TX pin 1
  2. the other HC-05 connects with Arduino Mega
    TX HC-05 ------> RX pin 0
    RX HC-05 ------> TX pin 1
    When I turn on power of two bluetooth module HC-05. At first, they flashing rapidly, then flashing 2 times/second
    One thing Im not sure is that whether they are carefully connecting to each other
    And if their connection is successful, the code does nt work
    From the master, I would like to send data ( number 1) and after the slaver receives the data, the light will turn on.
    Can anyone help me to solve the problem and also give me some simple test codes to check the connection between 2 modules.
    Code of sender
#include <SoftwareSerial.h>
 int bluetoothTx = 0; // TX-O pin of bluetooth mate, Arduino D0
int bluetoothRx = 1; // RX-I pin of bluetooth mate, Arduino D1
int val; 
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()

{

Serial.begin(9600);

bluetooth.begin(9600);

}
void loop()

{

val = 1;// 

bluetooth.print(val); // to send data (number 1)

delay(100);

}

code of receiver

#include <SoftwareSerial.h>
 
int bluetoothTx = 0; // TX-O pin of bluetooth mate, Arduino D0
int bluetoothRx = 1; // RX-I pin of bluetooth mate, Arduino D1
 
int led = 12;
 
int buttonPin1 = 7;
int buttonPin2 = 8;
int button1State = 0;
int button2State = 0;
 
int dataFromBt;
 
boolean lightBlink = false;
 
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
 Serial.begin(9600); // Begin the serial monitor at 9600bps
 
 bluetooth.begin(38400); // The Bluetooth Mate defaults to 115200bps
 bluetooth.print("$"); // Print three times individually
 bluetooth.print("$");
 bluetooth.print("$"); // Enter command mode
 delay(100); // Short delay, wait for the Mate to send back CMD
 bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
 // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
 bluetooth.begin(9600); // Start bluetooth serial at 9600
 pinMode(led, OUTPUT);
 pinMode(buttonPin1, INPUT);
 pinMode(buttonPin2, INPUT);
}
 void loop()
{
 
 if(bluetooth.available()) // If the bluetooth sent any characters
 {
 // Send any characters the bluetooth prints to the serial monitor
 //Serial.println((char)bluetooth.read());
 
 dataFromBt = bluetooth.read();
 
 if(dataFromBt == '1'){
 Serial.println("led on");
 digitalWrite(led, HIGH);
 bluetooth.print("1");
 }
 if(dataFromBt == '0'){
 Serial.println("led off");
 digitalWrite(led, LOW);
 bluetooth.print("0");
 }
 if(dataFromBt == 'b'){
 Serial.println("a");
 lightBlink = true;
 }else{
 lightBlink = false;
 }
 }
 
 if(Serial.available()) // If stuff was typed in the serial monitor
 {
 // Send any characters the Serial monitor prints to the bluetooth
 bluetooth.print((char)Serial.read());
 }
 
 // and loop forever and ever!
 if(lightBlink){
 digitalWrite(led, HIGH);
 bluetooth.print("1");
 Serial.println("HIGH");
 delay(500);
 digitalWrite(led, LOW);
 bluetooth.print("0");
 Serial.println("LOW");
 delay(500);
 }
}

receiver.txt (1.98 KB)

Hi! Thanks for your comment. I also re-edit my post. If you have time, just come back and give me some advices

Since you edited your initial post, I removed mine. :)

I'm not really familiar with Bluetooth, so can't really help, but this caught my attention:-

bluetooth.begin(38400); // The Bluetooth Mate defaults to 115200bps

You set the actual baud rate to 38400, yet the comment says 115200. Could this possibly be the problem?

Why not use Serial1 on your Mega to communicate with the HC05 and use Serial to show debug messages on your Serial Monitor.

I presume your Uno is NOT connected to the PC using the USB cable - because that also uses pins 0 and 1. If you want to use the USB cable you should use SoftwareSerial to create another Serial port on the Uno using different pins. However stick with 9600 baud for SoftwareSerial until you are satisfied that it works.

There are many possible causes of error in what you are doing and it will take patience and a very methodical approach to eliminate them all.

One useful thing might be to make sure each HCO5 can communicate with Bluetooth on your PC - at least then you know the problem must be on the Arduino (and only on 1 Arduino).

…R

Hi Robin! I would like to describe clearly my project to you. I am doing robot arm controlled by potentiometers and I come up with new ideas to control them via by bluetooth. Arduino Uno is used to read analog value of potentiometers, then sending data to Arduino Mega to control servo I also test each bluetooth modules as slavers. Both are ok. After that I set one of them as a master by At command, as At+Role=1.

Im making a robot arm controlled by potetiometers and I also use 2 arduino boards. One is Uno and the other is Mega. I want Uno to read analog value from potentiometer, then it sends to data to Mega via by bluetooth HC-05. I have already set master and slaver for HC-05 modules.
Now, I still confuse how to write the code to send data from Uno as well as receiver code in Mega.

  1. I have tried to write one in Uno ( master), but Im not sure about my code
  2. I also doubt the pins to connect bluetooth HC-05
    Lets give me some advices and share your wisdom. If you have some sample codes, please send me
    I really appreciate to your helps
#include <SoftwareSerial.h>
 
int bluetoothTx = 10; // TX-O pin of bluetooth mate, Arduino D10
int bluetoothRx = 11; // RX-I pin of bluetooth mate, Arduino D11 
int val; 
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()

{

Serial.begin(9600);

bluetooth.begin(9600);

}
void loop()

{

val = 1;



bluetooth.print(val);

delay(100);

}

DON'T DOUBLE POST

...R

phamb587: I would like to describe clearly my project to you.

You have not responded to the first 2 paragraphs in my Reply #3

...R

MOderator: double posts merged (please do not waste time)