Using HC-05 for Master-Slave connection [Solved]

Hi Guys, this my first time using the HC-05 module. I am trying to start off with a simple program that I found on youtube where, on the master node, when I press the push button, the LED on the slave node should turn. I followed the code and circuit connection as the youtube video but my LED on the slave node is not turning on. I have already used the AT command to set as master and slave and bind them.

Any help would be much appreciated, thanks.

This is my code for Master:


#define button 9
#define led 13

void setup() {
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600); 
}

void loop() {
 int bState = digitalRead(button);
 if (bState == LOW){
 digitalWrite(led,HIGH);
 Serial.write('1');
 delay(20);
 } 
 else {
 digitalWrite(led,LOW);
 Serial.write('2');
 delay(20);
 }
}

Slave Code:

char Incoming_value = 0;                //Variable for storing Incoming_value
void setup() 
{
  Serial.begin(9600);         //Sets the data rate in bits per second (baud rate) for serial data transmission
  pinMode(7, OUTPUT);        //Sets digital pin 7 as output pin
  Serial.println("Hello");
}
void loop()
{
  if(Serial.available() > 0)  
  {
    Serial.println("I am receiving");
    Incoming_value = Serial.read();      //Read the incoming data and store it into variable Incoming_value
    Serial.println(Incoming_value);
    if(Incoming_value == '1')            //Checks whether value of Incoming_value is equal to A
    {
      digitalWrite(7, HIGH);  
    }
    else if(Incoming_value == '2')
    {
      digitalWrite(7, LOW);
    }   
  }                            
 
}                 

By the way, this the output I am getting on my slave node.
image

You get text out when doing Seria.print meaning Serial is using the USB. From where do You read the wire,ess?

Which controller? Schematics please.

Is serial monitor set to the same baud rate?

I tried the code on my Uno and I get the expected result. LED on with '1' and off with '2'.

I am receiving
1
I am receiving
2
I am receiving
1
I am receiving
2

To prevent the serial monitor to send extraneous characters, set Line Endings to "No line ending" in serial monitor.

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