Reading the KY-031 Knock Switch with Arduino

Reading the KY-031 Knock Switch with Arduino

The KY-031 does the same basic job as the raw piezo sensor we covered before — detecting a knock or shock — but takes a completely different approach to get there, using a spring-loaded mechanical switch instead of a piezoelectric disc read through an analog threshold.

Commercial KY-031 boards vary. Confirm active polarity, use INPUT_PULLUP only when the circuit supports it, and count a validated edge rather than every bouncing sample.

How it works

Inside the module, a small internal switch stays open under normal conditions, keeping the output HIGH. A knock or vibration strong enough to move the spring closes that switch momentarily, pulling the output LOW for an instant before it returns to its resting state.

Components needed

An Arduino Uno, a KY-031 knock sensor module, and jumper wires.

Wiring it up

The module needs just three connections: VCC to 5V, GND to ground, and its digital signal output to a digital pin — pin 2 in the typical setup.

Programming it

Because the sensor's output is a clean digital signal rather than a noisy analog one, the sketch only needs a simple digitalRead() check: when the pin reads LOW, it prints "Knock detected!" A short delay after each detection helps avoid registering a single knock as several rapid triggers.

How this compares to our earlier piezo sensor project

Our earlier piezoelectric sensor project read a raw analog signal and compared it against a tunable threshold to decide what counted as a knock. The KY-031 skips that tuning step entirely — its built-in mechanical switch already does the thresholding internally, giving a simple digital HIGH/LOW result that's easier to wire into a project but offers less fine control over sensitivity than the raw piezo approach.

Where this is used

This kind of impact-triggered switch fits vehicle security systems, tamper alarms, simple robot bump-detection, and any project needing a straightforward "something just got hit" signal.

You can find KY-031 knock sensor modules at Juthour Tech.

اترك تعليقا

*
*
يمكن للأعضاء المسجلين فقط ترك تعليقات.