Concept Flow - Configuring pin as input or output
Start
Select Pin
Set Pin Mode
If Output
Yes→Configure as Output
Pin Ready
If Output
No→Configure as Input
Pin Ready
End
Choose a pin, decide if it is input or output, then configure it accordingly.
DDRB |= (1 << 2); // Set pin 2 as output DDRB &= ~(1 << 3); // Set pin 3 as input
| Step | Register | Operation | Bit | Resulting Register Value | Explanation |
|---|---|---|---|---|---|
| 1 | DDRB | DDRB |= (1 << 2) | 2 | 0b00000100 | Set bit 2 to 1 to configure pin 2 as output |
| 2 | DDRB | DDRB &= ~(1 << 3) | 3 | 0b00000100 | Clear bit 3 to 0 to configure pin 3 as input |
| 3 | - | - | - | - | Configuration complete, pins ready |
| Register | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| DDRB | 0b00000000 | 0b00000100 | 0b00000100 | 0b00000100 |
| PORTB | 0b00000000 | 0b00000000 | 0b00000000 | 0b00000000 |
Configuring pin direction: - Use DDRx register (Data Direction Register) - Set bit to 1 (DDRx |= 1<<pin) for output - Clear bit to 0 (DDRx &= ~(1<<pin)) for input - Use PORTx to set output level or enable pull-up - Always modify bits carefully to avoid changing other pins