Home ┬╗ Arduino and KY-031 Knock Sensor

Arduino and KY-031 Knock Sensor

by arduinoguy71

In this article, we connect an KY-031 Knock Sensor to an Arduino Uno

The KY-031 Knock Sensor module is a vibration sensor that sends a signal when a knock/tap is detected on the module.

This module consists of a spring-based sensor, a 10K resistor and a header. The spring sends a high signal when a vibration is detected.

 

Parts Required

You can connect to the module using dupont style jumper wire.

This should work with other Arduino board – I have tried an Uno and Mega

Name Link
Arduino Uno
37 in one sensor kit
Connecting cables

 

Schematic/Connection

ARDUINO SENSOR
Pin 2 Signal
5 V +V
GND GND

 

Code Example

The following code example will switch the onboard LED the Arduino when the module is tapped

int LedPin = 13 ;// define LED Interface
int ShockPin = 2; // define the percussion Sensor Interface
int val ;// define numeric variables val

void setup ()
{
  pinMode (LedPin, OUTPUT) ;// define LED as output interface
  pinMode (ShockPin, INPUT) ;// define knock sensor output interface
}

void loop ()
{
  val = digitalRead (ShockPin) ;// read ShockPin
  if (val == HIGH) // When the knock switch is triggered the LED flashes
  {
    digitalWrite (LedPin, LOW);
  }
  else
  {
    digitalWrite (LedPin, HIGH);
  }
}

 

Serial Monitor Output

N/A

Links

https://github.com/getelectronics/ArduinoCode/tree/main/37%20Sensor%20Kit/KY-031_Knock_Sensor

 

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.