Maker.io main logo

Raspberry Pi Pushbutton Switch for Beginners

2017-03-01 | By All About Circuits

License: See Original Project Raspberry Pi

Courtesy of All About Circuits

Want to build a Raspberry Pi Controller? Here’s How:

The Raspberry Pi (RPi) is a single board Linux computer that allows for the creation of a variety of programming projects and electronic devices. Thanks to a dual inline female header connector and an accessible General Purpose Input/Output (GPIO), you can program a variety of electrical, electronic, and electromechanical devices that can be wired to the Raspberry Pi. The RPi also has support features like audio, composite video, camera, USB, HDMI, and LCD devices using standard electrical connectors, audio, and RCA phono jacks. You can connect to the web easily by way of a standard Ethernet connector. The RPi uses the Python programming language, which is standard for the Debian Linux distro operating system installed on the SD card. 

In this introductory project to building electronic controllers for RPi, you'll learn how to wire a programmable pushbutton switch for the RPi and read its status using a basic Python script.

You don’t need a lot of electronic components this programmable switch and it’s quite easy to build. The Parts List for the programmable switch is shown below.

Bill of Materials

Installing the RPi.GPIO Library

The RPi.GPIO library needs to be installed on the Raspberry Pi in order to read the tactile pushbutton switch’s status. The RPi.GPIO  is a software module which allows the Raspberry Pi’s GPIO pins to be used for electronic controls applications. In order to install the RPi.GPIO library onto the Raspberry Pi, you will need to open the LXTerminal and type the linux installation command shown below after the prompt:

Copy Code
pi@raspberrypi ~ $ sudo apt-get install python-dev python~rpi.gpio

After you enter the Linux installation command, you’ll see a series of RPi.GPIO bulld-installation file sequences being displayed on your monitor as shown below.

Code for RPi.GPIO

Once the build has been completed, the RPi is ready for to read the status of a tactile pushbutton switch.

Wiring the Tactile Pushbutton Switch to the RPi

It’s quite easy to attach the tactile pushbutton switch to your RPi. It’s important to remember is that the RPi's GPIO pins are 3.3VDC compliant. Applying voltages greater than 3.3VDC will cause damage to the RPi. The electrical wiring diagram for attaching the tactile pushbutton switch is shown below.

Although, wiring the circuit for the RPi is quite simple, it’s good practice to recheck the wiring before programming your RPi. This is an important verification step that will ensure successful project execution when the python code is installed and running on the RPi. 

The Programmable Switch Python Script

Providing a python script for reading a tactile pushbutton switch wired to a RPi GPIO pin is the next phase of this project build. The python script for reading a tactile pushbutton switch is shown below.

Copy Code
import  RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)

while True:
inputValue = GPIO.input(18)
if (inputValue == False):
print("Button press ")
time.sleep(0.3)

You can enter this script using the LXTerminal's nano editor or with the Python's IDLE (Integrated Development Environment). Save the script as pbbutton.py in the RPi’s home/pi directory. Next, type the Linux command shown below in order to run the script on the RPi into the LXTerminal as shown next.

Copy Code
pi@raspberrypi ~ $ sudo python pbbutton.py

Next, press the pushbutton switch to see if the code worked. You will see the message "button press" displayed on your monitor if the script was typed correctly. 

Congratulations, you now have a programmable pushbutton switch!

"button press" pop-up displays

For additional build reference, check out the video clip below.

The tactile pushbutton switch can be programmed easily to provide a variety of switching responses and output messages. Try changing the "Button Press" message to display your name or a whimsical word when you activate the switch. Record your results in a laboratory notebook. In the next tutorial, you will learn how to code your RPi to toggle an LED on and off using your programmable pushbutton switch.

 

制造商零件编号 1825910-6
SWITCH TACTILE SPST-NO 0.05A 24V
TE Connectivity ALCOSWITCH Switches
¥1.30
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum