A knock sensor detects vibrations or sudden impacts and can be used for applications such as security systems, interactive projects, or automation. In this tutorial, we will interface a knock sensor with an Arduino and control an onboard LED based on the sensor's input.

 

Components Required

 

About the Components

Knock Sensor

The Knock Sensor is a vibration-detecting sensor offering:

  • Detection method: Piezoelectric sensor for detecting mechanical knocks.
  • Output signal: Digital (High/Low).
  • Supply voltage: 3.3V - 5V.
  • Application areas: Security systems, interactive projects, and engine diagnostics.

 

Arduino KY-031 Knock Sensor Circuit Diagram

Connections 

  • Knock Sensor:

    • Signal Pin → Arduino D3

    • VCC → 5V

    • GND → GND

  • LED(Optional, since arduino has built-in LED):

    • Positive Leg (+) → D13

    • Negative Leg (-) → GND

  • LCD (I2C 16x2):

    • SDA → A4 (Arduino SDA)

    • SCL → A5 (Arduino SCL)

    • VCC → 5V

    • GND → GND

 

Arduino KY-031 Knock Sensor Code Explanation

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

  • #include <Wire.h>: This library enables I2C communication, which is required for the LiquidCrystal_I2C library to function.
  • #include <LiquidCrystal_I2C.h>: This library is used to control the 16x2 LCD with an I2C interface, reducing the number of pins required to control the display.

int Led = 13;  // LED on Arduino board
int Shock = 3; // Sensor signal pin
int val;       // Variable to store sensor status
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD1602 with I2C address 0x27

  • Led = 13 → The built-in LED on pin 13 will indicate when a knock is detected.
  • Shock = 3 → The shock sensor is connected to digital pin 3 and will send a HIGH or LOW signal based on detection.
  • val → This variable will store the digital reading from the shock sensor.
  • LiquidCrystal_I2C lcd(0x27, 16, 2); → The I2C address of the LCD is 0x27, and the LCD size is 16x2 characters.

void setup()
{
  lcd.init();  // Initialize the LCD
  lcd.backlight();
  pinMode(Led, OUTPUT);  // Define LED as output
  pinMode(Shock, INPUT); // Define Shock sensor as input
  delay(1000);           // 1-second delay for stabilization
  lcd.clear();           // Clear the LCD screen
}

  • lcd.init(); → Initializes the LCD.
  • lcd.backlight(); → Turns ON the LCD backlight for visibility.
  • pinMode(Led, OUTPUT); → Configures the LED pin as an output.
  • pinMode(Shock, INPUT); → Configures the shock sensor pin as an input.
  • delay(1000); → Waits 1 second to ensure that the system is stable before starting.
  • lcd.clear(); → Clears any previous display from the LCD.

void loop()
{
  val = digitalRead(Shock); // Read sensor value from pin 3
  lcd.clear();  // Clear the LCD to avoid text overlap

  • val = digitalRead(Shock); → Reads the digital state (HIGH/LOW) from the shock sensor.
  • lcd.clear(); → Clears the LCD before printing new text to prevent overlapping characters.

  if (val == HIGH) // When sensor does not detect a knock
  {
    digitalWrite(Led, HIGH); // LED turns ON

    lcd.setCursor(0, 0);

    lcd.print("knock detected "); // Print "Knock detected" on LCD
  }
  else
  {
    digitalWrite(Led, LOW); // LED turns OFF

    lcd.setCursor(0, 0);

    lcd.print("No knock       "); 
  }
  
  delay(500); // Short delay of 500ms before repeating the loop
}

  • If val == HIGH (Knock Detected)

    • The sensor detects a knock or shock.
    • The LED turns ON (digitalWrite(Led, HIGH);).
    • The LCD displays "knock detected".

     

  • else (no Knock)

    • The sensor is idle (not detecting a knock).
    • The LED remains OFF (digitalWrite(Led, LOW);).
    • The LCD displays "No knock".
  • delay(500); → A 500ms delay prevents excessive LCD flickering.


Result

The implemented system successfully detects knocks using a shock sensor and provides real-time feedback through an LCD display and an LED indicator. The 16x2 I2C LCD module effectively displays the sensor status, ensuring clear visibility of the system's response.

 Interfacing KY-031 Knock Sensor with Arduino NanoArduino KY-031 Knock Sensor Interfacing Tutorial


Arduino KY-031 Knock Sensor Code 

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

int Led = 13;
int Shock = 3;
int val;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
    lcd.init();
    lcd.backlight();
    pinMode(Led, OUTPUT);
    pinMode(Shock, INPUT);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("No knock"); // Initial message
}

void loop() {
    val = digitalRead(Shock); // Read sensor value

    if (val == HIGH) {  // Change to LOW if sensor logic is inverted
        digitalWrite(Led, HIGH);
        lcd.setCursor(0, 0);
        lcd.print("Knock detected ");
    } else {
        digitalWrite(Led, LOW);
        lcd.setCursor(0, 0);
        lcd.print("No knock           "); 
    }

    delay(500); // Small delay to stabilize display
}

 

Video:

Leave a comment

Please note, comments must be approved before they are published

Your cart

×