O'Reilly logo

Internet of Things with Python by Gastón C. Hillar

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

Reading pushbutton statuses with digital inputs and the mraa library

We will create a new PushButton class to represent a pushbutton connected to our board that can use either a pull-up or a pull-down resistor. The following lines show the code for the new PushButton class that works with the mraa library. The code file for the sample is iot_python_chapter_05_01.py.

import mraa import time from datetime import date class PushButton: def __init__(self, pin, pull_up=True): self.pin = pin self.pull_up = pull_up self.gpio = mraa.Gpio(pin) self.gpio.dir(mraa.DIR_IN) @property def is_pressed(self): push_button_status = self.gpio.read() if self.pull_up: # Pull-up resistor connected return push_button_status == 0 else: # Pull-down resistor connected return ...

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