What is the fastest way to read a digital pin status in an Arduino Due? Is it possible to achieve speed in the Nanosecond area?

With which type of code I can achieve this?

Edit: This question is not a duplicate please see my comment.

  • 1
    Yes, write to or read from the correct GPIO / PORT registers directly. Best case this needs only a few CPU cycles to read, i.e. a few nanoseconds. The processing time after that unknown, dependent on what you want to do with the read out data. Duplicate of arduino.stackexchange.com/questions/9117/…. Also read forum.arduino.cc/index.php?topic=260731.0 . Mar 6, 2017 at 12:27
  • 1
    Possible duplicate of SAM3X8E (Arduino Due) Pin IO registers Mar 6, 2017 at 19:31
  • 1
    No it is not a duplicate. There you will find the answer how to set a digital pin as an output, but NOT how to set it as in input and read its pin state. Or atleast it is not explained detailed enough. Also you will not find the appropiate answer to the question "the fastest" but only sadly an alternative to the digitalWrite. You will not find any type of code example to set a pin as an input and to read from it, which actually for beginners like me would be very vital, if you would not forget that espiacially the "unnormal" register commands are very cryptic for a beginner. Mar 6, 2017 at 19:50
  • ns? no. us? yeah.
    – dandavis
    Mar 6, 2017 at 20:39
  • The OneWire library uses it: github.com/PaulStoffregen/OneWire/blob/master/OneWire.h
    – Jot
    Mar 6, 2017 at 22:59

2 Answers 2


If the pin is declared as input, let's say pin 13:

uint32_t pin_status;
pin_status = PIOB->PIO_PDSR & PIO_PDSR_P27;

If the pin is declared as output, let's say pin 13:

uint32_t pin_status;
pin_status = PIOB->PIO_ODSR & PIO_ODSR_P27;

Read the In out data register would be the fastest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.