How many clock cycles does digitalRead/Write take?

(1) The Experiment Setup of Section-3 using Arduino UNO R3 says:
//------------------------------------------------------------------------------------------------------------
(a) Execution time of digitalWrite(12, HIGH); instruction with 2.2k load/no-load at the Port-pin is: 59 clock cycles of 16 MHz clkSYS (59 x 1/16000000 = 3.6875 us).

(b) Execution time of digitalWrite(12, LOW); instruction with 2.2k load/no-load at the Port-pin is: 61 clock cycles of 16 MHz clkSYS (61 x 1/16000000 = 3.8125 us).

//-----------------------------------------------------------------------------------------------------------
(c) Execution time of digitalRead(9); instruction (with internal pull-up enabled) is:
50 clock cycles of 16 MHz clkSYS (50 x 1/16000000 = 3.125 us).

(2) Experiment Methodology:
(a) Just before the execution of the digitalWrite(12, HIGH/LOW); instruction, We have started Timer-1(T1) to count the clkSYS (16 MHz) pulses. After the execution of the digitalWrite(12, HIGH/LOW); instruction, we have stopped the Timer-1.

(b) Number of counts of Timer-1 must be equal to the time that digitalWrite(12, HIGH/LOW); has spent for its own execution.

(c) We have read the content of TCNT1, and we have displayed it on the LCD.

(d) Similarly, we have found the execution time of digitalRead(9) instruction.

(3) Experiment Setup using Arduino UNO R3

(4) Arduino Program Codes (Prog1.ino)

#include <LiquidCrystal.h>
//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(5, A0, A1, A2, A3, A4);


void  setup()
{
  lcd.begin(16, 2);
  pinMode(12, OUTPUT);
  pinMode(9, INPUT_PULLUP);
  //Timer-1 as a n Internal Pulse Counter----
  TCCR1A = 0x00;   //Normal UpCounting Mode
  TCCR1B = 0x00;  //T1 is OFF
  TCNT1 = 0x00;   //Initial Value
  
  TCCR1B = 0x01;  //T1 ON; running at 16 MHz
  digitalWrite(12, HIGH);
  //digitalWrite(12, LOW);
 
  //digitalRead(9);

  TCCR1B = 0x00;    //Timer-1 OFF
  lcd.setCursor(0, 0);  //cursor position
  lcd.print(TCNT1, 10); //show Counter-1's content
}

void loop()
{
    
}

Prog1.ino (634 Bytes)