Logo

Interfacing Shock Sensor with ESP32

What is a Shock Sensor?

A Shock Sensor is a vibration-detecting sensor that generates a digital signal when it detects sudden movement, shock, or vibration. It is often used in alarm systems, impact detection, and safety devices.

Working Principle of Shock Sensor

The sensor contains a spring or conductive element that closes or opens a circuit when a vibration or mechanical shock is detected, generating a high or low pulse on the output pin.

  • The ESP32 sends a HIGH signal to power the sensor.
  • When the sensor detects a shock or vibration, it triggers a digital HIGH/LOW pulse.
  • ESP32 reads the digital signal and performs actions like triggering alarms or logging data.

Formula: Shock Detected = Digital Read (HIGH or LOW)

Components Required

  • ESP32 board
  • Shock Sensor Module (SW-420 or equivalent)
  • Jumper wires
  • Breadboard (optional)

Pin Configuration of Shock Sensor Module

  • VCC: Connects to 3.3V of ESP32
  • GND: Connects to ESP32 GND
  • DO (Digital Out): Sends digital signal to ESP32 when vibration is detected

Make sure to use a sensor module that supports 3.3V logic or use a level shifter if necessary.

Wiring Shock Sensor to ESP32

  • VCC -> 3.3V (or 5V if supported)
  • GND -> GND
  • DO -> GPIO 15

Arduino Code for ESP32 + Shock Sensor

1const int shockPin = 15;
2
3void setup() {
4  pinMode(shockPin, INPUT);
5  Serial.begin(115200);
6}
7
8void loop() {
9  int shockState = digitalRead(shockPin);
10  if (shockState == HIGH) {
11    Serial.println("Shock Detected!");
12  } else {
13    Serial.println("No Shock");
14  }
15  delay(500);
16}

Code Explanation (Line-by-Line)

  • const int shockPin = 15;: Defines GPIO 15 as the input pin connected to the shock sensor.
  • pinMode(shockPin, INPUT);: Sets the shock sensor pin as an input.
  • digitalRead(shockPin);: Reads the sensor’s digital signal (HIGH or LOW).
  • Serial.println("Shock Detected!");: Prints a message when a shock is detected.

Applications

  • Anti-theft alarm systems
  • Crash or impact detection in vehicles
  • Smart door knock detection
  • Robotics obstacle or collision alert
  • Earthquake early-warning systems (basic)

Conclusion

The Shock Sensor is a simple yet effective module for detecting vibrations and mechanical shocks using ESP32. It finds use in security and automation applications where detecting motion or impact is crucial.