0
0
Raspberry Piprogramming~10 mins

Digital input (GPIO.input) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Digital input (GPIO.input)
Setup GPIO pin as input
Read pin state with GPIO.input
Check if pin is HIGH or LOW
Use pin state in program
Repeat or Exit
This flow shows how to set a pin as input, read its digital state, and use that value in your program.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
state = GPIO.input(17)
print(state)
This code sets GPIO pin 17 as input, reads its digital state (0 or 1), and prints it.
Execution Table
StepActionPin 17 ModeGPIO.input(17) ResultOutput
1Set GPIO mode to BCMNot set yetN/ANo output
2Setup pin 17 as inputInputN/ANo output
3Read pin 17 stateInput0 (LOW) or 1 (HIGH)No output yet
4Print stateInput0 or 1Prints 0 or 1 to console
💡 Program ends after printing the pin state.
Variable Tracker
VariableStartAfter GPIO.setupAfter GPIO.inputFinal
stateUndefinedUndefined0 or 10 or 1
Key Moments - 3 Insights
Why do we need to set the pin mode before reading it?
GPIO pins must be set as input or output first; otherwise, GPIO.input won't know how to read the pin. See execution_table step 2 where pin mode changes to input.
What does GPIO.input(17) return?
It returns 0 if the pin voltage is LOW (off) or 1 if HIGH (on). This is shown in execution_table step 3.
Can GPIO.input return values other than 0 or 1?
No, GPIO.input only returns digital states: 0 (LOW) or 1 (HIGH), never other numbers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pin mode after step 2?
AOutput
BNot set
CInput
DAnalog
💡 Hint
Check the 'Pin 17 Mode' column in execution_table row for step 2.
At which step does GPIO.input(17) return the pin state?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'GPIO.input(17) Result' column in execution_table.
If the pin is connected to 3.3V, what will GPIO.input(17) return?
A1
B0
C-1
DIt depends on the pin mode
💡 Hint
GPIO.input returns 1 for HIGH voltage, see key_moments about return values.
Concept Snapshot
GPIO.input reads the digital state of a pin.
1. Set pin mode to input with GPIO.setup(pin, GPIO.IN).
2. Call GPIO.input(pin) to get 0 (LOW) or 1 (HIGH).
3. Use the returned value in your program.
Always set mode before reading.
Returns only 0 or 1.
Full Transcript
This lesson shows how to read a digital input from a Raspberry Pi GPIO pin. First, you set the pin mode to input using GPIO.setup. Then, you read the pin state with GPIO.input, which returns 0 if the pin is LOW or 1 if it is HIGH. Finally, you can use this value in your program, for example, printing it. The execution table traces these steps, showing the pin mode changes and the input reading. Key points include the need to set the pin mode before reading and that GPIO.input only returns 0 or 1. The visual quiz tests understanding of these steps and values.