0
0
Raspberry Piprogramming~15 mins

Digital input (GPIO.input) in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading a Button Press with GPIO.input on Raspberry Pi
📖 Scenario: You have a Raspberry Pi connected to a push button. When you press the button, the Raspberry Pi should detect it and tell you if the button is pressed or not.
🎯 Goal: Build a simple program that reads the button state using GPIO.input and prints whether the button is pressed or released.
📋 What You'll Learn
Use the RPi.GPIO library to access GPIO pins
Set up GPIO pin 18 as an input with a pull-down resistor
Read the button state using GPIO.input(18)
Print 'Button Pressed' when the button is pressed
Print 'Button Released' when the button is not pressed
💡 Why This Matters
🌍 Real World
Buttons and switches are common inputs in electronics projects. Reading their state lets your Raspberry Pi respond to user actions.
💼 Career
Understanding GPIO input is essential for embedded systems, IoT devices, and hardware interfacing roles.
Progress0 / 4 steps
1
Set up GPIO and button pin
Import the RPi.GPIO library as GPIO and set the GPIO mode to GPIO.BCM. Then create a variable called button_pin and set it to 18.
Raspberry Pi
Need a hint?

Use import RPi.GPIO as GPIO to import the library. Use GPIO.setmode(GPIO.BCM) to set pin numbering. Assign 18 to button_pin.

2
Configure the button pin as input with pull-down resistor
Use GPIO.setup to configure button_pin as an input pin with a pull-down resistor by passing GPIO.IN and pull_up_down=GPIO.PUD_DOWN.
Raspberry Pi
Need a hint?

Use GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) to set the pin as input with pull-down resistor.

3
Read the button state using GPIO.input
Create a variable called button_state and assign it the value returned by GPIO.input(button_pin).
Raspberry Pi
Need a hint?

Use GPIO.input(button_pin) to read the current state of the button.

4
Print if the button is pressed or released
Use an if statement to check if button_state is GPIO.HIGH. If yes, print "Button Pressed". Otherwise, print "Button Released".
Raspberry Pi
Need a hint?

Use if button_state == GPIO.HIGH: to check if the button is pressed. Print the messages accordingly.