HC-05 two way communication serial or I2C?

In my next Escape Room (PARADOXsquared.com) I want two Arduinos to communicate via HC-05 Bluetooth Modules. I have successfully paired the modules/Arduinos-05 and the Master Bluetooth can send data to the Slave Bluetooth using: SoftwareSerial BTSerial(10, 11); // RX | TX and BTSerial.write(c);. I need the Slave to send data to the Master, but I am clueless on the how.

If this is not possible in Serial communication, can I use I2C with HC-05?

With Classic Bluetooth used by the HC05 modules, the master/slave designation refers to which unit controls the connection. Once the units are connected, they can each talk and receive to/from each other.

What have you tried, and what problems did you encounter.

Thanks for your interest cattledog.

I ran the Slave code on the Master Arduino/Master AHC05 configuration and ran the Master code on the Slave Arduino/HC05. They could not communicate. I know I2C Masters must request transmission from a Slave. I don't understand the difference between serial communication and I2C. And I don't think there is a BTSerial.request command.

Here is the master code:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
byte c;
int BT_Enable_pin = 9;
void setup() {
  Serial.begin(9600); while (!Serial) {}
  Serial.println("HC-05_bluetooth_master");
  digitalWrite(BT_Enable_pin, LOW); // data mode
  BTSerial.begin(38400); while (!Serial) {} // Bluetooth module com rate
}
void loop() {
  c = 5;
  BTSerial.write(c);
  delay(5000);
  c = 9;
  BTSerial.write(c);
  delay(5000);
}

Slave code:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
int firstRead = 0;
byte NewState;
byte FirstState;
void setup() {
  Serial.begin(9600); while (!Serial) {} 
  Serial.println("HC-05_Bluetooth_slave");
  BTSerial.begin(38400); // Bluetooth module com rate
}
void loop() {
  BTSerial.begin(38400); while (!Serial) {} // Bluetooth module com rate
  if (BTSerial.available() > 0) { // Checks for data from master BT
    NewState = BTSerial.read(); // Reads data from master BT
    if (firstRead == 0) {
      firstRead = 1;
      FirstState = NewState;
    }
    Serial.begin(9600); while (!Serial) {}
    Serial.print("FirstState "); Serial.print(FirstState);
    Serial.print("  NewState "); Serial.println(NewState);
  }
}

The HC05 modules that I have used have 9600 as the default communication mode baud rate and 38400 is the default AT mode baud rate.

The serial input basics tutorial may be of interest.

A few issues. As said, software serial should be at 9600 baud. 38400 is pushing the edge of reliability. The recommended serial input tutorial is also important.

Can you please confirm how your modules are connected to the Arduinos. What module terminal is connected to pin 10(Arduino SS Rx) and what module terminal is connected to pin 11(Arduino SS Tx)?

I recommend that you develop your slave code using a Bluetooth Serial Terminal app. I recommend Kai Morich's.
https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal&hl=en_US&gl=US

BTSerial.begin(9600); while (!Serial) {} 
Serial.begin(9600); while (!Serial) {}while (!Serial) {}

These commands do not belong in loop(). In setup() only.

When I revise your slave code as follows and send to it from the Serial Terminal App with no line endings on send, I see the code working as expected.

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
int firstRead = 0;
byte NewState;
byte FirstState;
void setup() {
  Serial.begin(9600); while (!Serial) {} 
  Serial.println("HC-05_Bluetooth_slave");
  BTSerial.begin(9600); // Bluetooth module com rate
}
void loop() {
 // BTSerial.begin(9600); //while (!Serial) {} // Bluetooth module com rate
  if (BTSerial.available() > 0) { // Checks for data from master BT
    NewState = BTSerial.read(); // Reads data from master BT
    if (firstRead == 0) {
      firstRead = 1;
      FirstState = NewState;
    }
    //Serial.begin(9600); //while (!Serial) {}
    Serial.print("FirstState "); Serial.print(FirstState);
    Serial.print("  NewState "); Serial.println(NewState);
  }
}

Thanks for the baud correction cattledog. Removing those lines of code ended the garbage characters on the Arduino Serial Monitor.

pin 10 - TXD, pin 11 - RXD

The Serial Terminal App won't install on my iphone. I could try different app, but I'm more comfortable with the computer. (I'm somewhat tech challenged. This is my 70th winter)

Yes, it is an Android app, and the HC-05 isn't compatible with iOS. You can certainly develop the BT slave code with a serial connection to the computer. When all is complete, the HC05 can replace the monitor and connect to the Serial port pins or through software serial pins.

Are the master and slave now working as you want?

I'm somewhat tech challenged. This is my 70th winter

A youngster compared with many on this forum. :wink:

Merely having one "HC" config'd as a Master and the other as a Slave is not enough.
Here's a great resource. Which approach (pair/bind or cmode) to take is your call.
MC refers to HC-05/HC-06 situations, but an HC-05 can be config'd as either M/S (so an '06 as such is not essential).

Bluetooth | Martyn Currey

This is the best way (IMO), but there's a good deal of prep involved.
Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link | Martyn Currey

My two HC05s are bound, one is Master, the other Slave. The Master sends data to the Slave (38400 baud, 9600 didn't work). I need the Slave to send data to the Master.

BT.print() on the slave should be sending to a connected master. Is that not happening?

I see no reading in the master code you posted. Have you revised the master code to read the bluetooth?

I have now, and it works! Thanks for helping me get past my assumptions.

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