Bird
Raised Fist0
Arduinoprogramming~10 mins

Button reading with pull-up resistor in Arduino - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the button pin as input with pull-up resistor.

Arduino
pinMode(2, [1]);
Drag options to blanks, or click blank then click option'
AINPUT
BINPUT_PULLUP
COUTPUT
DHIGH
Attempts:
3 left
💡 Hint
Common Mistakes
Using OUTPUT instead of INPUT_PULLUP
Using INPUT without enabling pull-up resistor
2fill in blank
medium

Complete the code to read the button state from pin 2.

Arduino
int buttonState = digitalRead([1]);
Drag options to blanks, or click blank then click option'
A2
B3
C13
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Reading from the wrong pin number
Using analogRead instead of digitalRead
3fill in blank
hard

Fix the error in the if statement to check if the button is pressed (active LOW).

Arduino
if (buttonState == [1]) {
  // button pressed
}
Drag options to blanks, or click blank then click option'
ALOW
B1
C0
DHIGH
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for HIGH instead of LOW
Using numeric 1 or 0 instead of HIGH or LOW
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps button pins to their states if pressed.

Arduino
auto pressedButtons = {pin: digitalRead(pin) [1] pin in pins if digitalRead(pin) [2] LOW};
Drag options to blanks, or click blank then click option'
Afor
B==
C!=
Dwhile
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == to check pressed state
Using while instead of for in comprehension
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores pin numbers as keys and their pressed status as boolean values.

Arduino
auto buttonStatus = { [1]: digitalRead([2]) [3] LOW for [2] in pins };
Drag options to blanks, or click blank then click option'
Apin
B==
C!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == for pressed check
Using different variable names inconsistently

Practice

(1/5)
1. What does setting pinMode(pin, INPUT_PULLUP) do in Arduino when reading a button?
easy
A. Enables the internal pull-up resistor to keep the pin HIGH when button is not pressed
B. Disables the pin to save power
C. Sets the pin as an output to drive an LED
D. Connects the pin directly to ground

Solution

  1. Step 1: Understand pinMode with INPUT_PULLUP

    Using INPUT_PULLUP activates the internal pull-up resistor on the pin, so it reads HIGH by default.
  2. Step 2: Effect on button reading

    When the button is pressed, it connects the pin to ground, making the reading LOW. When not pressed, the pull-up resistor keeps it HIGH.
  3. Final Answer:

    Enables the internal pull-up resistor to keep the pin HIGH when button is not pressed -> Option A
  4. Quick Check:

    INPUT_PULLUP means pin reads HIGH unless grounded [OK]
Hint: INPUT_PULLUP means pin is HIGH until button grounds it [OK]
Common Mistakes:
  • Thinking INPUT_PULLUP sets pin as output
  • Assuming pin reads LOW when button is not pressed
  • Confusing pull-up with pull-down resistor
2. Which of the following is the correct syntax to set pin 7 as input with internal pull-up resistor in Arduino?
easy
A. pinMode(7, INPUT_PULLDOWN);
B. pinMode(7, OUTPUT_PULLUP);
C. pinMode(7, INPUT_PULLUP);
D. pinMode(7, INPUT); digitalWrite(7, LOW);

Solution

  1. Step 1: Recall correct pinMode usage

    The correct way to enable internal pull-up resistor is pinMode(pin, INPUT_PULLUP);.
  2. Step 2: Check each option

    pinMode(7, OUTPUT_PULLUP); uses OUTPUT_PULLUP which does not exist. pinMode(7, INPUT_PULLDOWN); uses INPUT_PULLDOWN which Arduino does not support internally. pinMode(7, INPUT); digitalWrite(7, LOW); sets pin as INPUT but digitalWrite LOW disables pull-up (equivalent to plain INPUT, floating pin), does not enable internal pull-up resistor.
  3. Final Answer:

    pinMode(7, INPUT_PULLUP); -> Option C
  4. Quick Check:

    Use INPUT_PULLUP to enable pull-up resistor [OK]
Hint: Use INPUT_PULLUP exactly to enable pull-up resistor [OK]
Common Mistakes:
  • Using OUTPUT_PULLUP which is invalid
  • Trying INPUT_PULLDOWN which Arduino lacks
  • digitalWrite(7, LOW) after INPUT (floating input, no pull-up)
3. What will be the output on the Serial Monitor when the button connected to pin 4 is pressed, given this code?
void setup() {
  pinMode(4, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int state = digitalRead(4);
  Serial.println(state);
  delay(500);
}
medium
A. Prints 1 when button pressed, 0 when released
B. Always prints 1 regardless of button state
C. Always prints 0 regardless of button state
D. Prints 0 when button pressed, 1 when released

Solution

  1. Step 1: Understand INPUT_PULLUP behavior

    With INPUT_PULLUP, the pin reads HIGH (1) when button is not pressed and LOW (0) when pressed because button connects pin to ground.
  2. Step 2: Analyze Serial output

    The code prints the pin state every 500ms. When pressed, it prints 0; when released, it prints 1.
  3. Final Answer:

    Prints 0 when button pressed, 1 when released -> Option D
  4. Quick Check:

    Pressed = LOW (0), Released = HIGH (1) [OK]
Hint: Pressed button reads LOW (0) with pull-up resistor [OK]
Common Mistakes:
  • Assuming pressed reads HIGH (1)
  • Confusing pull-up with pull-down logic
  • Ignoring that button grounds the pin when pressed
4. The following code is intended to read a button with internal pull-up resistor on pin 2, but it does not work correctly. What is the error?
void setup() {
  pinMode(2, INPUT);
  Serial.begin(9600);
}

void loop() {
  int val = digitalRead(2);
  Serial.println(val);
  delay(200);
}
medium
A. Missing INPUT_PULLUP mode to enable pull-up resistor
B. Serial.begin should be in loop, not setup
C. digitalRead cannot be used on pin 2
D. delay(200) is too short for button reading

Solution

  1. Step 1: Check pinMode configuration

    The code uses pinMode(2, INPUT); which does not enable the internal pull-up resistor, so the pin may float and give unreliable readings.
  2. Step 2: Correct usage for button with pull-up

    To use the internal pull-up resistor, the pinMode should be INPUT_PULLUP. This prevents floating and ensures stable readings.
  3. Final Answer:

    Missing INPUT_PULLUP mode to enable pull-up resistor -> Option A
  4. Quick Check:

    Use INPUT_PULLUP to avoid floating input [OK]
Hint: Always use INPUT_PULLUP for button input to avoid floating [OK]
Common Mistakes:
  • Using INPUT without pull-up resistor
  • Moving Serial.begin to loop unnecessarily
  • Thinking delay affects button reading correctness
5. You want to detect a button press using pin 8 with internal pull-up resistor. Which code snippet correctly reads the button and turns on an LED on pin 13 only when the button is pressed?
hard
A. pinMode(8, INPUT_PULLUP); pinMode(13, OUTPUT); if(digitalRead(8) == HIGH) digitalWrite(13, HIGH); else digitalWrite(13, LOW);
B. pinMode(8, INPUT_PULLUP); pinMode(13, OUTPUT); if(digitalRead(8) == LOW) digitalWrite(13, HIGH); else digitalWrite(13, LOW);
C. pinMode(8, INPUT); pinMode(13, OUTPUT); if(digitalRead(8) == LOW) digitalWrite(13, HIGH); else digitalWrite(13, LOW);
D. pinMode(8, OUTPUT); pinMode(13, INPUT_PULLUP); if(digitalRead(8) == LOW) digitalWrite(13, HIGH); else digitalWrite(13, LOW);

Solution

  1. Step 1: Set pin modes correctly

    Pin 8 must be input with internal pull-up resistor: INPUT_PULLUP. Pin 13 is output for LED.
  2. Step 2: Understand button logic with pull-up

    Button press connects pin 8 to ground, so digitalRead(8) returns LOW when pressed.
  3. Step 3: Write correct if condition

    Turn LED on when button is pressed (pin reads LOW), so condition is if(digitalRead(8) == LOW).
  4. Final Answer:

    pinMode(8, INPUT_PULLUP); pinMode(13, OUTPUT); if(digitalRead(8) == LOW) digitalWrite(13, HIGH); else digitalWrite(13, LOW); -> Option B
  5. Quick Check:

    Pressed = LOW, LED ON when LOW [OK]
Hint: Button pressed reads LOW; turn LED ON when reading is LOW [OK]
Common Mistakes:
  • Checking for HIGH instead of LOW on button press
  • Setting button pin as OUTPUT
  • Not enabling INPUT_PULLUP resistor