Communication among 2 arduinos using HC05 bluetooth

Hello, I am using 2 arduino nano 33 iot with two HC05 bluetooth modules. I want to transfer data (integers from 0 to 255) among them. However the data I am receiving are totally a mesh.

If I use the same code and connect arduinos with serial1 (bypassing the bluetooths) I am receiving the correct data...

SENDER CODE:

#define button 12
int buttonState = 0;
int pls = 0;
char stable = '4';

void setup() {
  pinMode(button, INPUT);
  Serial1.begin(38400);  // HC-05 default speed in AT command mode
  while (!Serial1) {
    delay(1);
  }
}

void loop() {
  buttonState = digitalRead(button);
  if (buttonState == HIGH) {
    Serial1.write(pls);
    pls = pls + 1;
    delay(1000);
   }
}

RECIEVER CODE:

#define led_pin 2

byte state3 = 0;
int state4;

void setup() {
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);

  Serial1.begin(38400);  // HC-05 default speed in AT command mode

  Serial.begin(38400);
  while (!Serial1) {
    delay(1);
  }
}

void loop() {

if (Serial1.available()>0) {  
  Serial.print("SA: ");
  Serial.println(Serial1.available());
  
  int state2 =   Serial1.read(); //Serial1.parseInt(); //
  Serial.println(state2,DEC);
  Serial.println(state2>>2, DEC);
  Serial.println(char(state2));
  Serial.println(state2);
  Serial.println(state2);

 if (state4 == 0) {
    Serial.println("YES");
    digitalWrite(led_pin, HIGH);  // LED ON
    state4 = 1;
  } else if (state4 == 1) {
    Serial.println("NO");
    digitalWrite(led_pin, LOW);  // LED ON
    state4 = 0;
  }
}

Could you please help on that?

Thanks!

Re you sure that the baud rate of the two HC-05 modules is the same and that it is 38400 ?

I just found the solution. The baud rate is 38400 in AT mode but it is 9600 in the connection mode. Now everything works perfect..

Thanks!

1 Like

Good news !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.