Interfacing Push Button with Raspberry Pi
Before, interfacing button, we should have the basics of Python shell and how to use the GPIO pins of PI using python codes. Here is a quick refresher for you.
Python is a widely used high-level programming language that one can use to develop his own program. It has wide application in tinkering Raspberry Pi and the best tool to program and use GPIO pins of Pi and to do almost everything. By using Raspberry pi libraries for Python, the program can be structured in a proper manner without any complexity. As an example OpenCV based on python, let image processing on raspberry pi simple and effective.
For opening Python Shell with full privileges, open the command terminal and type in: (idle refer to the name of python IDE)
Python shell is an very interactive interpreter where you can check and run python commands. When a script program is made to run then the corresponding results could be showed in a python shell and thereby you can verify it. In a Python shell a user can perform simple arithmetic calculations, printing a string, string concatenation, swapping, shorting of data etc.
Basics of Python:
Printing a Text:
Concatenation of Strings:
Looping: For Loop
Python editor save the file with .py extension, which is nothing but a python file format. We can write the corresponding program in the editor file and save it in a suitable folder. The suitable indications can be viewed in the python shell itself.
In order to get an editor file open a python shell and click on File–>New Window
Push Button with Raspberry Pi:
Connect the push button with Pi as shown below. When the push button is pressed, it will connect pin 7 configured as an input to ground. The input pin is pulled up to 3.3V by the command;
pull_up_down=GPIO.PUD_UP in GPIO.setup
By giving this command the pull up resistor is enabled, as each GPIO pin has software configurable pull-up and pull-down resistors. When the button is pressed the false state will be returned.
Figure below shows a sample code and the indication result in the python shell tab
With some addition of code you can perform many operation based on it.
Thus, in this tutorial you learned to use the basic python commands on Pi and also to use the GIPO pins as Input/Output and interface a Led and Button, which is like the first step for making electronics project with Pi.