Knock Sensor Module Arduino Tutorial

Created on: 13 December 2017

How to use the knock sensor from the 37 in 1 sensor kit for Arduino from Geekcreit, Elegoo, Elektor and others.

This tutorial shows the basic use and testing of the knock sensor module with Arduino. An Arduino sketch shows how to read the knock sensor to determine if it has been knocked or tapped.

Note that there are two different pinouts for the knock sensor module, depending on which kit they are from. See the knock sensor module pinout for more details.

The image below shows the knock sensor module used in this tutorial.

Geekcreit Knock Sensor Module
Geekcreit Knock Sensor Module

Knock Sensor Module Circuit

Important:

Before continuing, refer to the knock sensor module pinout to determine the configuration of your knock sensor.

There are two different knock sensors and two different ways that each can be configured.

The knock sensor module can be wired to the Arduino using the 10k resistor on the module as either a pull-down or pull-up resistor. A different sketch is required for each different wiring configuration.

Geekcreit Knock Sensor Module Circuit

Below are two circuits that can be used with the Geekcreit knock sensor module. It is recommended to check your module with a multimeter to make sure which pins the 10k resistor (R1) on the module is connected to.

Circuit Connections to Arduino:

  • +5V connects to the Arduino 5V pin.
  • GND connects to the Arduino GND pin.
  • SENSE connects to an Arduino digital input pin. Arduino pin 2 is used in the sketches below, but can be changed.

The circuit on the left is used with the pull-down resistor Arduino sketch below. The circuit on the right is used with the pull-up resistor Arduino sketch below.

Geekcreit Knock Sensor Module Circuit
Geekcreit Knock Sensor Module Circuit

Alternate Knock Sensor Module Circuit

If your knock sensor is configured with the 10k resistor connected between pins 1 and 2 of the module, then choose one of the following circuits.

Circuit Connections to Arduino:

  • +5V connects to the Arduino 5V pin.
  • GND connects to the Arduino GND pin.
  • SENSE connects to an Arduino digital input pin. Arduino pin 2 is used in the sketches below, but can be changed.
Alternate Knock Sensor Module Circuit
Alternate Knock Sensor Module Circuit

Knock Sensor Module Arduino Sketch

Choose one of the following sketches, depending on if you wired the knock sensor module to use the resistor as a pull-down or pull-up.

Both sketches monitor the pin that the knock module is connected to. If a knock is detected (when someone knocks or taps the sensor), the Arduino on-board LED is switched on for two seconds.

Pull-down Resistor Knock Sensor Module Arduino Sketch

Use this sketch with the pull-down resistor circuit. SENSE in the circuit diagram connects to KNOCK_PIN defined in the sketch (defaulted to pin 2 in the sketch).

// Sketch for knock sensor in pull-down resistor configuration
// Pinout:   https://startingelectronics.org/pinout/knock-sensor/
// Tutorial: https://startingelectronics.org/tutorials/arduino/modules/knock-sensor/
// Change pin number that knock sensor is connected to here
#define 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
  }
}

Pull-up Resistor Knock Sensor Module Arduino Sketch

Use the sketch below with the pull-up resistor circuit. SENSE in the circuit diagram connects to KNOCK_PIN defined in the sketch (defaulted to pin 2 in the sketch).

// Sketch for knock sensor in pull-up resistor configuration
// Pinout:   https://startingelectronics.org/pinout/knock-sensor/
// Tutorial: https://startingelectronics.org/tutorials/arduino/modules/knock-sensor/
// Change pin number that knock sensor is connected to here
#define 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 not detected with pull-up resistor
    digitalWrite(LED_BUILTIN, LOW);   // switch LED off
  }
  else {
    // knock detected with pull-up resistor
    digitalWrite(LED_BUILTIN, HIGH);  // switch LED on
    delay(2000);                      // leave LED on for period
  }
}