Pages: [1]   Go Down
Author Topic: read the status of an relay...is it ON or OFF  (Read 750 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 224
Posts: 13916
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

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

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

do like this...
Code:
Serial.println(digitalRead (Relay_1));
Logged


Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much!! It worked!!!  smiley
Logged

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the Karma smiley-mr-green
Logged


Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 224
Posts: 13916
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rob Tillaart

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50434
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 224
Posts: 13916
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 224
Posts: 13916
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lefty, and if one contact of the relay does work properly and the other not? I'm sure you never can be sure smiley-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
Logged

Pages: [1]   Go Up
Jump to: