What does GPIO.input(pin) do in Raspberry Pi programming?
It reads the current digital value (HIGH or LOW) from the specified GPIO pin, telling you if the pin is receiving voltage or not.
Click to reveal answer
beginner
Why do you need to set up a GPIO pin as input before using GPIO.input()?
Because the Raspberry Pi needs to know if the pin is used to read signals (input) or send signals (output). Setting it as input prepares it to detect voltage changes.
Click to reveal answer
beginner
What are the possible return values of GPIO.input(pin)?
It returns either GPIO.HIGH (or 1) if voltage is detected, or GPIO.LOW (or 0) if no voltage is detected.
Click to reveal answer
intermediate
How can you use GPIO.input() to detect a button press?
Connect the button to a GPIO pin set as input. When pressed, the pin reads HIGH or LOW depending on wiring. Use GPIO.input(pin) to check if the button is pressed.
Click to reveal answer
intermediate
What is a pull-up or pull-down resistor and why is it important when using GPIO.input()?
Pull-up or pull-down resistors keep the input pin at a known voltage level (HIGH or LOW) when no button is pressed, preventing random noise from causing false readings.
Click to reveal answer
What must you do before reading a pin with GPIO.input(pin)?
ANothing, just read directly
BSet the pin as output using <code>GPIO.setup(pin, GPIO.OUT)</code>
CCall <code>GPIO.output(pin, True)</code>
DSet the pin as input using <code>GPIO.setup(pin, GPIO.IN)</code>
✗ Incorrect
You must set the pin as input first so the Raspberry Pi knows to read signals from it.
What does GPIO.input(pin) return if the pin voltage is LOW?
AGPIO.HIGH
BAn error
CGPIO.LOW
DNone
✗ Incorrect
It returns GPIO.LOW (or 0) when the pin voltage is low.
Why use a pull-down resistor with a button input?
ATo keep the pin voltage LOW when button is not pressed
BTo keep the pin voltage HIGH when button is not pressed
CTo increase voltage
DTo power the button
✗ Incorrect
A pull-down resistor keeps the pin LOW when the button is not pressed, avoiding random signals.
If GPIO.input(pin) returns 1, what does it mean?
APin is HIGH
BPin is LOW
CPin is broken
DPin is not set up
✗ Incorrect
A return value of 1 means the pin voltage is HIGH.
Which Python library is commonly used for GPIO input on Raspberry Pi?
Apandas
BRPi.GPIO
Cmatplotlib
Dnumpy
✗ Incorrect
RPi.GPIO is the standard library for controlling GPIO pins on Raspberry Pi.
Explain how to read a button press using GPIO.input on a Raspberry Pi.
Think about wiring and reading the pin state.
You got /4 concepts.
Describe why pull-up or pull-down resistors are important when using digital inputs.
Imagine a wire not connected to anything.
You got /3 concepts.
Practice
(1/5)
1. What does GPIO.input(pin) do in Raspberry Pi programming?
easy
A. Resets the pin to default state
B. Sets the pin mode to output
C. Writes a value to the pin
D. Reads the current digital state (True/False) of the specified pin
Solution
Step 1: Understand GPIO.input function
GPIO.input(pin) reads the current digital signal on the given pin, returning True if the pin is HIGH and False if LOW.
Step 2: Differentiate from other GPIO functions
Functions like GPIO.setup set pin modes, and GPIO.output writes values. Only GPIO.input reads the pin state.
Final Answer:
Reads the current digital state (True/False) of the specified pin -> Option D
2. Which of the following is the correct way to set up GPIO pin 17 as an input pin?
easy
A. GPIO.setup(17, GPIO.OUT)
B. GPIO.input(17, GPIO.IN)
C. GPIO.setup(17, GPIO.IN)
D. GPIO.input(17)
Solution
Step 1: Identify the correct function to set pin mode
Use GPIO.setup(pin, mode) to set pin mode. Here, mode should be GPIO.IN for input.
Step 2: Check each option
GPIO.setup(17, GPIO.OUT) sets pin as output, wrong mode. GPIO.input(17, GPIO.IN) uses GPIO.input incorrectly with two arguments. GPIO.input(17) calls GPIO.input without setup. Only GPIO.setup(17, GPIO.IN) correctly sets pin 17 as input.
Final Answer:
GPIO.setup(17, GPIO.IN) -> Option C
Quick Check:
Setup input pin = B [OK]
Hint: Use GPIO.setup(pin, GPIO.IN) to prepare input pins [OK]
Common Mistakes:
Using GPIO.input to set pin mode
Setting pin as output instead of input
Calling GPIO.input before setup
3. What will be the output of this code snippet if the button connected to pin 22 is pressed (assuming pressed means HIGH)?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
state = GPIO.input(22)
print(state)
medium
A. True
B. 22
C. False
D. Error
Solution
Step 1: Understand pin setup and input reading
Pin 22 is set as input. When the button is pressed, the pin reads HIGH, so GPIO.input(22) returns True.
Step 2: Check the print output
The variable state holds True, so print(state) outputs True.
Final Answer:
True -> Option A
Quick Check:
Pressed button = True output [OK]
Hint: Pressed button means pin HIGH, so GPIO.input returns True [OK]
Common Mistakes:
Assuming pressed means False
Confusing pin number with state
Expecting numeric output instead of boolean
4. Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.input(18)
GPIO.setup(18, GPIO.IN)
medium
A. GPIO.input called before GPIO.setup
B. GPIO.setup uses wrong pin number
C. GPIO.setmode is missing
D. No error, code is correct
Solution
Step 1: Check order of GPIO calls
GPIO pins must be set up before reading input. Here, GPIO.input(18) is called before GPIO.setup(18, GPIO.IN), which is incorrect.
Step 2: Verify other parts
Pin number 18 is consistent, and GPIO.setmode(GPIO.BCM) is present. So no error there.
Final Answer:
GPIO.input called before GPIO.setup -> Option A
Quick Check:
GPIO.input called before GPIO.setup = D [OK]
Hint: Always setup pins before reading input [OK]
Common Mistakes:
Calling GPIO.input before GPIO.setup
Ignoring pin mode setup
Assuming code runs without setup
5. You want to detect if a motion sensor connected to pin 27 is active (HIGH) and print "Motion detected" only once when it changes from LOW to HIGH. Which code snippet correctly implements this behavior?
hard
A. GPIO.setup(27, GPIO.OUT)
while True:
if GPIO.input(27):
print("Motion detected")
B. GPIO.setup(27, GPIO.IN)
prev_state = False
while True:
current_state = GPIO.input(27)
if current_state and not prev_state:
print("Motion detected")
prev_state = current_state
C. GPIO.setup(27, GPIO.IN)
while True:
if GPIO.input(27):
print("Motion detected")
D. GPIO.setup(27, GPIO.IN)
prev_state = True
while True:
current_state = GPIO.input(27)
if current_state and not prev_state:
print("Motion detected")
prev_state = current_state
Solution
Step 1: Setup pin as input and track previous state
Pin 27 must be set as input. To detect change from LOW to HIGH, store previous state starting as False (LOW).
Step 2: Detect rising edge and print once
Inside the loop, read current state. If current is True and previous is False, print message once. Then update previous state.
Step 3: Analyze options
GPIO.setup(27, GPIO.IN)
prev_state = False
while True:
current_state = GPIO.input(27)
if current_state and not prev_state:
print("Motion detected")
prev_state = current_state correctly implements this logic. GPIO.setup(27, GPIO.OUT)
while True:
if GPIO.input(27):
print("Motion detected") sets pin as output, wrong. GPIO.setup(27, GPIO.IN)
while True:
if GPIO.input(27):
print("Motion detected") prints repeatedly while HIGH, not once. GPIO.setup(27, GPIO.IN)
prev_state = True
while True:
current_state = GPIO.input(27)
if current_state and not prev_state:
print("Motion detected")
prev_state = current_state starts prev_state as True, so first detection missed.