2,按钮控制LDE灯亮灭

学习重点

1,上拉与下拉电阻

If you do not have the input pin connected to anything, it will 'float'. (即可能是任何值)为了解决这个问题,可以接个上拉或下拉电阻。原理大家可以到网上查一下,树莓派的引脚已经集成了上拉和下来电阻,所以不需要我们在输入引脚上外接电阻,但是需要在设置引脚是输出引脚时设置一下,是使用上拉还是下拉电阻
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)
or
GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
In hardware, a 10K resistor between the input channel and 3.3V (pull-up) or 0V (pull-down) is commonly used.
电阻直接和 GPIO(3.3V)连接 是 上拉电阻,按钮断开时,GPIO 读出来的 是 1
电阻直接和 Ground(0V)连接是 下拉电阻, 按钮断开时,GPIO 读出来的是 0

2,读取引脚的输入数据(polling 与 interrupts)

GPIO.setup(channel, GPIO.IN) // 如果不外接的话,读出的值是不确定的。

polling

It can potentially miss an input if your program reads the value at the wrong time.
And It can potentially be processor intensive.

while GPIO.input(channel) == GPIO.LOW:
    time.sleep(0.01)  # wait 10 ms to give CPU chance to do other things

interrupts

Base on edge detection.
An edge is the name of a transition from HIGH to LOW (falling edge) or LOW to HIGH (rising edge).
GPIO.RISING, GPIO.FALLING or GPIO.BOTH

# wait for up to 5 seconds for a rising edge (timeout is in milliseconds)
#process will block for 5 seconds
channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000)
if channel is None:
    print('Timeout occurred')
else:
    print('Edge detected on channel', channel)

The event_detected() function is designed to be used in a loop with other things, but unlike polling it is not going to miss the change in state of an input while the CPU is busy working on other things. This could be useful when using something like Pygame or PyQt where there is a main loop listening and responding to GUI events in a timely basis

GPIO.add_event_detect(channel, GPIO.RISING)  # add rising edge detection on a channel
do_something()
if GPIO.event_detected(channel):
    print('Button pressed')

Threaded callbacks
RPi.GPIO runs a second thread for callback functions. This means that callback functions can be run at the same time as your main program, in immediate response to an edge. For example:

def my_callback(channel):
    print('This is a edge event callback function!')
    print('Edge detected on channel %s'%channel)
    print('This is run in a different thread to your main program')

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

or If you wanted more than one callback function:

def my_callback_one(channel):
    print('Callback one')

def my_callback_two(channel):
    print('Callback two')

GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, my_callback_one)
GPIO.add_event_callback(channel, my_callback_two)

3,按钮的使用

开关一般有4个脚,每2根脚是互相联通,电会从这俩脚里流过,用的时候意选一组用就可以了。可以自己研究一下哪2个联通或者问卖家.

电路连接

注意开关一头接在输入GPIO引脚上,一头接在3.3V的电压上

参考代码 buttonLed.py

from RPi import GPIO
import time
 
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
buttonChannel = 7
ledChannel = 40
ledState = False
GPIO.setup(buttonChannel, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(ledChannel,GPIO.OUT)

def on_switch_pressed(channel):
    global ledState
    print('on_swith_pressed:', channel)
    if GPIO.input(channel) == GPIO.LOW:
    # Note some time  edge low is missed.
        print('Button Release ---')
    else:
        print('Button press +++')
        ledState = not ledState
        GPIO.output(ledChannel, ledState is True)

GPIO.add_event_detect(buttonChannel, GPIO.BOTH, bouncetime = 200, callback = on_switch_pressed) 

while True:
    time.sleep(10)
GPIO.cleanup(ledChannel)
GPIO.cleanup(buttonChannel)

参考
https://yuerblog.cc/2018/11/05/raspberry-pi-gpio-switch-pullup-pulldown/

你可能感兴趣的:(2,按钮控制LDE灯亮灭)