PinRead() Question re Speed

I just had a general question about how fast does it take (approximately) for a sketch to read a pin value?

If I had 20 pinRead() statements in my Loop, do they all get read pretty fast or does it take some time to read each one?

I'm trying to get a feel for when I should use other methods to read a lot of pin values in my main Loop.

Thanks...

By "pinRead()" do you mean "digitalRead()"? I read that digitalRead() takes about 56 instruction cycles. The Arduino UNO (and most other Arduinos) do 16 instruction cycles per microsecond so that would take 3.5 microseconds (about 286000 reads per second). You can read 20 pins about 14000 times per second.

If you need faster reads you can use direct port manipulation.

Wow...that is fast! I had no idea it could read pins that fast. So 20 pin reads in the main loop is fast enough to keep them in the main loop and not have to use interrupts. Thanks...

johnwasser:
By "pinRead()" do you mean "digitalRead()"? I read that digitalRead() takes about 56 instruction cycles.

Rather, it is between 60 and 80 (PWM capable output) clock cycles.

There are two other faster solutions:

  1. digitalReadFast () functions which take about 30 clock cycles.

  2. Direct reading of PINX register (X = A, B, C, D: see datasheet microcontroler) that take only 8 clock cycles.
    With 16 MHz Xtal one clock cycle = 62.5 ns