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.
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.
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.
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.
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
edit
I probably should have mentiond that in the code, Relay is pin 12 and Relay_State is Pin 6
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?
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.
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.