Button Read on Raspberry-Pi
By: Ankush Agrawal
Hardware Requirement
1. Raspberry-Pi Board
2. Bread Board
3. Switch
4. Connecting Wires
Raspberry-Pi GPIO PINS
1. There are 40 pins on the Raspberry Pi (26 pins on early
models), and they provide various different functions.
Hardware Setup
• Connect Switch to Raspberry-Pi:
• Connect Switch terminal to Raspberry-Pi pin GND.
• Connect another Switch terminal to Raspberry-Pi
GPIO pin 2.
Hardware Setup
Using buttons to get input
• Create a new file by clicking File > New file.
• Save the new file by clicking File > Save. Save the file
as button.py.
• This time you'll need the Button class, and to tell it that
the button is on pin 2. Write the following code in your
new file:
Using buttons to get input
from gpiozero import Button
from time import sleep
button = Button(2)
while True:
button.wait_for_press()
print(‘Button Press')
sleep(1)
How to Execute Code
• After type code you need to save or press ctrl+s.
• For execute code press F5 from your key board, or Go to
Run in program file select Run Module.
• The LED should be flashing on and off. To exit the
program press Ctrl + C on your keyboard.
• Again press button for exit loop.