Example Code for Arduino-Detecting vibration
Last revision 2026/01/08
This article presents a practical guide for setting up Arduino to detect vibrations, including necessary hardware and software preparations, wiring diagrams, and sample code for implementing a vibration sensor connected to an LED.
Wiring Diagram

Sample Code
/*
Detecting vibration
*/
int SensorLED = 13; //LED PIN
int SensorINPUT = 3; //Connect the sensor to digital Pin 3 which is Interrupts 1
unsigned char state = 0;
void setup() {
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT);
// Trigger the blink function when the falling edge is detected
attachInterrupt(1, blink, RISING);
}
void loop(){
if(state!=0){
state = 0;
digitalWrite(SensorLED,HIGH);
delay(500);
}
else
digitalWrite(SensorLED,LOW);
}
void blink(){ //Interrupts function
state++;
}
Was this article helpful?
