Created on: 14 December 2017
How to use the shock switch 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 shock sensor module with Arduino. An Arduino sketch shows how to read the shock sensor to determine if it has registered a shock after being tapped or knocked.
Note that there are two different pinouts for the shock switch sensor module, depending on which kit they are from. See the shock switch sensor module pinout for more details.
The image below shows the shock switch sensor module used in this tutorial.
Important:
Before continuing, refer to the shock switch sensor module pinout to determine the configuration of your shock sensor.
There are two different shock sensors and two different ways that each can be configured.
The shock 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 Arduino sketch or program is required for each different wiring configuration.
Below are two circuits that can be used with the Geekcreit shock switch 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:
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.
If your shock 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:
Choose one of the following sketches, depending on if you wired the shock sensor module to use the resistor as a pull-down or pull-up.
Both sketches monitor the pin that the shock module is connected to. If a shock is detected (when someone knocks or taps the sensor), the Arduino on-board LED is switched on for two seconds.
Use this sketch with the pull-down resistor circuit. SENSE in the circuit diagram connects to SHOCK_PIN defined in the sketch (defaulted to pin 2 in the sketch).
// Sketch for shock switch sensor in pull-down resistor configuration // Pinout: https://startingelectronics.org/pinout/shock-sensor/ // Tutorial: https://startingelectronics.org/tutorials/arduino/modules/shock-sensor/ // Change pin number that the shock sensor is connected to here #define SHOCK_PIN 2 void setup() { pinMode(LED_BUILTIN, OUTPUT); // on-board LED, usually pin 13 pinMode(SHOCK_PIN, INPUT); // shock sensor pin set to input } void loop() { if (digitalRead(SHOCK_PIN)) { // shock detected? // shock detected with pull-down resistor digitalWrite(LED_BUILTIN, HIGH); // switch LED on delay(2000); // leave LED on for period } else { // shock not detected with pull-down resistor digitalWrite(LED_BUILTIN, LOW); // switch LED off } }
Use the sketch below with the pull-up resistor circuit. SENSE in the circuit diagram connects to SHOCK_PIN defined in the sketch (defaulted to pin 2 in the sketch).
// Sketch for shock sensor in pull-up resistor configuration // Pinout: https://startingelectronics.org/pinout/shock-sensor/ // Tutorial: https://startingelectronics.org/tutorials/arduino/modules/shock-sensor/ // Change pin number that the shock sensor is connected to here #define SHOCK_PIN 2 void setup() { pinMode(LED_BUILTIN, OUTPUT); // on-board LED, usually pin 13 pinMode(SHOCK_PIN, INPUT); // shock sensor pin set to input } void loop() { if (digitalRead(SHOCK_PIN)) { // shock detected? // shock not detected with pull-up resistor digitalWrite(LED_BUILTIN, LOW); // switch LED off } else { // shock detected with pull-up resistor digitalWrite(LED_BUILTIN, HIGH); // switch LED on delay(2000); // leave LED on for period } }