Timing for digitalread()

Hi everyone, currently I'm working on a relay tester project. I would like to use Arduino mega 2560 for this. this tester work when one of the Digital pin gives high to the relay, so there will be contact and the relay gives high to another pin of Arduino. All this must be done in 2ms, any contact later than 2ms will be considered as relay faulty. Thank you

A digitalRead/digitalWrite takes ≈ 4us.

The execution time for the code before and after the digitalRead/digitWrite depends on what you are doing.

1 Like

ok sir, is there any previous forum for me to refer? I'm kind of out of idea on what I should do, thank you.

We need exact information from questioners before we can help.

Exactly what are you trying to do ?

1 Like

im going to do a 1 pole relay tester (mercury relay), so the relay has input and output as we know (normally open/normally close).. both input and output connected to the arduino ... so when the arduino gives high to the relay, the relay should gives back HIGH to arduino again within 2m..more thn 2ms, the relay is considered faulty..

Why ?

So you are wanting to time how long it takes the relay to operate ?

1 Like

no sir, i want the response in 2ms...if the respond is in 2ms, green led will light up(pass), else red led(fail)

For something like this the relay is slow compared to the Arduino.

Maybe if you draw a timing diagram showing the relationship between the digitalWrite to the digitalRead would help.

1 Like

Something like this sir

Ok so the goal is to time how long it takes the relay to activate.:face_with_raised_eyebrow:
But what is the problem?

Exactly, we haven't had a question yet.

But anyway, yes, this should be trivial to do. You can use the microsecond counter to see if the relay contacts close within 2000 microseconds of the relay being actuated.

Nobody here will write the code for you, unless you offer money. But if you make a start yourself, we can give you guidance.

Assume that D2 is OUTPUT and D3 is INPUT, when D2 gives 5V(HIGH) it will turn on the relay and there will be contact for D3, so D3 will have 5V input. If this event occured <= 2ms, D4( green led indicate PASS) will be HIGH, else D5 (red led indicate FAIL) will be HIGH.

Yes, yes, we've got it. What's the question?

1 Like

how to write the function for " if i get the respond within 2ms,its pass, else its fail"

Post the relay control code, using code tags, and forum members can suggest how to add that "if".

Right now, your schematic does not show how the relay is activated. Please add that information.

You don't have any pins going to the relay coil. There is no way for the Arduino to flip the relay. From the part number, it looks like the relay might have a 12V coil. To trigger that you will need a transistor driver.

You don't need two pins going to the relay contacts. Use an INPUT_PULLUP pin on one side of the contact and Ground on the other side. It may be useful to use Pin 2 or Pin 3 since they are the External Interrupt pins on the Arduino UNO. Just using the 'micros()' function will get you a timing precision of about 4 microseconds. If you want more precision you can use the Input Capture feature of the hardware for Timer1 which will get you a precision of 1/16th microsecond.

Forgive me, but it sounds like you haven't made much effort to find out for yourself. This forum is not a place to get your code written for you (unless you offer money). It's a place to get help when you are struggling.

The first thing you must do is work through some of the examples so that you learn the fundamentals of how to write Arduino code. There are absolutely loads of tutorials on the web, including YouTube. Also, you should work through all the examples in

File | Examples | 01.Basic

Look at the Blink example, and use it to "flash" your relay. Then try the Blink Without Delay and Button examples. Find out about how to use the micros() function.

Very soon you will have covered everything you need to make your first attempt at your relay response timer. Show us your efforts and we will guide you all the way.

1 Like

sorry sir, i did not upload the code

void setup() {
  pinMode(3,INPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
}
void loop()
{
  digitalWrite(4,HIGH);
  delay(2);
  if(digitalRead(3)==HIGH)
    digitalWrite(5,HIGH);
  else
    digitalWrite(6,HIGH);
}

Have you considered what happens next?