I have a project that requires having SPST relays with an extremely small contact time (sorry I don't know the word for it, um the time from when the coil is energized to when the contacts make....contact, if anyone can understand that) I want to be able to test a bunch of relays and find the one with the smallest time. I always run into trouble with the code, but this a basic process I think it should take.
The circuit could look like this:
relay coil connected to pin 9 (OUTPUT)
5 volts to one side of the switch part, with a pull down resistor to ground
pin 8 on the other side of the switch part (INPUT)
Then I was thinking the code would look something like this:
write the relay coil HIGH
when the relay coil == HIGH start a timer
when pin 8 == HIGH stop the timer
when pin 8 == HIGH delay(250) then write pin 9 as LOW
serial.print(time)
if someone could help me with this code it would be much appreciated.
If your coil needs 9V, then you need a stronger transistor to drive it, the arduino output will fail with 9v on it.
And you need a diode across the coil to keep from damaging the transistor. See the right side of this picture.
Do I understand that you want to measure the time from when you energize the coil to when you see the contacts close?
What's your coil resistance? The arduino output is not current limited, it will smoke itself if you let it put out too much current.
For code, you might try this, along with declaring variables, etc:
void loop(){
digitalWrite(outputPin, HIGH);
startTime = micros(); // startTime and endTime are type unsiged long,
while (digitalRead (inputPin) == 0){ // keep capturing time until the contacts close -
endTime = micros(); // time saved will be right before the contact closed
}
// or put it here:
// endTime = micros(); // for the time read after the close.
digitalWrite(outputPin, LOW);
serial.print ((endTime-startTime), DEC); // result will be microseconds
delay(1000); // repeats once a second
}