Example Code for Arduino-Vibration Triggered LED Control

Last revision 2026/01/09

This article guides you on controlling an LED using an Arduino and a vibration sensor, detailing hardware preparation, wiring, and sample code for practical implementation.

Wiring Diagram

connection diagram

Sample Code

#define SensorLED   13
//Connect the sensor to digital Pin 3 which is Interrupts 1.
#define SensorINPUT  3
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, FALLING);
}

void loop() {
  if (state != 0) {
    state = 0;
    digitalWrite(SensorLED, HIGH);
    delay(500);
  }
  else
    digitalWrite(SensorLED, LOW);
}
 
//Interrupts function 
void blink() {
    state++;
}
#define SensorLED   13
//Connect the sensor to digital Pin 3 which is Interrupts 1.
#define SensorINPUT  3
 
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, FALLING);
}

void loop() {
  if (state != 0) {
    state = 0;
    digitalWrite(SensorLED, HIGH);
    delay(500);
  }
  else
    digitalWrite(SensorLED, LOW);
}
 
//Interrupts function 
void blink() {
    state++;
}

Result

As shown in figure connection , plug LED and lay a finger on Digital Vibration Sensor , LED will be lightened.

Was this article helpful?

TOP