Lesson 14 Vibration Switch

Share for us

Introduction

A vibration switch, also called spring switch or shock sensor, is an electronic switch which induces shock force and transfers the result to a circuit device thus triggering it to work. It contains the following parts: conductive vibration spring, switch body, trigger pin, and packaging agent.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Vibration switch module

– Jumper wires

Experimental Principle

In a vibration switch module, the conductive vibration spring and trigger pin are precisely placed in the switch body and bond to curing position through adhesive. Normally, the spring and the trigger pin do not contact. Once shook, the spring will shake and contact with trigger pin to conduct and generate trigger signals.

With the LED attached to pin 13, connect the vibration switch to digital pin 8. When the vibration switch inducts vibration signals, pin SIG will output low level, and the LED on the module and the LED attached to pin 13 will light up. The schematic diagram of the vibration switch module is shown as follows.

Experimental Procedures

Step 1: Build the circuit

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, shake the switch module, and you should see the LED attached to pin 13 on SunFounder Uno board and that on the module light up.

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
 //Serial.begin(9600);
}
/*******************************************/
void loop()
{
  val = digitalRead(vibswPin); //read the value from vibration switch
  //Serial.println(val);
  if(val == LOW) //without vibration signal
  {
    digitalWrite(ledPin,HIGH); //turn off the led
    delay(500);//delay 500ms,The LED will be on for 500ms
  }
  else
  {
    digitalWrite(ledPin,LOW); //turn on the led
  }
}
/*********************************************/