Arduino two Bluetooth HC-05, receiving different values on the receiver

I am currently working with 2 arduinos where each of them is connected to a HC-05 module. I configured the HC-05 such that both of them have the baudrate of 9600. I double checked that one is a master and one is a slave. However when I am sending an integer conditionally from the sender and I print out those values on the serial monitor connected to the receiver arduino, it is outputting different values.. why is this?

Sender Code

#include <SoftwareSerial.h>

#define tx 10
#define rx 11
SoftwareSerial BT(tx, rx);

int weather;

//btn//
#define btn1 2
#define btn2 3
#define btn3 4
#define btn4 5
#define btn5 6


void setup() {
  pinMode(tx, INPUT);
  pinMode(rx, OUTPUT);

  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  pinMode(btn3, INPUT);
  pinMode(btn4, INPUT);
  pinMode(btn5, INPUT);
  delay(40);

  Serial.begin(9600);
  BT.begin(9600);

}



void loop() {

  BT.listen();

  if (digitalRead(btn1) == HIGH) {
    int weather = 500;
    BT.write(weather);
    Serial.println("btn1 pressed");
    delay(50);
  }


  if (digitalRead(btn2) == HIGH) {
    int weather = 600;
    BT.write(weather);
    Serial.println("btn2 pressed");
    delay(100);

  }


  if (digitalRead(btn3) == HIGH) {
    int weather = 700;
    BT.write(weather);
    Serial.println("btn3 pressed");
    delay(100);
  }


  if (digitalRead(btn4) == HIGH) {
    int weather = 800;
    BT.write(weather);
    Serial.println("btn4 pressed");
    delay(100);

  }

  if (digitalRead(btn5) == HIGH) {
    int weather = 900;
    BT.write(weather);
    Serial.println("btn5 pressed");
    delay(100);

  }
}

receiver code

#include <SoftwareSerial.h>
#include <FastLED.h>


#define tx 10
#define rx 11

SoftwareSerial BT(tx, rx); // tx rx of bt
int c ;  
void setup()
{
  pinMode(tx, INPUT);
  pinMode(rx, OUTPUT);

  delay(40);
  Serial.begin(9600);
  BT.begin(9600);
}

void loop()
{

  BT.listen();
  c = BT.read();
  Serial.println(c);
  delay(50);
}

as seen in the code, im supposed to receive integer 500,600,700,800,and 900 respectively. But when I print out the value received from the receiver HC-05, it prints out insted 32, 188, 88, and 244...

I tried changing BT.write() to BT.println() and also tried changing datatype from integer to uint8_t and none of these work.

You are reading single ASCII characters (not values) without first checking whether any characters are there to be read.

This writes a single 8 bit binary value (not the integer value of "weather"):

    int weather = 800;
    BT.write(weather);

There is a good introduction to serial communications on this forum at Serial Input Basics - updated

I am also sending imu sensor values over the sender bluetooth to the receiver over hc-05. After reading the post you showed me, I am unsure how I can send and receive this sensor value in integer correctly...

That tutorial seems pretty clear, and there are several examples showing how to transmit different data types that you should study.

Try those examples, and if you are still confused, post the code and questions.

When you get around to communications between two Arduinos, you don't need to use the bluetooth radios for the examples. Just connect the two Arduinos with wires (TX, RX and GND).

There are plenty of other tutorials on using Bluetooth between two Arduinos.

1 Like

thank you I will take a look

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