Program to read from push button

In Chapter 4, Controlling LED Using a Push Button, we did a push button press detection exercise. Let's do the same in Python. Connect a push button as shown in Chapter 4. Type the following program in Cloud9, save it as push_button.py and run. You should get Button is pressed as the output when you press the button. When the button is not pressed, you get Button is released printed as the output:

#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
from time import sleep
button = 'P8_16'

GPIO.setup(button,GPIO.IN)

while True:
    if GPIO.input(button) == True: # Checks if the pin is HIGH
        print("Button is pressed")
    else:
        print("Button is released")
    sleep(1)

Explanation

In the preceding code, we chose pin P8_16 to connect to the push button. The push button is a component of input type. So, we set the direction as input using the GPIO.setup() function. Now, we can read the state of the GPIO pin. As this is a digital component, the state will be HIGH or LOW. In Python, HIGH and LOW are mapped to Boolean True and False. The function GPIO.input() reads the state of the specified GPIO pin. If the actual button is pressed, it returns True otherwise it returns False. Inside the infinite loop, we check if the GPIO pin connected to the button is HIGH. If yes, Button is pressed is printed. Otherwise we print Button is released.

Detecting button state using interrupt

Our previous program prints the button state each second. Even if nobody presses the button for a long time, it will still wake up each second and check if the button is pressed. This is an inefficient polling mechanism. Also, if you press the button quickly in between two printings, it will be lost. A better approach to this problem is to use the "interrupt" mechanism. Let's write a program that uses the interrupt mechanism to detect change in the push button state. Type the following program in Cloud9, save it as push_button2.py and run it. You should get Button is pressed when you press the button. When you release the button, it will print Button is released.

#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO

button = 'P8_16'

GPIO.setup(button,GPIO.IN)

while True:
    GPIO.wait_for_edge(button, GPIO.RISING)
    print("Button is pressed")
    GPIO.wait_for_edge(button, GPIO.FALLING)
    print("Button is released")

Explanation

Let's see how the interrupt mechanism works. We set the direction of the GPIO pin P8_16 as the input. Adafruit BBIO library provides the function wait_for_edge() to register an interrupt. When the button is pressed, the voltage on pin goes from 0 to 3.3V. This is a voltage RISING event. When it is released, the voltage goes from 3.3 to 0V. This is a voltage FALLING event. An interrupt can be registered on the RISING event or FALLING event. When any of these events occur, the library is woken up and it continues execution of the program. When the actual push button is pressed, the CPU will get an interrupt of type FALLING. The interrupt handler will wake up the Python library, which will execute the next statement of printing Button is pressed. When the button is released, an interrupt of the type FALLING will be triggered. Control will reach to the next statement, which will print Button is released. Instead of printing, you can turn the LED on/off depending on the button pressed/released. This addition will achieve same result as of the program in Program to control LED by push button section from Chapter 4, Controlling LED Using a Push Button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.45.5