Raspberry_Pi_Education_Manual

Notes:

Push-button circuit

This is the about the simplest circuit you can use to test GPIO inputs with your Raspberry Pi. The 10k resistor is what is known as a “pull-up” resistor – that means that the input will be pulled high (to 3.3V) when the button is not pressed. When you press the button, it connects the input to 0V via the 1k resistor, sending the input low. The 1k resistor is present to protect your Raspberry Pi in case you accidentally set it up as an output instead of an input. The Gertboard has some channels wired up like this circuit.

Push-button circuit experiment wiring diagram.

On the next page is an example of some Python code that monitors a push button. We use the tasking features of Python to create a class that monitors a push button to demonstrate how we might use multitasking. This is so that we do not miss the button press while the program is busy doing other things. You will probably notice that this is similar to checking for events when using PyGame. When using the Python RPi.GPIO module, LOW = False and HIGH = True. As in the previous example, this program must be run as root by putting “sudo” in front of the Python command.

import threading import time import RPi.GPIO as GPIO class Button (threading.Thread): """A Thread that monitors a GPIO button""" def __init__ (self, channel): threading.Thread.__init__(self) self._pressed = False self.channel = channel # set up pin as input GPIO.setup(self.channel, GPIO.IN) # terminate this thread when main program finishes self.daemon = True

Human-computer interfacing

132

Made with FlippingBook flipbook maker