具体讲解
爆震传感器模块的基本用法和测试。草图显示了如何读取爆震传感器以确定其是否已被敲击或敲击。
电路连接
- 5V连接到Arduino 5V引脚。
GND连接到Arduino GND引脚。
SENSE连接到Arduino数字输入引脚。代码实现
如果检测到敲击声(有人敲击或敲击传感器),则Arduino板载LED会亮起两秒钟。
```javascriptdefine KNOCK_PIN 2
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // on-board LED, usually pin 13
pinMode(KNOCK_PIN, INPUT); // knock sensor pin set to input
}
void loop() {
if (digitalRead(KNOCK_PIN)) { // knock detected?
// knock detected with pull-down resistor
digitalWrite(LED_BUILTIN, HIGH); // switch LED on
delay(2000); // leave LED on for period
}
else {
// knock not detected with pull-down resistor
digitalWrite(LED_BUILTIN, LOW); // switch LED off
}
}
```