爆震感知器

爆震感知器

MicroPython 程式碼範例 運行在 Raspberry Pi Pico 2 W 微控制器上 在麵包板上,​​連接到一個帶有螺旋彈簧的爆震感測器模組、一個RGB LED模組和一個OLED顯示模組(SSD1306)。 當偵測到突然搖晃時,改變 LED 指示燈顏色並在顯示器上顯示狀態。

爆震感知器用於檢測振動。. 這個模組採用了一種基於彈簧的振動開關。. 當模組靜止時,彈簧不會接觸中心引腳;但當受到碰撞或搖晃時,彈簧會擺動並撞擊中心引腳,從而瞬間閉合電路。. 對於爬梯機器人而言,敲擊感應器可以偵測到伸出的機械手臂何時碰到梯級,然後啟動下一個動作。. 碰撞感測器需要安裝在撞擊點,而敲擊感測器則可以偵測整個手臂的振動。. 測試時,執行腳本,然後一手拿著整個麵包板連同敲擊感測器,用力搖晃,使其突然停止。.

在 Thonny 中建立一個名為 knock_sensor.py 的新檔案。. 將以下程式碼複製並貼上到您建立的文件中。.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
print("\nKnock Sensor")

# DESCRIPTION

# A knock sensor detects vibrations. This particular module uses a spring-based vibration switch.
# When the module is still, the spring does not touch the center pin, but when bumped or shaken,
# the spring sways and hits the center pin closing the circuit momentarily. For a ladder climbing robot, 
# a knock sensor could detect when an extending arm hits a ladder rung, and then initiate next motion.
# While a crash sensor would need to be placed at the point of impact, a knock sensor could detect 
# vibrations across an entire arm. To test, run the script, then hold the entire breadboard along with 
# the knock sensor in hand, and give it a big shake bringing it to a sudden stop.

# CONNECTIONS REQUIRED

# Connect Knock Sensor (KY-031) module 
# (labeled 'Digital Sensor' with an encased spring)
#   Pins: GND => GND, VCC => VCC, S => GP28

from time import sleep
from machine import Pin
from oled import OLED
from buzzer import Buzzer

knock_sensor = Pin(28, Pin.IN, Pin.PULL_UP)
oled = OLED(scl_pin=15, sda_pin=14)
buzzer = Buzzer(pwm_pin=16)

try:
    while True:
        oled.print("Shake...")
        
        # Sensor value == 1 => Knock detected, wait 2 seconds
        if knock_sensor.value() == 0:
            buzzer.play_tone(600)
            oled.print("Knock knock!")
            sleep(2)

except KeyboardInterrupt:
    print("\nInterrupted by user!")

finally:
    oled.reset()
    buzzer.reset()

回到頂部

讓教師和學生更容易參與動手發明創造.
© 2026 Inventor Pi, Inc.
Inventor Pi, Inc.是一家註冊的 501(c)(3) 非營利組織