日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用声音传感器控制led灯功能_云中树莓派(4):利用声音传感器控制Led灯

發布時間:2024/3/7 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用声音传感器控制led灯功能_云中树莓派(4):利用声音传感器控制Led灯 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 聲音傳感器及其配置

聲音傳感器如下圖所示:

將 VCC 引腳接入樹莓派 5V 引腳,將 GND 引腳接入樹莓派 GND 引腳,將 OUT 引腳接入樹莓派 GPIO20。

要注意,模塊在環境聲音強度達不到設定閾值時,OUT輸出高電平(1),當外界環境聲音強度超過設定閾值時,模塊OUT輸出低電平(0)。

2. GPIO Event 機制

樹莓派提供了三種電信號事件反饋機制。

(1)GPIO.wait_for_edge:直接等待電信號達到某種條件(升高還是降低或者任意),并且可以設置超時時間。在超時時間內,函數會一直等待,直到期望的電信號改變出現,或者超時。

#wait for up to 5 seconds for a rising edge (timeout is in milliseconds)

channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000)if channel isNone:print('Timeout occurred')else:print('Edge detected on channel', channel)

(2)GPIO.add_event_detect:設置事件觸發檢測,一旦檢測到,會返回True。

GPIO.add_event_detect(channel, GPIO.RISING) #add rising edge detection on a channel

do_something()ifGPIO.event_detected(channel):print('Button pressed')

(3)GPIO.add_event_detect:回調函數機制。注冊回調函數,一旦指定事件觸發,回調函數會被調用。

defmy_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) #add rising edge detection on a channel

3. 利用聲音檢查模塊控制Led燈

實現目標:當檢測到聲音時,改變Led 燈的狀態。

3.1 代碼

importRPi.GPIO as GPIOimporttimefrom time importsleep

SOUND_PIN_NUM= 20 #聲音模塊的輸出引腳接的GPIOLED_PIN_NUM= 26 #LED 的長腳接的GPIOstate=0 #保存led 的狀態

timeLast=time.time() #保存上次觸發的時間#in one sounding, the callback function will be invoked for a few times, so need wait for some time to

validDuration = 0.1GPIO.setmode(GPIO.BCM)

GPIO.setup(SOUND_PIN_NUM, GPIO.IN)

GPIO.setup(LED_PIN_NUM, GPIO.OUT)defcallback_fun_soundOccurred(input_pint):globaltimeLast

timeNow=time.time()

duration= timeNow -timeLastif (duration

timeLast=timeNowreturn

print("accepted for valid duration" +str(duration))

timeLast=timeNow

switchLed()defswitchLed():globalstateif(state):

turnOffLed()

state=0else:

turnOnLed()

state= 1

defturnOnLed():print("Turn on")

GPIO.output(LED_PIN_NUM,GPIO.HIGH)defturnOffLed():print("Turn off")

GPIO.output(LED_PIN_NUM, GPIO.LOW)

GPIO.add_event_detect(SOUND_PIN_NUM, GPIO.RISING, callback=callback_fun_soundOccurred)try:whileTrue:

sleep(0.1)exceptKeyboardInterrupt:

GPIO.remove_event_detect(SOUND_PIN_NUM)

GPIO.cleanup()

3.2 兩個小技巧

(1)盡管一個只需要一塊五毛錢,但聲音檢測模塊的靈敏度是可以調節的。使用螺絲刀轉動上面的旋鈕,邊轉變說話,看其開關指示燈的反應,亮表示檢測到聲音,亮度表示聲音大小。旋到合適的位置即可。默認時,它非常靈敏,任何細小的聲音都會觸發它。

(2)在一句話說話過程中,回調函數會被觸發好多次。因此,需要的話,如上面代碼,可以計算兩次調用之間的事件間隔,把太短的間隔過濾掉。下面是一句短話過程中函數被觸發的情況:

ignored because duration 0.000501155853271 istoo short

ignored because duration0.000110864639282 istoo short

ignored because duration0.00215411186218 istoo short

ignored because duration0.000218868255615 istoo short

ignored because duration0.000470161437988 istoo short

ignored because duration0.000167846679688 istoo short

ignored because duration0.000583171844482 istoo short

ignored because duration0.000425815582275 istoo short

ignored because duration0.0010621547699 istoo short

ignored because duration0.000314950942993 istoo short

ignored because duration0.000555038452148 istoo short

ignored because duration0.000130891799927 istoo short

ignored because duration0.000461101531982 istoo short

ignored because duration0.00022292137146 istoo short

ignored because duration0.00274705886841 istoo short

ignored because duration0.000133037567139 istoo short

ignored because duration0.00597095489502 istoo short

ignored because duration0.000155925750732 istoo short

ignored because duration0.00107598304749 istoo short

ignored because duration0.000198125839233 is too short

參考鏈接:

歡迎大家關注我的個人公眾號:

總結

以上是生活随笔為你收集整理的利用声音传感器控制led灯功能_云中树莓派(4):利用声音传感器控制Led灯的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。