ARDUINO LED AND SWITCH INTERFACE USING REGISTERS



In this tutorial, we are going to interface LED and SWITCH using REGISTERS.

SOFTWARE:ARDUINO IDE

SIMULATION: PROTEUS

REGISTERS

DDRx

This register is used to set the PORT pins as Input or Output.

0 -Set the pin as Input

1 -Set the pin as Output

Example:

DDRD=0B00001111;

here, PORTD pins D0,D1,D2 and D3 as OUTPUT and pins D4,D5,D6,D7 are INPUT

PORTx

This register is used to read or write on that PORT

Example:

            PORTD=0B00000100;

Here PORTD 2 pin, (pin No 2 in Arduino -PD2) is set to 1 or HIGH.

PINx

This register is used to read the status of the PORT pins

Example:

(PIND>>2)&1;

Here, the 2nd pin of PORTD is checked whether it is set or cleared.

CODE:

void setup()

{

  DDRD = (1 << 2); // set 2nd pin as OUTPUT

  DDRD |= ~(1 << 3);//set 3rd pin as INPUT

}

void loop()

{

  int LED = ((PIND >> 3) & 1); //check the value of 3rd pin is SET

  if (LED == 1)     // If 3rd pin is SET 2nd pin become HIGH

    PORTD |= (1 << 2);

  else

    PORTD &= ~(1 << 2);

}


  • In this code ,PORTD 2 pin (PD2) is set as OUTPUT and PORTD 3 pin (PD3) is set as INPUT.
  • The integer variable LED is used to store the status of 3 pin of PORT D.
  • If 3 pin is set then the 2 pin of PORTD (PD2) is set to HIGH, else the 2 pin of PORTD(PD2) is cleared.
  • In this circuit pull down resistor is used to clear the voltage in the PIN 3.

Comments