Skip to main content

How to use a Vibration Sensor



Vibration / Shake Switch


Introduction 

In this tutorial we will use one vibration sensor or shake switch to make a beep sound from a buzzer while we shake our breadboard.
Picture

What you will need - Hardware

For this tutorial you will need:
  • Arduino uno
  • Breadboard
  • Vibration/Shake switch
  • Buzzer

The Circuit

Ardumotive Tutorial - Arduino Shake Vibration Sensor
The connections are pretty easy.
  • Connect one pin of vibration sensor to Arduino Analog pin A0 and the other to 5V pin.
  • Now connect the buzzer, one pin to Arduino pin 8 and the other to GND.

The code :

​Here's the code
*********************************************************************

/* Vibration Sensor (Shake Switch) - Testing with buzzer

   In this tutorial we will use one vibration sensor (or shake switch) 
   to make a beep sound from a buzzer while we shake our breadboard.
     */
   
const int buzzer = 8; //Buzzer connected to pin 8 of Arduino uno / mega
int sensor;           //Variable to store analog value (0-1023)
void setup()
{
Serial.begin(9600);      //Only for debugging
pinMode(buzzer, OUTPUT);
}

void loop()
{
sensor = analogRead(A0);
//While sensor is not moving, analog pin receive 1023~1024 value
if (sensor<1022){
tone(buzzer, 500);
Serial.print("Sensor Value: ");
Serial.println(sensor);
}else{ 
noTone(buzzer);
Serial.print("Sensor Value: ");
Serial.println(sensor);
}
delay(100); //Small delay
}
*********************************************************************

Well done!

Picture
You have successfully completed one more "How to" tutorial and you learned how to use a vibration (or shake) sensor with Arduino.

I hope you liked this, let me know in the comments!

Comments

Popular posts from this blog

Build Your Own Arduino Weather Station

Arduino ​Obstacle Avoiding Robot Ardunio Computer

Complete Guide for Nokia 5110 LCD with Arduino