Category :Projects for Raspberry Pi

Byamber

Connecting a Push Switch to Raspberry Pi

The programmable switch doesn’t required a lot of  electronic components and is quite easy to build. The Parts Lists for the programmable switch is shown below .

1)tactile pushbutton switch

2)Raspberry Pi

3)solderless breadboard

4)several jumper wires

Installing the RPi.GPIO Library

In order to read the tactile pushbutton switch status, the RPi.GPIO library needs to be installed on the Raspberry Pi. The RPi.GPIO  is a software module that conveniently allows the Raspberry Pi GPIO pins to be manipulated for electronic controls applications. To install the RPi.GPIO library onto the Raspberry Pi, open the LXTerminal and type the following linux installation command after the prompt:

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

After the linux installation command is entered, you will see a series of RPi.GPIO bulld-installation file sequences being displayed on the monitor as shown below.

Wiring the Tactile Pushbutton Switch to the RPi

Attaching a tactile pushbutton switch to the RPi is quite easy to do. An important item to remember is the RPi’s GPIO pins are +3.3VDC compliant. Applying voltages greater than +3.3VDC will damage the RPi. The electrical wiring diagram for attaching the tactile pushbutton switch is shown next.

raspberry-pi-pushbutton-fritzing

Although, the circuit wiring to the RPi is quite simple, recheck the wiring before programming the RPi. This important verification step will assure you project execution success when the python code is installed and running on the RPi.

The Programmable Switch Python Script

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

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 entered this script using either the LXTerminal’s nano editor or with the Python’s IDLE (Integrated Development Enviroment). Save the script as pbbutton.py in the home/pi directory of the RPi. Next, type the following linux command to run the script on the RPi into the LXTerminal as shown next.

Next, press the tactile pushbutton switch. If the script was typed correctly, you will see the message “button press” displayed on the monitor’s screen. Congratulations on building a programmable pushbutton switch!

pi@raspberrypi ~ $ sudo python pbbutton.py

The tactile pushbutton switch can easily be programmed to provide a variety of output messages and switching responses. Try changing the “Button Press” message to display your name or a whimiscal word when activating the switch. Record your results in a laboratory notebook.

tactile_button_Press