HC-05 State Pin

Hi,
I'm new with arduino programming and all that stuff but managed to make a project in which i want to unlock a door with my android phone.So far everything worked perfectly until i wanted to add the auto lock/unlock feature.
I made the android app send the commands for lock and unlock(manually pressing buttons),they work perfectly.It reconnects automatically when the app is started to the bluetooth module(HC-05).

My main concern is that the STATE pin returns only '0' value altho the bluetooth module is connected with my phone. I have no clue why or what my mistake is on the coding part involving arduino.
I want to use the STATE pin as a value that will be used by the Android app when i activate auto lock/unlock feature.

I used the " Serial.println(digitalRead(BTpin));" command to see what the pin STATE returns,that is the only reason it is included in the command without a specified statement attached to it.

I did a ton of searching on the internet about how to code it right and so on but nothing worked since STATE pin only returns the value '0'.Now i am confused and i have no clue what worked or what might have worked.

What i am trying to make arduino on a logical thinking: "If the bluetooth module is connects to the phone unlock the door, when the connection is lost lock the door." On a more detailed level i will make the app send a value/string(whatever) when in auto mode, which arduino will read it and unlock the door,but arduino will be the one checking if the connection is still available and if not it will lock the door.

#include <SoftwareSerial.h>
#define ledPin 7
#define autoPin 8
#define BTpin 12
SoftwareSerial BTserial(2, 3);


int state = 0;
void setup()
 {
  pinMode(BTpin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0)
    { 
    state = Serial.read(); 
    }
  
          if (state == '0')
          {
          digitalWrite(ledPin, HIGH);
          delay(500);
          digitalWrite(ledPin, LOW);
          Serial.println("Locked"); 
          Serial.println(digitalRead(BTpin));
          state = 0;
         
          }
          else if (state == '1')
          {
          digitalWrite(ledPin, HIGH);
          delay(500);
          digitalWrite(ledPin, LOW);
          Serial.println("Unlocked");
          Serial.println(digitalRead(BTpin));
          state = 0;
          }
}

My main concern is that the STATE pin returns only '0' value

The state variable is not valued by reading a pin, so I have to wonder what you are talking about.

PaulS:
The state variable is not valued by reading a pin, so I have to wonder what you are talking about.

My mistake that i didn't mention the fact that the android app while pressing the lock/unlock button sends the bytes 48 or 49(ASCII) and 'state' is used to store those two values.Altho they might look the same for unlock statement arduino will have a longer delay than the lock statement.
The ledPin 7 is actually controling a relay.

The thing is that yes i can and know how to read that STATE Pin from the bluetooth module but it returns only '0'/LOW so basically i can't use it.It never changes to HIGH state when my phone connects to the module.The question is why it doesn't change,do i need to tell it to change from LOW to HIGH from the Android app? I doubt that,it should return that by itself when connected or disconnected from the phone/app.

Post a link to the device you are using. It is not clear what the STATE pin is trying to tell you, but, I doubt that the STATE refers to connected vs. disconnected.

Good job using code tags with your first post.

I used the " Serial.println(digitalRead(BTpin));" command to see what the pin STATE returns

Are you saying that digitalRead(BTpin) returns a value of 0 when the 2x flash (not the unconnected rapid flash) of led on the module indicates the phone is connected to the module.

void setup() {
  Serial.begin(115200);
  pinMode(12,INPUT);//connnected to State pin
}
void loop() {
  Serial.println(digitalRead(12));
  delay(1000);
}

"If the bluetooth module is connects to the phone unlock the door, when the connection is lost lock the door."

You need to make the lock/unlock code dependent upon the digitalRead() of the state pin, and not dependent upon some entry from Serial.

The thing is that yes i can and know how to read that STATE Pin from the bluetooth module but it returns only '0'/LOW so basically i can't use it.It never changes to HIGH state when my phone connects to the module.The question is why it doesn't change,do i need to tell it to change from LOW to HIGH from the Android app? I doubt that,it should return that by itself when connected or disconnected from the phone/app.

On my HC05 the state pin indeed indicates the connection state.

cattledog:
You need to make the lock/unlock code dependent upon the digitalRead() of the state pin, and not dependent upon some entry from Serial.

But, the entry from Serial defines whether the phone wants the door locked or unlocked.

What OP is trying to do, as I understand it, is to lock the door when the phone drops the connection by going out of range.

With my phone and module, the digitalRead() of the state pin indeed transitions from 1 to 0 when the phone goes out of range and the connection is dropped.

So, this is the new code and i changed the int state to int command to get rid of the confusion regarding the STATE pin.

#include <SoftwareSerial.h>
#define ledPin 7
#define BTpin 12
SoftwareSerial BTserial(2, 3);


int command = 0;
void setup() {

  pinMode(12, INPUT);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0)
    { 
    command = Serial.read(); 
    }
    Serial.println(digitalRead(12));
    delay(1000);
  
          if (command == '0')
          {
          digitalWrite(7, HIGH);
          delay(500);
          digitalWrite(7, LOW);
          Serial.println("Locked"); 
          command = 0;
         
          }
          else if (command == '1')
          {
          digitalWrite(7, HIGH);
          delay(500);
          digitalWrite(7, LOW);
          Serial.println("Unlocked");
          command = 0;
          } 

}

This indeed returns the values 1 and 0 finally.
I am tho confused why just changing addressing the pins directly with the numers of the pins instead of their defined names made a difference in returning the 1 and 0 correctly.
I'm tired right now after 7-8 hours struggling with this,maybe after a good sleep tomorrow i'll manage to get a grip on my thinking and fully understand it.Now i can sleep with my mind at peace for today hehe.
Either way i couldn't have done it without you guys,many many thanks for your time and help.Highly appreciated !!!

I am tho confused why just changing addressing the pins directly with the numers of the pins instead of their defined names made a difference in returning the 1 and 0 correctly.

It didn't. You also change WHEN you read the pin. Reading the pin only when there is serial data to read, as you were doing, means that the device must have been connected.

Anonymous printing sucks. Print something before every value, so you know what the value means.

Reading the pin only when there is serial data to read, as you were doing, means that the device must have been connected.

Agreed. But then the OP should have seen 1's.
This is what I got running his sketch

Unlocked
1
Locked
1
Unlocked
1
Locked
1

My main concern is that the STATE pin returns only '0'

The fact that he was seeing 0's makes me think he somehow corrected a wiring problem. When he wakes up in the morning, perhaps he'll figure out what was going on. :slight_smile:

cattledog:
Agreed. But then the OP should have seen 1's.
This is what I got running his sketch

Unlocked

1
Locked
1
Unlocked
1
Locked
1




The fact that he was seeing 0's makes me think he somehow corrected a wiring problem. When he wakes up in the morning, perhaps he'll figure out what was going on. :)

It was printing

Unlocked
0
Locked
0
Unlocked
0
Locked
0

:slight_smile:

wiring didn't change at all,i don't even remember what i did yesterday in all that chaos :))
At least i learned a lot so can't complain at all :>

Hello guys, does anyone know why "STATE" pin is always LOW, 0V. I read it should be HIGH 3,3V when connected to module. I have connection and all seems to be working except this STATE signal and I need it to upload scetch via bluetooth but I am unable to because this signal is missing. I also tried solder wire to pin32 and try new module,but no succes. HC05 ZS-404, with Key button, EN and State pin. Thanks

Hello guys, does anyone know why "STATE" pin is always LOW, 0V. I read it should be HIGH 3,3V

Yes, the STATE pin should be HIGH when the module is connected and low when not connected. It is supposed to be tied to PIO9 of the chip.

There is an AT command to invert the behavior, but if it's always low, this setting does not appear to be the issue.

Can you read the STATE pin with a voltmeter. Perhaps the actual output is less than 3.0V which is the guaranteed value to give a HIGH with digitalRead().

It is 0V, I also checked with scope if there is any signal, but nothing. It is version 3.0-20170601. I guess something is wrong with modules or what, but it seems to be working if connection can be established.

Can you get a good photo of the chip side of your module. Typically PIO11 PIO9 and PIO8 will have solder blobs indicating connection to the ZS carrier module.

It certainly appears that PIO9 is connected to the module.

I also checked with scope if there is any signal, but nothing

Did you check at the chip pin, or the PIO9 solder blob? If the signal is at the blob, you should be able to solder a wire to the state pin. If there is no output from the chip/blob you are out of options. :frowning:

Check continuity from pin32 to state pin OK, solder good. I guess I have to order new one and try

Hi guys,
just wanted to let you know, I bought same HC05 module but from different seller and state pin works.
The new and old one have the same firmware 3.0-20170601.