O'Reilly logo

Programming the BeagleBone by Yogesh Chavan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required