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
Recall & Review
beginner
What is the purpose of a pull-up resistor when reading a button in Arduino?
A pull-up resistor ensures the input pin reads a HIGH value when the button is not pressed, preventing it from floating and giving unreliable readings.
Click to reveal answer
beginner
How does the Arduino internal pull-up resistor help when connecting a button?
Arduino has built-in pull-up resistors that can be enabled in code, so you don't need an external resistor. This simplifies wiring and ensures stable HIGH readings when the button is open.
Click to reveal answer
beginner
What value does the Arduino digitalRead() function return when a button connected with a pull-up resistor is pressed?
It returns LOW (0) because pressing the button connects the pin to ground, overriding the pull-up resistor's HIGH state.
Click to reveal answer
beginner
Why should you avoid leaving an input pin 'floating' when reading a button?
A floating pin can pick up electrical noise and randomly switch between HIGH and LOW, causing unreliable button readings.
Click to reveal answer
beginner
Show the Arduino code snippet to enable the internal pull-up resistor on pin 2 and read the button state.
pinMode(2, INPUT_PULLUP);
int buttonState = digitalRead(2);
Click to reveal answer
What does enabling INPUT_PULLUP on a pin do?
AConnects the pin internally to ground
BDisables the pin
CConnects the pin internally to 5V through a resistor
DMakes the pin an output
✗ Incorrect
INPUT_PULLUP connects the pin internally to 5V through a resistor, acting as a pull-up resistor.
When a button connected with a pull-up resistor is pressed, what value does digitalRead() return?
ALOW
BIt depends on the button
CHIGH
DAnalog value
✗ Incorrect
Pressing the button connects the pin to ground, so digitalRead() returns LOW.
Why is it bad to leave a button input pin floating?
AIt causes the Arduino to reset
BIt can cause random HIGH or LOW readings
CIt drains the battery quickly
DIt makes the button press slower
✗ Incorrect
Floating pins pick up noise and cause unstable readings.
Which Arduino function sets a pin as input with an internal pull-up resistor?
AdigitalRead(pin)
BpinMode(pin, OUTPUT_PULLUP)
CdigitalWrite(pin, HIGH)
DpinMode(pin, INPUT_PULLUP)
✗ Incorrect
pinMode(pin, INPUT_PULLUP) enables the internal pull-up resistor.
If you want to use an external pull-up resistor, where should it be connected?
ABetween the pin and 5V
BBetween the button and 5V
CBetween the button and ground
DBetween the pin and ground
✗ Incorrect
A pull-up resistor connects the input pin to 5V to keep it HIGH when the button is not pressed.
Explain how a pull-up resistor works when reading a button press on an Arduino.
Think about what happens to the pin voltage when the button is pressed or released.
You got /4 concepts.
Describe how to use Arduino's internal pull-up resistor to read a button and why it is useful.
Consider how internal pull-ups replace external resistors.
You got /4 concepts.
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
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.
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.
Final Answer:
Enables the internal pull-up resistor to keep the pin HIGH when button is not pressed -> Option A
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
Step 1: Recall correct pinMode usage
The correct way to enable internal pull-up resistor is pinMode(pin, INPUT_PULLUP);.
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.
Final Answer:
pinMode(7, INPUT_PULLUP); -> Option C
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
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.
Step 2: Analyze Serial output
The code prints the pin state every 500ms. When pressed, it prints 0; when released, it prints 1.
Final Answer:
Prints 0 when button pressed, 1 when released -> Option D
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
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.
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.
Final Answer:
Missing INPUT_PULLUP mode to enable pull-up resistor -> Option A
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?