# AVR. How to configure as output just one pin?

In AVR C code, if you want to declare some port as ouput, you just have to to do this:

DDRB = 0xFF;


But how can I do if I want to declare as ouput just one pin. For example, I want to declare has output Arduino's pin 13, which corresponds to PB5.

I know I can declare that in binary format:

DDRB = b11111111;


But, I don't know which bit position corresponds to PB5.

Thanks in advance, I could not find this answer in Google.

• "...could not find this answer in Google." Try the datasheet. Just set or clear the respective ddrx bits. – dannyf Mar 23 '17 at 15:35
• Set the fifth bit: DDRB = b00100000; – Dmitry Grigoryev Mar 23 '17 at 16:02

PB5 is the 6th bit.
Just remember that the numbering is zero-based and starts with the least-significant-bit on the right, so the value you're looking for in this case is 0b00100000.
When you're setting this bit, you'll probably want to use a logical OR operation instead of a simple assignment, so:

DDRB |= 0b00100000;


DDRB = 0b00100000;


That way you won't inadvertently clear any of the other bits which might already be set.

• The OR-assignment is particularly useful as it gets compiled down to one machine instruction, which only modifies that one bit. – Austin Mar 26 '17 at 15:33

But, I don't know which bit position corresponds to PB5.

You don't need to; the toolchain handles this for you.

DDRB |= _BV(PB5);


If there is a register then there is a datasheet that will tell you everything about the given register.

Description of DDRB register clearly shows that PB5 pin can be set to in or output by bit 5, which is the 6th bit.

If you want to set only this bit then the following syntax can be used.

DDRB |= (1<<5); // 5 for bit 5


To make it more readable you can use the PB5 macro which is just #define PB5 5 (already defined under the hood).

DDRB |= (1<<PB5);


And to clear only the specific bit:

DDRB &= ~(1<<PB5);