What is Shock Sensor??
We have seen shock sensors or shake sensors in your day-to-day
life in toys, electronic gadgets, security systems, etc. shock sensors are
basically used to wake a device for example – you have a bicycle in which you have
attached a shock sensor and a LED light. At the stationary position LED light will be
turned OFF but when you shake the bicycle, the shock sensor will turn ON resulting
in turn ON led lights which indicate awake of the system from sleep mode.
How do Shock Sensors Work??
Another example can be toys, when you shake the toys it
will generate a sound.
The shock sensor consists of SW-18015p which looks like a cylindrical
shape on the module.
The shock sensor consists of a metallic wire and a metallic terminal. The metallic terminal is connected to 5v, this metallic terminal is surrounded by a spring formed by a metal wire. When there is a vibration or when we shake the sensor the metal terminal hits the metal wire and current from the metallic terminal now flows to the metal wire. So the output of the sensor will be 5v to the Arduino board.
The shock sensor has a pull-down
resistor configuration and a pull-up resistor configuration. In the pull-down
configuration sensor by default sends a LOW signal and changes its State to HIGH
when a knock is detected. Whereas in pull-up configuration sensor by default sends a HIGH signal and changes its State to LOW when vibration or shake is detected.
Parts Required
- KY-002 (shock sensor module)
- Arduino
- Jumper wires
- LED
- Breadboard
Schematic Diagram
Connect the ground pin of the shock sensor to the ground pin of the Arduino, the VCC pin to 5v, and the Signal pin to 8. Also, connect the positive lead of LED to Arduino pin 11.
int shocksensor=8; // Set sensor pin at Arduino pin 8
int sensorvalue; // initialize variable to store sensor data
void setup()
{
pinMode(ledpin,OUTPUT); // LED pin set to output
pinMode(shocksensor,INPUT); //shock sensor pin set to input
Serial.begin(115200); // set baud rate for Serial communication
}
void loop()
{
sensorvalue = digitalRead(shocksensor); // read values from sensor pin and store in variable
if (sensorvalue==HIGH) // if shake detected
// if (sensorvalue==LOW) // for pull-up configuration
{
digitalWrite(ledpin,HIGH); // turn LED on
Serial.println("vibration detected system awake"); // Print message
}
else{
digitalWrite(ledpin,LOW); // turn LED off
Serial.println("Sleep mode"); // Print message
}
delay(20); // delay
}
First, initialize pins of shock sensor & LED. We have declared a variable name sensor value to read values from the shock sensor. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values from the sensor pin so we will declare it as INPUT and LED as OUTPUT. We also configure the baud rate to 115200.
In a void loop, we continuously read values from the shock sensor and
store values in the variable name sensor value and we check here condition if it is
HIGH or not. HIGH means vibration or shake has been detected, the sensor sends a HIGH
signal to our Arduino which we printed as "vibration detected system
awake" Here we have written code to turn ON led when vibration is
detected. Whereas when the sensor is in pull-up configuration then the sensor will send a Low signal in the presence of vibration or shake.
When the sensor doesn’t sense a shake or vibration then the sensor sends a LOW signal to Arduino which is printed as “Sleep mode “ and the LED connected to pin 11 will turn OFF. Whereas when the sensor is in pull-up configuration, the sensor will send a HIGH signal. We have introduced a delay of 20 milliseconds.
Conclusion -
"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."