Speed of digital input reading / writing

Greetings everyone

I am currently building a custom based linux distribution designed to have low latencies (xenomai).
In order to measure the latencies I thought about using an Arduino and would like some advices.

What I want the arduino to do :

   - measure time t0
   - Sending an interruption through the parallel port (setting an output pin UP)
   - waiting for an imput pin to go UP
   - Measure reaction_time = t0 - current_time
   - Store worst reaction time
   - Sart over again

What I want the PC to do

   - react on the IrQ of the parallel port
   - set an output parallel pin to UP

I'm a novice with arduino (I don't even have one) and before buying one I would like to have some info :

  • What is the speed of an input pin reading i.e what would be the result of this little pseudoprogram ?
   - t0 = get_time
   - read a pin
   - t_read = t0 - get_time
   - print (t_read)
  • Wich model of arduino would you advise me to buy knowing that I want to make as little of electronics circuitry as possible while having a reasonable speed (in the order of the microsecond for t_read)

I really appreciate any help you could provide and will provide any additional info if needed

Regards

And while I'm at it I would also like to know the time it takes to write to an PIN :slight_smile:

Read and write a pin:
// read PortD, bit 2 and write it to bit 3.
PORTD = 0b00001000 | (PIND & 0b00000100);

So, maybe 4 clock cycles at 62.5nS/clock cycle?

If you want to react to an interrupt, it takes about 4-8 microseconds, as documented here:

The most common Arduino board is the Arduino Uno.
The high end is the Arduino Due, it is faster with a ARM processor, but I would not recommend it to start with.

With the Arduino Uno, and using the normal Arduino functions, a loop that reads the interval and stores the slowest value uses about 5 to 20 µs.

For timing is a function called micros()

I read on that page that the resolution is 4µs.

Or millis()

There is a special function to measure the duration of a pulse.

The Arduino has hardware timers inside. If you use those, and optimize the code, you can go beyond the normal Arduino functions.

Read or write to I/O pins (with optimized code) is 0.125 µs

(while I was typing this, CrossRoads was already showing optimized code).

Thanks for the support CrossRoads and Peter_n

If I understood correctly, in my case the best course of action would be :

  • Using direct port writing / reading instead of digitalWrite(ledPin, HIGH); / digitalRead as CrossRoads showed
  • Use the hardware timers for fine time management (and checking it doesn't overflow) as Peter_n showed

Thanks for your help !

That's what I do.
Time management I either use micros(), which has about 4uS resolution, or just free running with no time management, depending on the application.