HC-05 Master and Slave connection problems

Hi,

I'm trying to control a LED with a pushbutton using two different arduino uno's and a pair of HC-05 bluetooth devices. I followed this instruction to setup and pair the devices: How to Pair HC-05 Bluetooth Modules - YouTube

After the pairing, both HC-05 seems to be connected, but when I try to upload code and interact with the button on board 1 to turn on the LED on board 2, nothing happens. What am I missing?
I have also seen a lot of different connection options on different sites and forums, but should I use voltage dividers for the HC-05?

Master Setup:

Slave Setup:

Same as Master, only connected LED

Master AT Setup:

+ROLE:1

+UART:38400,0,0

+BIND:98d3:71:fd807d

Slave AT Setup:

+ROLE:0

+UART:38400,0,0

+ADDR:98d3:71:fd807d

Master code:

SoftwareSerial mySerial(10,11);

//Master-program


#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);

//Master-program

int LEDbut = 3;
int LEDVal;
int dt = 50;


void setup() {
  // put your setup code here, to run once:

  pinMode(LEDbut, INPUT);
  digitalWrite(LEDbut, HIGH);
  Serial.begin(38400);
  mySerial.begin(38400);

}

void loop() {
  // put your main code here, to run repeatedly:

  LEDVal = digitalRead(LEDbut);

  if (LEDVal == 0) {
    mySerial.write('0');
  }
  else {
    mySerial.write('1');
  }
  Serial.println("butVal: ");
  Serial.println(LEDVal);

  delay(dt);

}

Slave code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial (10,11);


//Slave-program
int state;
int blueLed=8;
int dt=50;


void setup() {
  // put your setup code here, to run once:

  pinMode(blueLed,OUTPUT);
  Serial.begin(9600);
  mySerial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

  if(Serial.available()>0){
    state=mySerial.read();
    }

   if(state=='0'){
    digitalWrite(blueLed,HIGH);
    //state='b';
    }

   if(state=='1'){
    digitalWrite(blueLed,LOW);
    state='1';
    }

   Serial.println(mySerial);
   delay(dt);
}

did you mean

   Serial.println(state);

you are sending the message 20 times per second... you could send it only when the button changes state... see the state change detection example in the IDE


side note:

usually we use

  pinMode(LEDbut, INPUT_PULLUP);

note that when button is not pressed, it will read HIGH, so you want to send '0' and it will be 'LOW' when pressed, so send '1' then

1 Like

I think you meant to be:

  if(mySerial.available()>0){
1 Like

@OP

You may practice the following Tutorial (tested using UNO-NANO) and then you may correct your sketch of post #1. When a Step/Section is done. put Tick Mark on it.

1. Check that your hardware setup agrees with the setup of Fig-1.


Figure-1:

2. == Slave Configuaration== by AT Commands
(1) Remove 5V from Slave-BT.
(2) Connect EN-pin of Slave-BT with 3.3V.
(3) Press and Hold the micro switch of Slave-BT.
(4) Connect 5V with Slave-BT.
(5) Wait for3/4 seconds and then release the micro switch.
(6) Check that the Red LED of Slave-BT blinks once in 2-sec indicating that the Slave-BT has entered into AT Command Mode.

(7) Upload the following sketch into Slave-UNO (UNO-2 of Fig-1).

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);  //SRX at UNO = 10, STX = 11

void setup()
{
  Serial.begin(9600);
  SUART.begin(38400);   //AT command Baud Rate
}

void loop()
{
  if (SUART.available())
  {
    char x = SUART.read();
    Serial.print(x);
  }

  if (Serial.available())
  {
    char c = Serial.read();
    SUART.print(c);
  }
}

(8) Open Serial Monitor (SM) of UNO-2 with BD = 9600 along with "Both NL & CR" option for the Line ending Tab.

(9) Enter AT+RMAAD (To clear any paired devices) in the InputBox of SM of UNO-2 and then click on Send Button. The BT will respond with OK message.

(10) Send AT+ROLE=0 (To set BT-2 as Slave) command to BT-2. The respond should be OK.

(11) Send AT+ADDR (To get the address of Slave-BT) command to BT-2. Note down the address as it will be used during Master configuration). For my BT it is: 21:13:A36B (It will be entered as: 21,11,A36B during Master configuration).

(12) Send AT+PSWD (to know password/PIN of Slave-BT). It must be same as the Master; otherwise no pairing will occur. command to Slave-BT. For my BT it is: 1234.

(13) Send AT+CMODE=1 (Slave connect mode) command to Slave-BT.

3. //===Master Configuration=== by AT Commands
(14) Bring Master-BT into AT Command Mode following the same steps of Slave-BT.

(15) Upload the following sketch into UNO-1.

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);  //SRX at UNO = 10, STX = 11

void setup()
{
  Serial.begin(9600);
  SUART.begin(38400);   //AT command Baud Rate
}

void loop()
{
  if (SUART.available())
  {
    char x = SUART.read();
    Serial.print(x);
  }

  if (Serial.available())
  {
    char c = Serial.read();
    SUART.print(c);
  }
}

(16) Send AT+RMAAD command to Master-BT.
(17) Send AT+ROLE=1 (To set it as Master) command to Master-BT.
(18) Send AT+PSWD=1234 command to Master-BT (Password/PIN should be same as Slave).

(19) Send AT+CMODE=0 (Master connect mode) command to Master-BT.
(20) Send AT+BIND=21,11,A36B command to Master-BT (the Slave address found in Step-11; the colons (: ) are replaced by commas (,)).

4. //=== End of Configuration===

(21) Remove 3.3V and 5V connections from both Master-BT and Slave-BT.
(22 Connect the 5V supplies to both BTs.
(23) Check that the BTs have been paired. The Red LEDs of both BTs blink once a while.

5. ===Checking communication between UNO-1 and UNO-2 via BTs==
Every time you press the Ledbut switch at the UNO-1 side, the blueLed at the UNO-2 side will just flash for once. And at the same time, the Serial Monitor of UNO-2 will show this message: "A Message is received from Master".

(24) Upload the following Sketch in UNO-1.

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);  //SRX at UNO = 10, STX = 11
#define Ledbut 3

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  pinMode(Ledbut, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(Ledbut) == LOW)
  {
    SUART.print('L');
  }
  delay(500);
}

(25) Upload the following sketch in UNO-2.

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);  //SRX at UNO = 10, STX = 11
#define blueLed 13

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
  pinMode(blueLed, OUTPUT);

}

void loop()
{
  if (SUART.available())
  {
    char x = SUART.read();
    if (x == 'L')
    {
      digitalWrite(blueLed, HIGH);
      delay(200);
      digitalWrite(blueLed, LOW);
      delay(200);
      Serial.println("Message received from Master.");
    }
  }
}

(26) Open SM of UNO-2 with Bd = 9600.
(27) Press and release Ledbut switch at Master side.
(28) Check that the blueLed at Slave/UNO-2 side just flashes for once.
(29) Check that the SM of UNO-2 shows the following message.

A Message is received from Master.

6. ==End of Tutorial==.

1 Like

Yeah you're right, was ment to be Serial.println(state). I'm aware of the INPUT_PULLUP command, I just learned it in another way. But thank you for the tip about the example in the IDE.

Thank you! I will correct the code and check if it works.

Wow, thank you for such detailed instructions! I will follow the steps in the tutorial and see if it solves the issue!

I have now tried every suggestion posted so far, but nothing seems to work. Everything seems to work right up the AT synchronization. The devices appears to be connected due to the synchronized blink after both BT are finished with the setup. Is there anything else i am missing? does the power supply source mather? I notice when a device is connected to the pc, the rx/tx channels on the UNO blinks, but when it's connected to just a 5v power supply nothing happens.

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