Bird
Raised Fist0
Embedded Cprogramming~20 mins

Pull-up and pull-down resistor configuration in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pull-up and Pull-down Mastery
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 embedded C code configuring a pull-up resistor?

Consider the following code snippet configuring a microcontroller pin with a pull-up resistor enabled. What will be the value read from the pin if the external switch is open?

Embedded C
#define PIN_INPUT 0x01
#define PULLUP_ENABLE 0x02

unsigned char port_config = 0;
unsigned char pin_state = 0;

// Enable pull-up resistor on the pin
port_config |= PULLUP_ENABLE;

// Simulate reading the pin state
pin_state = (port_config & PULLUP_ENABLE) ? 1 : 0;

printf("Pin state: %d\n", pin_state);
APin state: 2
BPin state: 0
CPin state: 1
DCompilation error
Attempts:
2 left
💡 Hint

Think about what enabling a pull-up resistor does to the pin's voltage level when the switch is open.

❓ Predict Output
intermediate
2:00remaining
What is the pin state when a pull-down resistor is configured and the switch is open?

Given this embedded C code snippet that configures a pull-down resistor on a pin, what will be the output when the external switch is open?

Embedded C
#define PULLDOWN_ENABLE 0x04
unsigned char port_config = 0;
unsigned char pin_state = 0;

// Enable pull-down resistor on the pin
port_config |= PULLDOWN_ENABLE;

// Simulate reading the pin state
pin_state = (port_config & PULLDOWN_ENABLE) ? 0 : 1;

printf("Pin state: %d\n", pin_state);
ARuntime error
BPin state: 0
CPin state: 4
DPin state: 1
Attempts:
2 left
💡 Hint

Recall what a pull-down resistor does to the pin voltage when the switch is open.

🧠 Conceptual
advanced
1:30remaining
Which statement correctly describes the behavior of pull-up and pull-down resistors?

Choose the correct statement about pull-up and pull-down resistors in microcontroller input pin configurations.

APull-up resistors connect the pin to ground, making the default state low when the switch is open.
BPull-down resistors disconnect the pin from any voltage, leaving it floating when the switch is open.
CPull-down resistors connect the pin to the supply voltage, making the default state high when the switch is open.
DPull-up resistors connect the pin to the supply voltage, making the default state high when the switch is open.
Attempts:
2 left
💡 Hint

Think about which resistor connects to supply voltage and which connects to ground.

🔧 Debug
advanced
2:00remaining
Identify the error in this pull-up resistor configuration code

Examine the following embedded C code snippet intended to enable a pull-up resistor on a pin. What is the error that will prevent it from working correctly?

Embedded C
unsigned char port_config = 0;
#define PULLUP_ENABLE 0x01

// Incorrectly clearing the pull-up bit instead of setting it
port_config &= ~PULLUP_ENABLE;

// Reading pin state
unsigned char pin_state = (port_config & PULLUP_ENABLE) ? 1 : 0;

printf("Pin state: %d\n", pin_state);
AThe code clears the pull-up bit instead of setting it, so the pull-up resistor is disabled.
BThe code uses the wrong bit mask for the pull-up resistor, causing a runtime error.
CThe pin_state variable is not initialized before use, causing undefined behavior.
DThe printf statement is missing a newline character, so output is not visible.
Attempts:
2 left
💡 Hint

Look carefully at the operation used to modify the port_config variable.

📝 Syntax
expert
1:30remaining
Which option will cause a syntax error in configuring pull-up resistor in embedded C?

Identify which code snippet will cause a syntax error when trying to enable a pull-up resistor on a pin.

Aport_config =| PULLUP_ENABLE;
Bport_config = port_config | PULLUP_ENABLE;
Cport_config |= PULLUP_ENABLE
Dport_config |= PULLUP_ENABLE;
Attempts:
2 left
💡 Hint

Check the operator syntax carefully.

Practice

(1/5)
1. What is the main purpose of a pull-up resistor in embedded C microcontroller input pins?
easy
A. To set the input pin to a default LOW voltage level
B. To set the input pin to a default HIGH voltage level
C. To increase the input pin's voltage beyond supply voltage
D. To disconnect the input pin from the circuit

Solution

  1. Step 1: Understand pull-up resistor function

    A pull-up resistor connects the input pin to a positive voltage (usually Vcc) to ensure it reads HIGH when not driven.
  2. Step 2: Compare pull-up and pull-down roles

    Pull-down resistors connect to ground to set LOW by default, so pull-up sets HIGH by default.
  3. Final Answer:

    To set the input pin to a default HIGH voltage level -> Option B
  4. Quick Check:

    Pull-up resistor = default HIGH [OK]
Hint: Pull-up means default HIGH, pull-down means default LOW [OK]
Common Mistakes:
  • Confusing pull-up with pull-down resistor function
  • Thinking pull-up increases voltage beyond supply
  • Assuming pull-up disconnects the pin
2. Which of the following is the correct way to enable an internal pull-up resistor on a microcontroller input pin in embedded C?
easy
A. Set the pin as output and write HIGH
B. Configure the pin as input and set the pull-down bit in the port register
C. Set the pin as output and write LOW
D. Configure the pin as input and set the pull-up bit in the port register

Solution

  1. Step 1: Identify pin direction for input

    To use pull-up, the pin must be configured as input to read signals.
  2. Step 2: Enable internal pull-up resistor

    Setting the pull-up bit in the port register activates the internal pull-up resistor for that input pin.
  3. Final Answer:

    Configure the pin as input and set the pull-up bit in the port register -> Option D
  4. Quick Check:

    Input + pull-up bit set = internal pull-up enabled [OK]
Hint: Input pin + pull-up bit = pull-up enabled [OK]
Common Mistakes:
  • Setting pin as output instead of input
  • Confusing pull-up bit with pull-down bit
  • Not enabling pull-up resistor in code
3. Consider the following embedded C code snippet configuring a microcontroller pin:
DDRB &= ~(1 << 2);  // Set pin 2 of PORTB as input
PORTB |= (1 << 2);  // Enable pull-up resistor on pin 2

if (PINB & (1 << 2)) {
    // Pin reads HIGH
} else {
    // Pin reads LOW
}
What will be the output if the pin is not connected to anything externally?
medium
A. The pin will read LOW due to floating input
B. The pin will read an undefined random value
C. The pin will read HIGH because pull-up resistor sets default HIGH
D. The pin will cause a short circuit

Solution

  1. Step 1: Analyze pin configuration

    Pin 2 is set as input and internal pull-up resistor is enabled, connecting it internally to Vcc through resistor.
  2. Step 2: Determine pin reading when floating

    Since no external connection pulls it LOW, the pull-up resistor keeps the pin voltage HIGH, so the condition reads true.
  3. Final Answer:

    The pin will read HIGH because pull-up resistor sets default HIGH -> Option C
  4. Quick Check:

    Pull-up enabled + floating pin = reads HIGH [OK]
Hint: Pull-up resistor keeps floating input HIGH [OK]
Common Mistakes:
  • Assuming floating input reads LOW
  • Thinking floating input causes short circuit
  • Ignoring pull-up resistor effect
4. The following code is intended to enable a pull-down resistor on pin 5 of PORTD, but it does not work as expected:
DDRD &= ~(1 << 5);  // Set pin 5 as input
PORTD |= (1 << 5);  // Enable pull-down resistor on pin 5
What is the error in this code?
medium
A. Pull-down resistors are not enabled by setting PORTD bit HIGH; this enables pull-up instead
B. Pin 5 is set as output instead of input
C. The bit shift operator is used incorrectly
D. Pull-down resistors are enabled by clearing the PORTD bit, not setting it

Solution

  1. Step 1: Review pull-up/down enabling method

    Setting PORTD bit HIGH with pin as input enables internal pull-up resistor, not pull-down.
  2. Step 2: Understand pull-down resistor hardware

    Most microcontrollers do not have internal pull-down resistors; external resistors are needed, so setting PORTD bit cannot enable pull-down.
  3. Final Answer:

    Pull-down resistors are not enabled by setting PORTD bit HIGH; this enables pull-up instead -> Option A
  4. Quick Check:

    PORT bit HIGH + input = pull-up, not pull-down [OK]
Hint: Setting PORT bit HIGH enables pull-up, not pull-down [OK]
Common Mistakes:
  • Assuming internal pull-down resistor exists
  • Confusing pull-up and pull-down enabling bits
  • Setting pin as output by mistake
5. You want to read a button input on pin 3 of PORTC with a default HIGH state to avoid floating. The microcontroller only supports internal pull-up resistors. Which is the best way to configure the pin and button wiring to achieve this?
hard
A. Configure pin 3 as input with internal pull-up enabled; connect button to ground to pull pin LOW when pressed
B. Configure pin 3 as input without pull-up; connect button to Vcc to pull pin HIGH when pressed
C. Configure pin 3 as output and write LOW; connect button to Vcc
D. Configure pin 3 as input with internal pull-up enabled; connect button to Vcc

Solution

  1. Step 1: Understand internal pull-up limitation

    Since only pull-up resistors are available internally, enable pull-up to default pin HIGH.
  2. Step 2: Wire button to ground

    Connect button to ground so pressing it pulls pin LOW, overriding pull-up and avoiding floating.
  3. Step 3: Confirm logic

    Pin reads HIGH when button not pressed (due to pull-up), LOW when pressed (button connects to ground).
  4. Final Answer:

    Configure pin 3 as input with internal pull-up enabled; connect button to ground to pull pin LOW when pressed -> Option A
  5. Quick Check:

    Pull-up enabled + button to ground = default HIGH, pressed LOW [OK]
Hint: Pull-up + button to ground = default HIGH [OK]
Common Mistakes:
  • Trying to enable internal pull-down resistor which doesn't exist
  • Connecting button to Vcc with pull-up enabled causing always HIGH
  • Setting pin as output instead of input
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