Arduino Shock/Vibration Detector Project

In this project, you will be exploring the capabilities of the HW-513 Shock/Vibration Sensor as a shock/vibration detector that may be useful in several applications, including automobile tracking devices, bicycle speedometers, pedometers, robotics, etc. Here, you will be constructing a simple shock/vibration detector that is interfaced with an Arduino and a 2-colour red/green LED module that indicates whether a vibration is detected or not. The HW-513 sensor module is unique compared to other sensors due to its specific functionality and the fact that it contains a very small percussive spring mechanism that is sensitive to sudden movements. When the module is shaken, the conductive spring within the sensor will move around, resulting in a HIGH (1) signal being sent to the Arduino and the LED module switching to a red colour. When no vibration is detected, the sensor module will send back a LOW signal (0) to the Arduino, resulting in the LED module displaying a green colour (default state). 

The code for this project may seem a bit longer than others as there are certainly a few considerations that will need to be highlighted when using this module, but all of those details will be explained in the below sections of this project. However, you should be able to pick out a few themes that you may have already come across in other projects, such as the use of if-else statements, digitalWrite and digitalRead functions. With how versatile this sensor is, you should not find much difficulty in adapting and utilizing this sensor for a wide range of other projects and with its 3-pin digital wiring interface, it can be connected to other microcontrollers with ease (e.g. Raspberry Pi, ESP8666/ESP32, MicroPython, etc. For this specific project, you need the following components, if it is a commercial activity, FS Technology can provide you with component procurement services:

  • Arduino Nano (other Arduino-compatible boards will work)
  • USB cable (compatible with the Arduino board)
  • Breadboard
  • Male-Male Jumper Wires (5)
  • HW-513 Shock/Vibration Sensor Module
  • HW-480 Red/Green 2-Colour LED Module (common cathode)
  • 220Ω resistors (2)
Vibration Detector


Depending on your Arduino board, you may or may not require a breadboard to plug your board in. In this example, an Arduino Nano is used, thus requiring a breadboard but if you are using an Arduino Uno, for example, the jumper wires can be plugged in from the components on the breadboard directly to the board pins. The wiring for this project is fairly simple as aside from the main modules, not a lot of extra components are needed. In terms of the shock/vibration sensor module, the HW-513 sensor module that is featured in this project has a simple 3-pin configuration where there is a digital output/signal (S) pin, positive (+) pin and GND (-) pin. In terms of the LED module, the reason that two 220Ω resistors are connected in series with the two outputs from this module is to prevent the LEDs from burning out when applying a supply voltage of +5 volts. A circuit diagram of the wiring is also featured below.

  • HW-513 Shock/Vibration Sensor Module: Connect the signal (S) pin to D2 on your Arduino board, the positive (+) pin to +5v and the GND (-) pin to GND.
  • HW-480 Red/Green 2-Colour LED Module: Insert one 220Ω resistor in the breadboard in series with the output pin for the red colour LED signal (R) and insert another 220Ω resistor in series with the output pin for the green colour LED signal (G). Connect the red colour output pin (R) to D3 and the green colour output pin (G) to D4. Connect the negative (-) pin to GND on your Arduino.
  • Now, you can plug in your Arduino board via the USB cable to the computer.
Vibration Detector Wiring

Project Code

					int sensorPin = 2;
int redPin = 3;
int greenPin = 4;
boolean val;
int loopcounter = 0;
long starttimer = 0;
long betweenTime = 5000;
long awake = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(sensorPin, INPUT);
attachInterrupt(0, shakeInterrupt, FALLING);
void loop() {
 digitalWrite(greenPin, HIGH);
 digitalWrite(redPin, LOW);
if(loopcounter == 30000){             
  loopcounter = 0;
else if(val == false){
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
 if(loopcounter == 30000){
  loopcounter = 0;
  awake = millis() - starttimer;
  if(awake > betweenTime){
    val = true;
void shakeInterrupt(){
val = false;
starttimer = millis();

About the code

The first two blocks of code are simply for defining important input & output pins and variables. This includes digital pin 2 (D2) for the HW-513 shock/vibration sensor, in addition to pins D3 & D4 for the red and green pins on the HW-480 2-colour LED module, respectively. In addition, a boolean variable called ‘val’ is declared that stores the digital output signal (HIGH/LOW signal) that the Arduino receives from the HW-513 sensor module. Several integer and long variables are also defined for the storage of number values, and these variables will become clearer as we look at other parts of the code.

Moving onto the void setup section, we set both LED pins as output devices and our sensor pin as an input device since digital information from the sensor is being fed into the Arduino. This is done via digitalWrite() functions. Next, serial communication is set up by setting our baud rate to 9600 bauds.

The last line of the void setup section features a special function, attachInterrupt(). Interrupts are very unique functions that can be used when dealing with timing issues. In this case, since the shock/vibration sensor is constantly feeding back information to the Arduino at a very high rate of speed, we can use an interrupt to ensure that the moment a shock/vibration is detected, it is processed by the Arduino correctly. Using an interrupt helps the microcontroller to detect these small input changes from the sensor while performing some other work also. One of the reasons why digital pin 2 (D2) was used for the HW-513 sensor was specifically due to digital pins 2 and 3 having interrupt capabilities (pins with interrupt capabilities do vary from Arduino board models). More specifically, digital pin 2 corresponds to digital pin 0 as the interrupt pin and that is the reason why a ‘0’ is seen within the argument of the attachInterrupt() function. The next part of the argument is to define when the interrupt occurs, also known as the interrupt service routine (ISR). In this code, we added another void section, called void shakeInterrupt(), to define the parameters of the ISR. If we scroll down to the bottom of the code, where the ISR we set points us to, we basically want the interrupt to set the variable ‘val’ to 0. With the function millis(), we start a stopwatch counting up the time from when the code is first running and this data is stored in the starttimer variable (as declared in the earlier sections of the code). This information will be important later. Moreover, moving back up to the original function, the last part of the argument defines the mode, or when the interrupt should be triggered. In our case, it is set to FALLING, meaning that the interrupt will trigger whenever the interrupt pin (D0 which is connected to D2) goes from high to low, or when the sensor detects a vibration to when the sensor is still.

For the void loop section, we first introduce an if-else statement which states that when the sensor returns a HIGH value (i.e. when a vibration is detected), the green LED is switched on and the red LED is switched off. This may seem counterintuitive to its working function as when the vibration sensor is shaken, a red LED is displayed, but this is all due to the presence of the interrupt function as mentioned above. Moving on, we add one to the value of the integer variable ‘loopcounter’ as defined in the early sections of the code. The purpose of ‘loopcounter’ is now revealed as the following if statement states that when the value of ‘loopcounter’ equals 3000, the digital input value of the HW-513 sensor is printed to the serial monitor and the variable ‘loopcounter’ is reset back to 0 for it to count back up again. The reason for this if statement is to ensure that the vibration sensor sends an accurate reading back to the Arduino based on a certain number of HIGH or LOW signals it receives. We definitely don’t want the vibration sensor triggering and sending back a HIGH signal due to a single faulty reading (outlier). 

At this point, we have covered the first half of the if-else statement but there is still the ‘else’ half that will now be explained. If the digital input signal from the sensor is LOW (i.e. the vibration sensor is not shaken), the red LED will be switched on and the green LED will be switched off. Again, this may seem counterintuitive but as explained above, it is inverted due to the presence of the interrupt function. The same if statement involving the variable ‘loopcounter’ then follows to ensure an accurate reading from the vibration sensor.

The last part of this if else statement deals with how long the LED stays on when a vibration is detected. As declared in the beginning, the long variable ‘awake’ is now used where it is set equal to the value of millis() minus the value of variable ‘starttimer’. This essentially means that ‘awake’ is the length of time that the vibration was detected for (i.e. the length of time from when the sensor receives a HIGH signal to when it switches back to the default LOW signal). A final if statement then states that if the length of time that the vibration was detected is greater than the long variable ‘betweenTime’, set the boolean variable ‘val’ back to 0, switching the LED back to its default state. Feel free to alter the value of the variable ‘betweenTime’ for the LED to be switched on for a longer or shorter time when a vibration is detected.

Next steps

Although you may have found the code to be a bit more complicated compared to some of FS Technology other projects, it certainly offers a lot of flexibility in terms of how you can alter and manipulate fine details of how the circuit operates. By using more if statements to filter through input data or to alter how long the LED is switched on, the beauty of such a short, yet complex code truly makes a difference when working on projects requiring greater attention to detail. Whether you are using the HW-513 shock/vibration sensor for robotic applications, automobile devices or other purposes, hopefully, you will be able to adapt the concepts from this code to your particular usage. 

DIY Electronics Project Blog