Go Down

Topic: read the status of an relay...is it ON or OFF (Read 794 times) previous topic - next topic

pjac82

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).

Code: [Select]

#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

robtillaart


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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

pjac82

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);

Khalid

do like this...
Code: [Select]
Serial.println(digitalRead (Relay_1));

pjac82


Khalid


robtillaart

Quote
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.

Code: [Select]

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() - http://arduino.cc/en/Reference/digitalRead -
There is a lot to learn there.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PaulS

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.

robtillaart

Quote
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.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

retrolefty

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


robtillaart

Lefty, and if one contact of the relay does work properly and the other not? I'm sure you never can be sure ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

retrolefty


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


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

Go Up