Skip to main content
Corrected bitwise operator for mask and explained _BV
Source Link
Nick Gammon
  • 38.7k
  • 13
  • 67
  • 125

Yes, you can use the data direction registers (DDRB, DDRC, DDRD depending on which port) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDR_DDRx will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out which DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168/ATMEGA328 (including Uno) here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD & _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB & _BV(5).

(_BV(x) is a macro that shifts 1 x bits to the right.)

Yes, you can use the data direction registers (DDRB, DDRC, DDRD depending on which port) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDR_ will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out which DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168/ATMEGA328 (including Uno) here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD & _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB & _BV(5).

(_BV(x) is a macro that shifts 1 x bits to the right.)

Yes, you can use the data direction registers (DDRB, DDRC, DDRD depending on which port) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDRx will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out which DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168/ATMEGA328 (including Uno) here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD & _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB & _BV(5).

(_BV(x) is a macro that shifts 1 x bits to the right.)

Corrected bitwise operator for mask and explained _BV
Source Link

Yes, you can use the data direction registers (DDRB, DDRC, DDRD depending on which port) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDRDDR_ will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out whatwhich DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168/ATMEGA328 (including Uno) here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD |& _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB |& _BV(5).

(_BV(x) is a macro that shifts 1 x bits to the right.)

Yes, you can use the data direction registers (DDR) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDR will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out what DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168 here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD | _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB | _BV(5).

Yes, you can use the data direction registers (DDRB, DDRC, DDRD depending on which port) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDR_ will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out which DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168/ATMEGA328 (including Uno) here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD & _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB & _BV(5).

(_BV(x) is a macro that shifts 1 x bits to the right.)

Source Link
bigjosh
  • 1.6k
  • 10
  • 13

Yes, you can use the data direction registers (DDR) to check what mode a pin is in.

If a pin is in output mode then the corresponding bit in DDR will be 1.

One complication is that the Arduino functions give each pin an "Arduino" pin number, and you have to look at a pin map to figure out what DDR register and bit corresponds to the pin you want to check. You can find a pin map for Arduinos based on the ATMEGA168 here...

https://www.arduino.cc/en/Hacking/Atmega168Hardware

So, for example, if you want to check if Arduino digital pin #4 is set for output, you'd look at the map and see that corresponds to PD4. That means "port D" and "bit 4", so you could say...

if ( DDRD | _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

If you want to check Arduino digital pin #14, then you would use DDRB | _BV(5).