Bird
Raised Fist0
Embedded Cprogramming~20 mins

Configuring pin as input or output in Embedded C - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pin Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this pin configuration code?
Consider a microcontroller where setting a bit in the DDR (Data Direction Register) configures the pin as output (1) or input (0). What will be the value of DDRB after running this code?
Embedded C
DDRB = 0x00;
DDRB |= (1 << 3);  // Set pin 3 as output
DDRB &= ~(1 << 1); // Set pin 1 as input
A0x0A
B0x08
C0x0C
D0x02
Attempts:
2 left
💡 Hint
Remember that (1 << 3) sets bit 3 and (1 << 1) sets bit 1. |= sets bits, &=~ clears bits.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly configures pin 5 as input?
Given a DDR register controlling pin directions, which code snippet correctly sets pin 5 as input without changing other pins?
ADDR &= ~(1 << 5);
BDDR = (1 << 5);
CDDR |= (1 << 5);
DDDR ^= (1 << 5);
Attempts:
2 left
💡 Hint
Input pins have DDR bit cleared (0). Use bitwise AND with NOT to clear a bit.
🔧 Debug
advanced
2:30remaining
Why does this code fail to configure pin 2 as output?
This code is intended to set pin 2 as output by setting bit 2 in DDR. Why does it not work as expected? DDRB = 0; DDRB & (1 << 2);
ADDRB is reset to 0 after setting the bit.
BThe bit shift is incorrect; it should be (1 << 3).
CThe code uses & instead of |=, so it does not set the bit.
DPin 2 cannot be configured as output on this microcontroller.
Attempts:
2 left
💡 Hint
Check the operator used to set bits.
📝 Syntax
advanced
1:30remaining
Which option contains a syntax error in configuring pin 0 as output?
Identify the code snippet that will cause a syntax error when trying to set pin 0 as output.
ADDRB |= (1 << 0);
BDDRB = DDRB | (1 << 0);
CDDRB |= 1 << 0;
DDDRB |= (1 << 0
Attempts:
2 left
💡 Hint
Look for missing parentheses or incomplete expressions.
🚀 Application
expert
3:00remaining
How many pins are configured as output after this code runs?
Given DDRC initially 0x00, what is the number of pins configured as output after running this code? DDRC |= (1 << 0); DDRC |= (1 << 2); DDRC &= ~(1 << 0); DDRC |= (1 << 7);
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Count bits set to 1 in DDRC after all operations.

Practice

(1/5)
1. Which register is used in embedded C to configure a microcontroller pin as input or output?
easy
A. PORT (Data Register)
B. TIMER Register
C. PIN (Input Register)
D. DDR (Data Direction Register)

Solution

  1. Step 1: Understand pin configuration registers

    The DDR register controls whether a pin is input or output by setting bits to 0 or 1.
  2. Step 2: Identify the correct register for direction

    PORT controls output values, PIN reads input values, TIMER is unrelated to pin direction.
  3. Final Answer:

    DDR (Data Direction Register) -> Option D
  4. Quick Check:

    DDR sets pin direction [OK]
Hint: Remember DDR sets direction: 1=output, 0=input [OK]
Common Mistakes:
  • Confusing PORT with DDR
  • Using PIN register to set direction
  • Thinking TIMER controls pin mode
2. Which of the following lines correctly sets pin 3 of PORTB as output in embedded C?
easy
A. DDRB |= (1 << 3);
B. DDRB &= ~(1 << 3);
C. PORTB |= (1 << 3);
D. PINB |= (1 << 3);

Solution

  1. Step 1: Understand setting pin as output

    To set a pin as output, set its bit in DDR register to 1 using bitwise OR.
  2. Step 2: Analyze each option

    DDRB |= (1 << 3); uses DDRB |= (1 << 3) which sets bit 3 to 1 correctly. DDRB &= ~(1 << 3); clears bit 3 (input). Options C and D affect PORTB and PINB, not direction.
  3. Final Answer:

    DDRB |= (1 << 3); -> Option A
  4. Quick Check:

    Use |= with DDR to set output [OK]
Hint: Use |= with DDR to set output bit [OK]
Common Mistakes:
  • Using &= ~ instead of |= to set output
  • Modifying PORT or PIN instead of DDR
  • Shifting wrong bit position
3. What is the effect of the following code snippet?
DDRC |= (1 << 2);  // Line 1
DDRC &= ~(1 << 2); // Line 2
medium
A. Pin 2 of PORTC is set as output then input
B. Pin 2 of PORTC remains output
C. Pin 2 of PORTC remains input
D. Syntax error in code

Solution

  1. Step 1: Analyze Line 1

    DDRC |= (1 << 2) sets bit 2 to 1, configuring pin 2 as output.
  2. Step 2: Analyze Line 2

    DDRC &= ~(1 << 2) clears bit 2 to 0, configuring pin 2 as input.
  3. Final Answer:

    Pin 2 of PORTC is set as output then input -> Option A
  4. Quick Check:

    Set then clear bit changes pin direction [OK]
Hint: |= sets output, &=~ clears to input [OK]
Common Mistakes:
  • Thinking both lines set output
  • Ignoring second line effect
  • Confusing PORTC with DDRC
4. Identify the error in this code that tries to set pin 5 of PORTD as input:
DDRD |= (0 << 5);
medium
A. DDRD cannot be modified directly
B. Bit shift is incorrect, should be (1 << 5)
C. Should use &= ~ instead of |= to set input
D. No error, code is correct

Solution

  1. Step 1: Understand setting pin as input

    To set a pin as input, clear its bit in DDR register using &= ~ (bitwise AND with NOT).
  2. Step 2: Analyze the code

    DDRD |= (0 << 5) does nothing because (0 << 5) is 0; it does not clear bit 5. Correct way is DDRD &= ~(1 << 5).
  3. Final Answer:

    Should use &= ~ instead of |= to set input -> Option C
  4. Quick Check:

    Use &= ~ to clear bit for input [OK]
Hint: Use &= ~ to clear bit for input, not |= [OK]
Common Mistakes:
  • Using |= with 0 does not clear bit
  • Confusing bitwise OR and AND
  • Not using bitwise NOT (~) when clearing
5. You want to configure pins 0, 1, and 2 of PORTA as outputs and pins 3 and 4 as inputs without changing other pins. Which code snippet correctly does this?
hard
A. DDRA |= (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
B. DDRA |= (1 << 0) | (1 << 1) | (1 << 2); DDRA &= ~((1 << 3) | (1 << 4));
C. DDRA |= (1 << 0) & (1 << 1) & (1 << 2); DDRA &= ~((1 << 3) & (1 << 4));
D. DDRA = (1 << 0) | (1 << 1) | (1 << 2); DDRA = ~((1 << 3) | (1 << 4));

Solution

  1. Step 1: Set pins 0,1,2 as outputs

    Use bitwise OR to set bits 0,1,2 without affecting others: DDRA |= (1<<0)|(1<<1)|(1<<2);
  2. Step 2: Set pins 3,4 as inputs

    Clear bits 3 and 4 using bitwise AND with NOT: DDRA &= ~((1<<3)|(1<<4));
  3. Final Answer:

    DDRA |= (1 << 0) | (1 << 1) | (1 << 2); DDRA &= ~((1 << 3) | (1 << 4)); -> Option B
  4. Quick Check:

    Use |= to set output bits, &=~ to clear input bits [OK]
Hint: Set outputs with |=, inputs with &= ~ combined masks [OK]
Common Mistakes:
  • Overwriting DDRA instead of modifying bits
  • Using & instead of | for combining bits
  • Setting input pins as output by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard