官术网_书友最值得收藏!

The applications of conditional statements: executing tasks using GPIO

In the previous chapter, we discussed interfacing outputs to the Raspberry Pi's GPIO. Let's discuss an example where a simple push button is pressed. A button press is detected by reading the GPIO pin state. We are going to make use of conditional statements to execute a task based on the GPIO pin state.

Let us connect a button to the Raspberry Pi's GPIO. All you need to get started are a button, pull-up resistor, and a few jumper wires. The figure given later shows an illustration on connecting the push button to the Raspberry Pi Zero. One of the push button's terminals is connected to the ground pin of the Raspberry Pi Zero's GPIO pin.

The schematic of the button's interface is shown here:

Raspberry Pi GPIO schematic

The other terminal of the push button is pulled up to 3.3V using a 10 K resistor. The junction of the push button terminal and the 10 K resistor is connected to the GPIO pin 2 (refer to the BCM GPIO pin map shared in the earlier chapter).

Interfacing the push button to the Raspberry Pi Zero's GPIO - an image generated using Fritzing

Let's review the code required to review the button state. We make use of loops and conditional statements to read the button inputs using the Raspberry Pi Zero.

We will be making use of the gpiozero library introduced in the previous chapter. The code sample for this section is GPIO_button_test.py and available for download along with this chapter.

In a later chapter, we will discuss object-oriented programming (OOP). For now, let's briefly discuss the concept of classes for this example. A class in Python is a blueprint that contains all the attributes that define an object. For example, the Button class of the gpiozero library contains all attributes required to interface a button to the Raspberry Pi Zero's GPIO interface. These attributes include button states and functions required to check the button states and so on. In order to interface a button and read its states, we need to make use of this blueprint. The process of creating a copy of this blueprint is called instantiation.

Let's get started with importing the gpiozero library and instantiate the Button class of the gpiozero library (we will discuss Python's classes, objects, and their attributes in a later chapter). The button is interfaced to GPIO pin 2. We need to pass the pin number as an argument during instantiation:

from gpiozero import Button 

#button is interfaced to GPIO 2
button = Button(2)

The gpiozero library's documentation is available at http://gpiozero.readthedocs.io/en/v1.2.0/api_input.htmlAccording to the documentation, there is a variable named is_pressed in the Button class that could be tested using a conditional statement to determine if the button is pressed:

if button.is_pressed: 
print("Button pressed")

Whenever the button is pressed, the message Button pressed is printed on the screen. Let's stick this code snippet inside an infinite loop:

from gpiozero import Button 

#button is interfaced to GPIO 2
button = Button(2)

while True:
if button.is_pressed:
print("Button pressed")

In an infinite while loop, the program constantly checks for a button press and prints the message as long as the button is being pressed. Once the button is released, it goes back to checking whether the button is pressed.

主站蜘蛛池模板: 汕尾市| 华安县| 瑞安市| 德令哈市| 上栗县| 乌拉特后旗| 保康县| 临武县| 保靖县| 广昌县| 宝鸡市| 博爱县| 临洮县| 阜城县| 德清县| 石屏县| 永清县| 安泽县| 东宁县| 汉川市| 承德县| 湘潭县| 绿春县| 乐都县| 巴楚县| 新疆| 杂多县| 高密市| 磴口县| 正镶白旗| 遂溪县| 共和县| 宝山区| 乐清市| 汉阴县| 合阳县| 苗栗县| 故城县| 靖江市| 东丰县| 马鞍山市|