Built-in project · Arduino Uno R3

Knock sensor on pin 3 lights the LED on 13, on an Arduino Uno

A knock module on pin 3: a spring switch over a 10k pull-down, so each knock is a few short pulses and the LED on pin 13 flashes. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • How a spring-based vibration sensor works
  • Reading short digital pulses
  • Using pull-down resistors to define a resting state
  • Printing only when a value changes
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The knock module has a spring wrapped around a pin and a 10k resistor that holds the S output LOW. A knock sets the spring ringing, and each time it touches the pin it connects S to 5 V for a millisecond or two. The result is a few short HIGH pulses.

The sketch reads pin 3 every 10 ms and copies it to the LED on pin 13. It also prints "knock" or "still" whenever the state changes. Because the pulses are so short, the LED flickers briefly.

Parts list

3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Knock sensor, KY-031 pin GNDArduino Uno R3 pin GND
Knock sensor, KY-031 pin SArduino Uno R3 pin 3
Knock sensor, KY-031 pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Knock sensor on pin 3 lights the LED on 13.
//
//   module S -> pin 3   + -> 5V   - -> GND
//
// A spring round a pin, and a 10k from S to GND that holds S LOW. A knock
// sets the spring ringing, and each time it touches the pin it joins S to
// 5 V for a millisecond or two: a few short HIGH pulses.
//
// Press knock on the module.

const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: knock sensor on pin 3, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "knock" : "still");
  }
  delay(10);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets SENSOR (pin 3) as a plain input, since the module already has its own pull-down. Makes pin 13 an output for the LED, and starts serial at 9600 baud.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: knock sensor on pin 3, the LED on pin 13");
}

loop sketch.ino

Reads pin 3: HIGH sets now to 1. It copies now to the LED. If now differs from last, it updates last and prints "knock" or "still". A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "knock" : "still");
  }
  delay(10);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED blinks too fast to see This project
    The pulses are about a millisecond or two long. Watch the serial monitor for the "knock" lines.
  • Nothing ever happens This project
    Check S goes to pin 3, and the module has 5V and GND.
  • A single knock gives several triggers. Knock sensor
    The spring bounces on its pin and pulses S low a few times in about 30 ms. Wait 50 ms or more after the first pulse before counting another knock, or use a pin interrupt with a lockout.
  • The reading never changes or is random. Knock sensor
    Connect VCC, GND and S correctly (check the labels on the module). Read S as a digital input. It sits HIGH at rest and goes LOW on a knock, so look for the falling edge.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.