Arduino Projects

Shock Sensor Arduino Circuit & Code, KY-002 Shock Sensor

SHOCK SENSOR KY-002 with Arduino:

In this tutorial, I am going to show you how the shock sensor works and how we can use it with Arduino. The shock sensor SW11810P also known as vibration sensor or shake sensor which can be used in many projects for example you can do a system wake up with it when you shake the system, the system will wake up or you can use it in a toy when you shake the toy it will start making noise or even better you can use it in a security system.

Shock Sensor

It is the sensor that will be used to detect vibration. It will start an alarm and you can also use it for light switching. When the sensor detects a vibration it will switch on the light.

Inside the Shock Sensor:

Now let’s have a closer look at how this sensor works. The inside of this sensor looks like this

Shock Sensor

Inside of the sensor we have a thin metal wire spring that is around a metal conductive leg and around all this we have the outer case. The metal conductive wire is connected to five volts and around the solid wire is a springform wire, it is very thin and when there is a vibration the spring will hit the solid and then the five volts will go through the spring and then the Arduino will read different in voltage and give our signal. So now if we shake the sensor the thin metal spring and the metal conductive leg will touch each other and the current will flow from the metal conductive leg to the thin metal wire spring. We will then have a potential of 5 volts and with this, we can detect a shaking or a vibration. The shock sensor is this component on the module the shape of this sensor looks like a cylindrical capacitor in which we have a 10-kilo ohm resistor.

Shock Sensor



Now it very important to know how this resistor is connected because there are two different hardware configurations depending on which hardware configuration you have, we will wire it differently here I listed all the possible wiring configurations that you can do.

Shock Sensor

So if your resistor is between pin number two and pin number three and you want to use this resistor as a pulldown you will have to wire it like this as shown in the above figure and if you want to use this resistor as they pullup then you will have to wire it like this same logic if your resistor is between pin number 1 and pin number 2. In my case the resistor is between pin number 2 and pin number 3 and I will use it as a pulldown resistor.




Interfacing the Shock sensor with the Arduino:

So, for interfacing the sensor with Arduino, I will  perform the following steps:

  • The “s” side of the sensor will be connected with 5 volts.
  • The middle of the sensor will be connected with ground
  • And the minus side of the sensor I will use it as the outputs and connected to pin number two of the Arduino.

So, now let’s start the wiring so we need one shake sensor, two LEDs in my case I will use a green one and a red one, one kilo-ohm resistor, one breadboard, and a few jumper wires. So, now let’s connect the shock sensor to the Arduino. The pin that is on the minus side will be connected to pin number 2, the pin in the middle will be connected to the ground, and the pin on the signal side will be connected to five volts. The two LEDs will be connected to pin number 3 for the green one and pin number 4 for the red one.

Shock Sensor

Then let’s connect the USB cable to the Arduino and to the computer after this you can start the Arduino ide.


Shock Sensor Arduino Code:

int shocksensor = 2;
int greenled = 3;
int redled = 4;
bool shocksensorstate=0;
void setup() {
pinMode(shocksensor,INPUT);
pinMode(greenled,OUTPUT);
pinMode(redled,OUTPUT);

shocksensorstate=digitalRead(shocksensor);
}

void loop() {
if(shocksensorstate==1)
{
  digitalWrite(greenled,LOW);
  for(int i=0; i<10; i++)
  {
      digitalWrite(redled,HIGH);
      delay (150);
      digitalWrite(redled,LOW);
      delay (150);
      if(i==9)
      {
       shocksensorstate==0; 
        }

    }
  }
  else
  {
    shocksensorstate=digitalRead(shocksensor);
      digitalWrite(greenled,HIGH);


    }

}



Shock Sensor Arduino Code Explanation:

So, now let’s have a look at the code on the top of the code I defined the interfaces to the hardware first I set the shocksensor pin to pin number 2 the greenled pin to pin number 3, and the redled pin to pin number 4. Then I defined a variable shocksensorstate that will read the sensor state from the previous defined shock sensor pin.

Int shocksensor = 2;

int greenled = 3;

int redled = 4;

bool shocksensorstate=0;

If the sensor detects a vibration then the shocksensorstate variable will update to 1 and if the sensor does not detect any vibration the shocksensorstate will stay at 0.



In the setup where the function pin mode will be defined if the interfaces should be used as input or output. In our case I will use the shocksensor pin as an input because thesensor is sending the state of thesensor to the Arduino and the green and red led as an output because the Arduino is sending the on/off command to the LEDs with the digitalWrite function.

pinMode(shocksensor,INPUT);

pinMode(greenled,OUTPUT);

pinMode(redled,OUTPUT);

shocksensorstate=digitalRead(shocksensor);

The Arduino is reading what is coming on the shocksensor pin and store to the state to the variable shocksensorstate.

In our loop, we have if structure so if our shocksensor is equal to 1 it will turn off the green LED and enter in the for loop, in this for loop it will turn on the red LED for 150 milliseconds and turn it off for 150 milliseconds to make a blinking effect.

if(shocksensorstate==1)

{

digitalWrite(greenled,LOW);

for(int i=0; i<10; i++)

{

digitalWrite(redled,HIGH);

delay (150);

digitalWrite(redled,LOW);

delay (150);

It will do this ten times when the for loop which the last turn the variable shocksensorstate will be set to zero.

if(i==9)

{

shocksensorstate==0;

}



Again if our sensor state is not equal to one it will enter in to the else statement. It will turn on the green LED and sense the sensor state if the sensor detects a vibration the shocksensorstate is updated to one and it will enter in the if structure and then same procedure again.

else

{

shocksensorstate=digitalRead(shocksensor);

digitalWrite(greenled,HIGH);

}

After uploading code to the Arduino we could see, that when the sensor is not detecting a vibration the green LED is on and when we will shake the sensor a little bit we will see that the red LED starts blinking and after this 10 loops in the for loop, the red LED stay off and the green LED goes on.

This was just a very basic getting started tutorial explaining how to use a Shock Sensor with Arduino; it can be used in so many amazing intermediate and advanced level projects. I will definitely use this sensor to wake up the Arduino from deep sleep using an interrupt, and I will also use it to implement a security based project.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button