HC05 Communication Issue

Hello All,

I'm having an issue with sending information from one Nano to another using the HC05 bluetooth module. I had it working but somehow deleted my code so I re-wrote it. I got the modules talking and have good two way communication, but when I press a button on the SLAVE device, a relay is supposed to close on the MASTER device but it's not.

On the MASTER device there is a reed switch which is supposed to turn an LED from green to red when the magnets are separated. This part is working fine. Can someone please take a look at my code and see what I'm getting wrong? Thanks in advance.

MASTER CODE

#include <SoftwareSerial.h>
#define redLed A0
#define greenLed A1
#define blueLed A2

SoftwareSerial mySerial(3, 2); //Rx, Tx

int GarSwitch = 4;
int relay = 5;

void setup() {
  Serial.begin(115200);
  mySerial.begin(38400);
  pinMode(GarSwitch, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
  digitalWrite(blueLed, LOW);
}

void loop() {

  // Sending
  int Gar1 = digitalRead(GarSwitch);

  if (Gar1 == HIGH) {
    mySerial.print('a');
  }

  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (mySerial.read() == 'b') {
    digitalWrite(relay, LOW);
    delay(25);
  } else {
    digitalWrite(relay, HIGH);
  }
}

SLAVE CODE

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2); //Rx, Tx

int redLed = 7;
int greenLed = 6;
int blueLed = 5;
int Button = 4;


void setup() {
  Serial.begin(115200);
  mySerial.begin(38400);

  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}

void loop() {
  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (mySerial.read() == 'a') {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    delay(1);
  } else {
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
  }

  // Sending
  int button = digitalRead(Button);

  if (button == LOW) {
    mySerial.print('b');
    Serial.print('b');
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
    digitalWrite(blueLed, HIGH);
  } else {
    digitalWrite(blueLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, LOW);
  }
}

The first use of mySerial.read() is pulling the 'b' from the buffer and will not be there for the second reading.

Try

 if (mySerial.available())
char val = mySerial.read();
   // Serial.write(mySerial.read());
  Serial.write(val);

 // if (mySerial.read() == 'b') {
if (val == 'b') {

Thanks very much but it didn't work. I actually had the code and deleted the entire folder that it was in. I had it working and now I just can't remember what I did. All the hardware is working, I tested every piece but for some reason I can't find the code solution. Completely stumped. Using the button on the slave to close a relat momentarily.

Do you really have your HC-05 set up for 38400 ?
Usually they're 9600 (unless you 'AT' them to something else)._

Yeah I set them for 38400. Thanks. Like I said I had them working then lost the code. I'm so bummed! :slight_smile:

No problem. Start over again following Martyn Currey's tutorials:
https://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/

It is much faster to write code a second time.

Troubleshoot with a sketch that sends 'b' once a second and see if that turns up on your smartphone with BT Serial Terminal.

Good Morning All !!

So I'm getting a little closer, I did what @runaway_pancake said and broke the sketch down to bare basics. The communications part of it seems to work fine, however, I can't get the relay to close with a button push from the other side. Let me elaborate.

On the SLAVE device there is a push button. When I press the button I see the "a" populate on the serial monitor of the MASTER device. So it's communicating. When I separate the reed switch on the MASTER device, I see "b" in the serial monitor of the SLAVE device. Everything works except when the MASTER sees the "a" it doesn't close the relay. It's just a 5 volt relay soldered to the PCB.

I tested the relay and it works. If I manually set it to LOW in the sketch it closes just fine. I thought I had it set correctly but apparently I don't. Please take a look at my bare bones sketch as well as the pictures demonstrating communication. Thanks again.


strong textMASTER CODE

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);

int Relay = 5;
int GarSwitch = 4;

void setup() {

  Serial.begin(115200);
  mySerial.begin(38400);

  pinMode(GarSwitch, INPUT_PULLUP);
  pinMode(Relay, OUTPUT);
}

void loop()
{
  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (mySerial.read() == 'a')
  {
    digitalWrite(Relay, LOW);
  }
  else
  {
    digitalWrite(Relay, HIGH);
  }

  // Sending
  int button = digitalRead(GarSwitch);

  if (button == HIGH)
  {
    mySerial.print("b");
  }
}

SLAVE CODE

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 2);

#define redLed    7
#define greenLed  6
#define blueLed   5
#define Button    4
#define pwrLed    A0

void setup() {

  Serial.begin(115200);
  mySerial.begin(38400);

  pinMode(Button, INPUT_PULLUP);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(pwrLed, OUTPUT);
}

void loop() {
  // Sending
  int button = digitalRead(Button);

  if(button == LOW)
  {
    mySerial.print('a');
    Serial.println('a');
    digitalWrite(blueLed, HIGH);
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, LOW);
    delay(1);
  }
  else {

    digitalWrite(blueLed, LOW);

  }

  // Receiving
  if (mySerial.available())
    Serial.write(mySerial.read());

  if (mySerial.read() == 'b')
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    delay(25);
  }
  else
  {
    digitalWrite(greenLed, HIGH);
    digitalWrite(redLed, LOW);
  }
}

You did not fix the issue mentioned in reply #2.

Actually I fixed it, everything works as it should. I feel kinda dumb for this but the problem was with one of my HC-05 modules. This small PCB is going into a small project box so I had to de-solder the angled pins, the re-solder straight pins. Something must have gotten too hot and the modules only worked one way. I setup two new modules and everything is good. I'm sorry for wasting everyones time but thanks for helping me anyway. :face_with_open_eyes_and_hand_over_mouth: I'm so embarrassed.

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