KY-002 Spring Vibration & Impact Shock Sensor
Capture mechanical impacts, table knocks, and tamper vibrations using hardware interrupts on a spring-contact switch.
Detect knocks, earthquakes, machine vibrations, and package impacts with the KY-002 vibration switch module! Because spring-contact closures last less than 1 millisecond, simple polling loops miss hits—so you will wire KY-002 to Arduino Uno hardware interrupt pin D2 (INT0) for 100% impact capture.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerThe KY-002 contains a microscopic conductive spring around a center pin plus an onboard 10kΩ pull-up resistor, consuming <0.5mA on Arduino Uno 5V. Interrupt-driven edge capture guarantees sub-millisecond shock pulses are never lost while Serial logs are printing.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P with INT0 on D2 | 1 | $8.00 |
| KY-002 Vibration Switch Module | SW-18015P conductive spring cylinder + 10kΩ pull-up | 1 | $1.00 |
| 5mm LED + 220Ω Resistor + Buzzer | Visual & audible shock alert stage | 1 | $0.95 |
| Breadboard & Jumper Wires | 6× breadboard jumpers | 1 | $2.00 |
Electrical & System Architecture
Connect KY-002 Middle (+) to 5V, (-) to GND, and Signal (S) to Arduino interrupt pin D2 (INT0). Wire Shock Alert LED (+220Ω) to D8 and optional Buzzer to D9.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling KY-002 SW-18015P Spring Vibration Module acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Inspect KY-002 Spring Capsule & Pinout
Verify the 3-pin layout on KY-002: S (Signal), center (+5V), and - (GND).
- Seat the KY-002 rigidly onto the breadboard so tabletop knocks transmit cleanly into the metallic cylinder.
- Verify silkscreen labels: S, middle (+), and (-).
- Note that the spring switch is normally OPEN (HIGH via pull-up) and shorts to LOW briefly during vibration.
Wire Signal to Hardware Interrupt Pin D2
Connect KY-002 to D2 (INT0), plus Alert LED to D8 and optional Buzzer to D9.
- Connect KY-002 center (+) → 5V, (-) → GND, and S → D2.
- Wire Red LED (+220Ω) to D8 and Piezo Buzzer to D9.
- Plug in USB power.
Verify Microsecond Contact Capture
Flick the breadboard edge lightly with a fingernail and confirm every shock fires INT0.
- Upload the diagnostic snippet and open Serial Monitor.
- Tap the desk firmly beside the breadboard—confirm instant interrupt counts print.
- Notice how reliable INT0 capture is compared to polling loops!
Upload Tamper & Impact Classifier Firmware
Flash the smart shock analyzer that differentiates gentle bumps from sustained tampering.
- Upload the production sketch below.
- Give a single light tap → Serial logs "Minor Bump" and flashes D8 briefly.
- Shake the breadboard briskly → Serial logs "TAMPER ALARM!" and sounds D9 buzzer.
Test Orientation & Sensitivity
Observe how horizontal vs. vertical orientation affects radial spring sensitivity.
- Tap vertically vs horizontally along the cylinder axis.
- Notice the spring flexes most easily perpendicular to the cylinder barrel.
- Tune TAMPER_HITS (default 3 hits/sec) for your application.
Subsystem Benchtop Verification Scripts
Test each hardware subsystem independently before deploying the full autonomous firmware.
1Bench Diagnostic: KY-002 SW-18015P Spring Vibration Module Hardware Check
Subsystem DiagnosticMinimal standalone sketch to verify pin wiring and signal response for KY-002 SW-18015P Spring Vibration Module before running the full project firmware.
// Diagnostic: Interrupt-Driven KY-002 Shock Counter
volatile unsigned long hits = 0;
void isrShock() { hits++; }
void setup() { pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), isrShock, FALLING); Serial.begin(115200); }
void loop() { static unsigned long last=0; if (hits != last) { last = hits; Serial.print("Shock Pulse #"); Serial.println(last); } }Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* Project 32: KY-002 Interrupt-Driven Vibration & Tamper Detector
* Board: Arduino Uno R3 | Pins: D2 (KY-002 INT0), D8 (LED), D9 (Buzzer)
*/
const uint8_t VIB_PIN = 2;
const uint8_t ALERT_LED = 8;
const uint8_t BUZZER_PIN = 9;
const uint8_t TAMPER_HITS = 3; // >=3 shocks within 1s = Tamper Alert
volatile uint16_t windowHits = 0;
volatile unsigned long lastIsrMs = 0;
void onVibration() {
unsigned long now = millis();
if (now - lastIsrMs >= 15) { // 15ms spring bounce filter
lastIsrMs = now;
windowHits++;
}
}
void setup() {
Serial.begin(115200);
pinMode(VIB_PIN, INPUT_PULLUP);
pinMode(ALERT_LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(VIB_PIN), onVibration, FALLING);
Serial.println(F("KY-002 Vibration & Tamper Monitor Armed"));
}
void loop() {
delay(800);
noInterrupts();
uint16_t count = windowHits;
windowHits = 0;
interrupts();
if (count >= TAMPER_HITS) {
Serial.print(F("[TAMPER ALARM!] Heavy Vibration (")); Serial.print(count); Serial.println(F(" hits)"));
digitalWrite(ALERT_LED, HIGH);
tone(BUZZER_PIN, 2000, 300);
delay(350);
digitalWrite(ALERT_LED, LOW);
} else if (count > 0) {
Serial.print(F("[BUMP] Minor shock detected (")); Serial.print(count); Serial.println(F(" hit)"));
digitalWrite(ALERT_LED, HIGH); delay(120); digitalWrite(ALERT_LED, LOW);
}
}Firmware Architecture Explanation:
- Captures sub-millisecond spring closures asynchronously via INT0 even while loop() sleeps.
- Atomically snapshots and resets windowHits inside noInterrupts()/interrupts() critical section.
- Separates benign single bumps from multi-hit tampering to eliminate false alarms.
Operational Verification & Pin State Matrix
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| 0 Hits / 0.8s | D2 Idle HIGH | Stationary | Armed Standby |
| 1–2 Hits / 0.8s | Brief INT0 | Light Knock / Bump | Short D8 LED Flash |
| ≥ 3 Hits / 0.8s | Rapid INT0 | Tamper / Shake | D8 LED + 2kHz Alarm |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| No response even when shaking vigorously | Wired to non-interrupt pin or loose S wire | Verify S connects strictly to D2 | Connect KY-002 S → D2 and confirm INPUT_PULLUP. |
| Triggers nonstop without touching | Defective shorted spring inside cylinder | Measure continuity across cylinder leads at rest | Tap gently to unstick spring or replace KY-002. |
| Only very hard impacts register | SW-18015P mounted on soft foam absorbing energy | Check mechanical coupling to test surface | Mount PCB rigidly to surface being monitored. |
Engineering Challenges & Upgrades
Take your build to the next level with these progressive engineering upgrades:
Record time gaps between 3 knocks and unlock Project 8 Servo only when your secret rhythm matches!
Monitor washing machine vibration and chime when vibration stops for 2 continuous minutes.
Combine with DS3231 RTC (Project 18) to timestamp peak shock counts.