read the status of an relay...is it ON or OFF

I cant quite get my code... I have searched the web to find out how to serial.print og lcd.print the status of my 4 relays... they are managed by my sensors... but when I want to check the status of an relay I just get Output pins they are connected to and not 1 or 0... (On or off).

#define RELAY_ON 0
#define RELAY_OFF 1

#define Relay_1  2  
#define Relay_2  3
#define Relay_3  4
#define Relay_4  5



//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);  
  
//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  delay(200); //Check that all relays are inactive at Reset

you can do a digitalRead() to check the status of a pin - so indirect the status of your relay.

I have tried digitalRead().. but it still gives my the connected pin number... I need help to code that little part...

digitalRead (Relay_1);
Serial.println(Relay_1);
delay(200);

do like this...

Serial.println(digitalRead (Relay_1));

Thank you so much!! It worked!!! :slight_smile:

Thanks for the Karma :grin:

digitalRead (Relay_1);
Serial.println(Relay_1);
delay(200);

This way the output of digitalRead() is thrown away.
And you just print the value of the variable Relay_1.

int relayState = digitalRead(Relay_1);
if (relayState == LOW)
{
  Serial.println("LOW");
} else {
  Serial.println("HIGH");
}

Please read the reference and tutorial section e.g. about how to use digitalRead() - digitalRead() - Arduino Reference -
There is a lot to learn there.

If you used variable names that make sense, the code would be easier to write (and follow).

One can not look at a variable named Relay_1 and have any clue what it does. But, names like RelayPin1 and relayState1 are quite clear about what they contain/should be used for.

It then becomes obvious that digitalRead(relayState1) is wrong, since the function takes a pin number, and that digitalRead(relayPin1) is the correct way to call the function.

Of course, looking at the reference page, and seeing that each function does is a good idea. You would see that a function like digitalRead() returns a value that you should not be throwing away.

Of course, reading the state of the relay is silly, anyway. You should keep track of what you set ti to.

Of course, reading the state of the relay is silly, anyway. You should keep track of what you set ti to.

But PaulS, the pin itself keeps track of the state of the relay? OK reading it back is sloooww compared to a variable.

Well if one want's to get anal about it the best and most positive method to determine the state of a relay output is to use relays with extra contact pairs and have one set of contacts be read back with a digitalRead(). That is the only way one can be positive the relay indeed engaged or not in case of say external DC relay power failed or the relay coil opened up, or the relay coil driver switching transistor failed, or a broken wire or even a damaged output pin, or etc.

At our refinery all our Safety Shutdown Systems required positive external hardware feedback sensing on all controller controlled outputs and typically we used triple inputs signals where it took >= two out of three signals in agreement/voting as to what input value to except.

Lefty

Lefty, and if one contact of the relay does work properly and the other not? I'm sure you never can be sure :wink:

robtillaart:
Lefty, and if one contact of the relay does work properly and the other not? I'm sure you never can be sure :wink:

Yes, and a team normally goes through a risk assessment and do consider the probability of the single point failure when making such decisions.

Lefty