How to calculate the operating time of instantaneous relay

I have used Arduino mega board. I have connected the circuit as shown, i.e. NO and NC of relay is connected to digital pin 2 and 3 of Arduino and common to 5V of arduino. By manually switching on and off the power supply we are checking the operating and release time. Instead of programmable power supply I am using a normal power supply.


The relay that I am using is shown below:


The code I am using is not giving correct result. I should get 17 ms but I am getting 6 ms. Please hep me with a corrected code and please check the connection if it is correct.

const int normallyClosedPin = 2; 

const int normallyOpenPin = 3; 

volatile unsigned long startTime = 0; 

volatile unsigned long endTime = 0; 

volatile bool startMeasured = false; 

void setup() { 

 Serial.begin(9600); 

 pinMode(normallyClosedPin, INPUT_PULLUP); 

 pinMode(normallyOpenPin, INPUT_PULLUP); 

 attachInterrupt(digitalPinToInterrupt(normallyClosedPin), ncToNoBegin, FALLING); 

 attachInterrupt(digitalPinToInterrupt(normallyOpenPin), noToNcEnd, RISING); 

} 

 

void loop() { 

 if(endTime>startTime){ 

 unsigned long operatingTime = endTime - startTime; 

 Serial.print("The operating time of the relay is:"); 

 Serial.print(operatingTime); 

 Serial.println("milliseconds"); 

 

 startTime = 0; 

 endTime = 0; 

 startMeasured = false; 

 delay(1000); 

 } 

} 

void ncToNoBegin() { 

 if(!startMeasured) { 

 startTime = millis(); 

 startMeasured = true; 

 } 

} 

void noToNcEnd() { 

 if(startMeasured) { 

  endTime = millis(); 

 } 

} 

Are you accounting for the response time of the programmable power supply?

You could eliminate any significant delay by using a MosFet to turn on the relay coil.

Your topic does not seem to indicate a problem with the IDE and hence has been moved to a more suitable location on the forum.

I am now using a normal power supply instead of programmable power supply.

What are the results with the normal supply.

How are you controlling the "normal" power supply? If you are turning it on (from being off) that will require the power supply to come up to voltage, could take a while.

I have connected a switch to the power supply. By manually turning on and off the switch I am calculating it.

Is the switch on the input or the output of the power supply? It should be on the output.

How do you sense the switch closure?


This is he connection. When I on the power supply the relay switches and so operating time can be seen. Again by switching it off release time can be seen.

How do you know it should be 16 ms?
From the datasheet?
The number in the datasheet may very well be a max number. So 99.9% of the relays of this type will be faster....

Yes, it is in the datasheet. Also I measured it with oscilloscope to verify.

What did the oscilloscope tell?

I am not sure, but millis depends on interrupts and therefore should not be used inside interrupts...
Better use micros().
Even better would be to set a flag...
...and have micros read in loop after the flag was raised...

I have tried by using micros() also. Still I am not getting the result.

16ms

No, it can be used inside interrupts. But it's value will never change inside an interrupt. That's not really a problem, because an interrupt routine should always be as short as possible.

There is nothing incorrect with faster turn-over time. You get the first contact after 6 ms, most probably followed by bouncing. It may take 17 ms from turning relay power off until the NO contact settles after bouncing.

If you want to measure the turn-ON time you should take the relay power ON as the start time and the NO contact closing as the end time. Continue taking the end time whenever the NO contact bounces to OFF again..

Also add a flyback diode across the relay coil!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.