Вернуться на страницу библиотеки модулей

/*
  KY-031 Knock Sensor Module - Arduino Example
  Blinks the on-board LED and prints a message to the Serial Monitor each
  time a knock or tap is detected. A short debounce lockout stops the
  sensor's spring from registering one knock as several.
*/

const int knockPin = 3;            // KY-031 signal (S) pin
const int ledPin   = LED_BUILTIN;  // on-board LED (pin 13 on the Uno)

// Most KY-031 boards pulse HIGH on a knock. If yours triggers constantly,
// it is an active-LOW board - change this to LOW.
const bool KNOCK_ACTIVE = HIGH;

const unsigned long debounceMs = 250;  // ignore further knocks for this long
unsigned long lastKnock = 0;

void setup() {
  pinMode(knockPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("KY-031 knock sensor ready");
}

void loop() {
  if (digitalRead(knockPin) == KNOCK_ACTIVE &&
      millis() - lastKnock > debounceMs) {
    lastKnock = millis();
    Serial.println("Knock detected");
    digitalWrite(ledPin, HIGH);  // flash the LED so the knock is visible
    delay(60);
    digitalWrite(ledPin, LOW);
  }
}

Рекомендуемые ссылки:
Arduino Knock Sensor Module KY-031 — Wiring & Code
KY-031 Knock sensor – SensorKit
Using the Knock Sensor Module KY-031 with Arduino – Phipps Electronics
Interfacing KY-031 Knock Sensor with Arduino – QuartzComponents
Arduino Projects – Fall Collision Detection using KY-031 Knock Sensor

Вернуться на страницу библиотеки модулей