輕觸開關

      在〈輕觸開關〉中尚無留言

硬体規格

輕觸開關圖片如下所示

共有四個腳位,但只有二個引腳。內凹相對的那組是直接連通的,如下圖所示,沒按下時,13相連,24相連。

當按下時,12及34才會相連。放開時就會斷開。

todo

線路接法

先供應 3.3V 電壓 (樹莓派腳位1) 給輕觸開關 (腳2) ,但電源跟開關之間要加一顆10KΩ電阻,然後輕觸開關腳 4 再接 1KΩ電阻,再接入GPIO 進行偵測是否開關有按下,最後 GND 接在輕觸開關腳位 3。如下圖所示。

todo

測試程式

todo

觸發Led

todo

程式碼

todo

import RPi.GPIO as gpio
import time
WAIT_TIME=200
led17 = 17
touch_key=18
ledOn=False
def led(data):
    global ledOn
    ledOn = not ledOn
    if ledOn:
        gpio.output(led17, gpio.HIGH)
    else:
        gpio.output(led17, gpio.LOW)
def init(callback):
    gpio.setwarnings(False)
    gpio.setmode(gpio.BCM)
    gpio.setup(led17, gpio.OUT)
    gpio.output(led17, gpio.LOW)    
    gpio.setup(touch_key, gpio.IN)
    gpio.add_event_detect(touch_key, gpio.RISING, callback=callback,bouncetime=WAIT_TIME)#放開
    #gpio.add_event_detect(touch_key, gpio.FALLING, callback=callback,bouncetime=WAIT_TIME)#按下
def destroy():
    try:
        gpio.remove_event_detect(touch_key)
        gpio.cleanup()
    except Exception as e :
        print(f"destroy fail:{e}")
if __name__=='__main__':
    try:
        init(led)        
        while True:
            time.sleep(1)
    except:
        destroy()

todo

執行緒

若要變換 Led,則需使用執行緒。

本範例按一下按鈕,三個 Led 依順序切換,再按一下則全部關掉,完整程式碼如下。

import RPi.GPIO as gpio
import time
import threading
WAIT_TIME=200
led18 = 13
led23 = 19
led24 = 26
touch_key=17
ledOn=False
def led(data):
    global ledOn
    ledOn = not ledOn
    print("Thomas", ledOn)
    if ledOn:
        t=threading.Thread(target=runnable)
        t.start()
    else:
        gpio.output(led18, gpio.LOW)
        gpio.output(led23, gpio.LOW)
        gpio.output(led24, gpio.LOW)
def runnable():
    while ledOn:
        gpio.output(led18, gpio.HIGH)
        time.sleep(0.05)
        gpio.output(led18, gpio.LOW)
        time.sleep(0.01)
        gpio.output(led23, gpio.HIGH)
        time.sleep(0.05)
        gpio.output(led23, gpio.LOW)
        time.sleep(0.01)
        gpio.output(led24, gpio.HIGH)
        time.sleep(0.05)
        gpio.output(led24, gpio.LOW)
        time.sleep(0.01)

        gpio.output(led24, gpio.HIGH)
        time.sleep(0.1)
        gpio.output(led24, gpio.LOW)
        time.sleep(0.01)
        gpio.output(led23, gpio.HIGH)
        time.sleep(0.1)
        gpio.output(led23, gpio.LOW)
        time.sleep(0.01)
        gpio.output(led18, gpio.HIGH)
        time.sleep(0.1)
        gpio.output(led18, gpio.LOW)
        time.sleep(0.01)

def init(callback):
    gpio.setwarnings(False)
    gpio.setmode(gpio.BCM)
    gpio.setup(led18, gpio.OUT)
    gpio.output(led18, gpio.LOW)
    gpio.setup(led23, gpio.OUT)
    gpio.output(led23, gpio.LOW)
    gpio.setup(led24, gpio.OUT)
    gpio.output(led24, gpio.LOW)    
    gpio.setup(touch_key, gpio.IN)
    gpio.add_event_detect(touch_key, gpio.RISING, callback=callback,bouncetime=WAIT_TIME)#放開
    #gpio.add_event_detect(touch_key, gpio.FALLING, callback=callback,bouncetime=WAIT_TIME)#按下
def destroy():
    try:
        gpio.remove_event_detect(touch_key)
        gpio.cleanup()
    except Exception as e :
        print(f"destroy fail:{e}")
if __name__=='__main__':
    try:
        init(led)        
        while True:
            time.sleep(1)
    except:
        destroy()

todo

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *