Introduction
A shock switch (as shown below), also vibration switch, spring switch or shock sensor, is an electronic switch which senses vibration amplitude and transfers the signals to circuit device thus switching on the circuit. It composes of conductive vibration spring, switch, trigger pin, packaging agent and so on.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * Shock switch module
– Jumper wires
Experimental Principle
The shock switch works like this: the conductive vibration spring and trigger pin are precisely placed in the switch and fixed by adhesive. Normally, the spring and the trigger pin are separated. Once the sensor detects shock, the spring will vibrate and contact with the trigger pin, thus conducting and generating trigger signals.
With the LED attached to pin 13 already, connect the shock switch to digital pin 8. When the shock switch induces shock signals, the LED will light up.
Experimental Procedures
Step 1: Build the circuit
Shock Switch Module SunFounder Uno
S —————————————- D8
– ————————————— GND
+ —————————————- 5V
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, shake the switch module and you will see the LED attached to pin 13 on the SunFounder Uno board light up.
C Code
const int vibswPin = 8; //the Vibration Switch attach to const int ledPin = 13; //the led attach to int val = 0; //initialize the variable val as 0 /******************************************/ void setup() { pinMode(vibswPin,INPUT); //initialize vibration switch as an input pinMode(ledPin,OUTPUT); //initialize ledPin switch as an output } /*******************************************/ void loop() { val = digitalRead(vibswPin); //read the value from vibration switch if(val == HIGH) //without vibration signal { digitalWrite(ledPin,LOW); //turn off the led } else { digitalWrite(ledPin,HIGH); //turn on the led } } /*********************************************/ |