Software vs Hardware speed

I am using Pin 12 to activate a relay coil. The relay is DPDT and I am useing one side of the relay to connect pin 6 to ground as a positive feedback to the software that the relay is in fact closed.

In my function I have this;

    digitalWrite(Relay,iR_State);
    return digitalRead(Relay_State);

My question is, will the relay close fast enough for the return statment to return LOW when the relay closes. Or do you think that the return statment might happen before the relay closes. Can/should I add a delay between the digitalWrite and the digitalRead inside my function?

No, most relays will not activate fast enough to read a valid feedback signal in the very next statement. The actual speed depends on the size of the relay, smaller ones are faster because less mass to move. Also there is contact bounce that also requires time delay to settle out for a valid true state.

I would think a 20 millsec delay would be a good starting point and could be adjusted faster or slower by testing. I think there are standard contact denouncing functions available in the Arduino library.

Lefty

Thanks, just wanted to confirm my assumptions.

My question is, will the relay close fast enough for the return statment to return LOW when the relay closes.

It's been answered, but just for fun, let's assume you have 0.25us (microseconds) between the two Arduino statements executing and the relay has a 1mm gap to cross.

1mm in 0.25us = 4mm/us = 4,000,000mm/second = 4km/s = 14,400km/h.

So the relay contact would have to travel at an average speed of 14,400km/h (about 8,500 miles per hour) to close in time. Peak speed will be double that, which means the contacts hit each other at over 28,000km/h.

Anyone want to know what the acceleration needed for that is?

If one side of the relay is connecting a pin to ground (assuming the pin is pulled high when the relay is open), why not monitor the pin to determine if it is closed.

digitalWrite(Relay,iR_State);

while( digitalRead(sensePin) )  
        ; // wait for pin 6 to go low 
return true; // return true/false or HIGH/LOW depending on your logic

You may want to add some debounce logic into this.

Anyone want to know what the acceleration needed for that is?

sure why not.
While you are at it, calculate the current draw needed to pull the relay in that fast ;D

Relay specs;
http://www.components.omron.com/components/web/PDFLIB.nsf/0/DB3627D7D8BCAFF485257201007DD587/$file/G6H_0607.pdf

Not sure I understand it all, but it's interesting. :slight_smile:

If one side of the relay is connecting a pin to ground (assuming the pin is pulled high when the relay is open), why not monitor the pin to determine if it is closed.

That is exactly what I am doing with pin 6 :wink:

edit
I probably should have mentiond that in the code, Relay is pin 12 and Relay_State is Pin 6

Your code is returning the value (before it has closed) , I am suggesting you add the code to wait for the contacts to pull the signal to ground

Your code is returning the value (before it has closed) , I am suggesting you add the code to wait for the contacts to pull the signal to ground

I see, I changedthe code to;

digitalWrite(Relay,iR_State);
delay(DEBOUNCE_TIME);
return digitalRead(Relay_State);

where DEBOUNCE_TIME is set at 20ms

But I think I like your Idea better, but should I have a counter or something inside the loop incase the relay doesn't close? wouldn't the loop run for ever in that case?

sure why not.

That's why I love this community :).

0.25us to accelerate to 8km/s
= 1s to accelerate to 32 million km/s. (8 times 4 million)

So your acceleration is 32 billion m/s^2 (or about 300 million gravities).

As for current needed, the datasheet you gave says it needs 2ms to close and takes 11.70mA at 12 volts. Assuming the power needed to close it is constant, you'd need 93.6 amps at 12 volts (or 1,123 watts) to close the relay.

93.6 amps at 12 volts (or 1,123 watts) to close the relay.

I'm useing the 5 volt version, but still I don't think the Arduino is capable of producing 93 amps to close the relay fast enough ;D

should I have a counter or something inside the loop incase the relay doesn't close? wouldn't the loop run for ever in that case?

If you want a timeout you can do something like this:

#define Relay  12 
#define Relay_State 6
#define R_TIMEOUT 200   // max time to wait for relay to close in ms
#define R_DEBOUNCE 10   // time from initial contact closure to stable closure

int closeRelay(int iR_State ){
  digitalWrite(Relay,iR_State);

  for(int i=0; i < R_TIMEOUT; i++){
    if( digitalRead(Relay_State) == LOW )  {
      delay(R_DEBOUNCE);
      return true;  // relay closed and stable
    }
    delay(1);    
  }     
  return false; // relay did not close within timeout period
}

Note that there are two time periods being handled here -

The time it takes the relay coil to move the contacts - we set the max time as R_TIMEOUT

The debounce time - the time it takes for the contacts to stabilize from initial contact.

I hope that helps

Thanks mem