Pull-up and pull-down resistor configuration in Embedded C - Time & Space Complexity
When configuring pull-up or pull-down resistors in embedded C, we often write code to set pin modes and states.
We want to understand how the time it takes to run this code changes as we configure more pins.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
if (config[i] == 1) {
// Configure pin with pull-up resistor
setPinMode(i, INPUT_PULLUP);
} else {
// Configure pin with pull-down resistor
setPinMode(i, INPUT_PULLDOWN);
}
}
This code sets the resistor configuration for n pins, choosing pull-up or pull-down based on an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each pin to configure its resistor.
- How many times: Exactly n times, once per pin.
Each pin requires one configuration step, so the total work grows directly with the number of pins.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 configuration calls |
| 100 | 100 configuration calls |
| 1000 | 1000 configuration calls |
Pattern observation: Doubling the number of pins doubles the work needed.
Time Complexity: O(n)
This means the time to configure resistors grows in direct proportion to the number of pins.
[X] Wrong: "Configuring multiple pins with pull-up or pull-down resistors happens instantly regardless of how many pins there are."
[OK] Correct: Each pin requires a separate configuration step, so more pins mean more time spent.
Understanding how configuration loops scale helps you write efficient embedded code and explain your reasoning clearly in interviews.
"What if we batch configure pins in groups instead of one by one? How would the time complexity change?"
Practice
Solution
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.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.Final Answer:
To set the input pin to a default HIGH voltage level -> Option BQuick Check:
Pull-up resistor = default HIGH [OK]
- Confusing pull-up with pull-down resistor function
- Thinking pull-up increases voltage beyond supply
- Assuming pull-up disconnects the pin
Solution
Step 1: Identify pin direction for input
To use pull-up, the pin must be configured as input to read signals.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.Final Answer:
Configure the pin as input and set the pull-up bit in the port register -> Option DQuick Check:
Input + pull-up bit set = internal pull-up enabled [OK]
- Setting pin as output instead of input
- Confusing pull-up bit with pull-down bit
- Not enabling pull-up resistor in code
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?Solution
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.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.Final Answer:
The pin will read HIGH because pull-up resistor sets default HIGH -> Option CQuick Check:
Pull-up enabled + floating pin = reads HIGH [OK]
- Assuming floating input reads LOW
- Thinking floating input causes short circuit
- Ignoring pull-up resistor effect
DDRD &= ~(1 << 5); // Set pin 5 as input PORTD |= (1 << 5); // Enable pull-down resistor on pin 5What is the error in this code?
Solution
Step 1: Review pull-up/down enabling method
Setting PORTD bit HIGH with pin as input enables internal pull-up resistor, not pull-down.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.Final Answer:
Pull-down resistors are not enabled by setting PORTD bit HIGH; this enables pull-up instead -> Option AQuick Check:
PORT bit HIGH + input = pull-up, not pull-down [OK]
- Assuming internal pull-down resistor exists
- Confusing pull-up and pull-down enabling bits
- Setting pin as output by mistake
Solution
Step 1: Understand internal pull-up limitation
Since only pull-up resistors are available internally, enable pull-up to default pin HIGH.Step 2: Wire button to ground
Connect button to ground so pressing it pulls pin LOW, overriding pull-up and avoiding floating.Step 3: Confirm logic
Pin reads HIGH when button not pressed (due to pull-up), LOW when pressed (button connects to ground).Final Answer:
Configure pin 3 as input with internal pull-up enabled; connect button to ground to pull pin LOW when pressed -> Option AQuick Check:
Pull-up enabled + button to ground = default HIGH, pressed LOW [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
