Arduino Vibration Sensor Project

A vibration sensor is a vital component used to measure an object’s vibration frequency, amplitude, and acceleration. Its applications are extensive, including but not limited to vibration alerts in gaming devices, anti-theft alarms in security systems, and wheel balancing and collision detection in automotive electronics. In this article, we will combine a vibration sensor, a ball switch, and an LED to create an Arduino project aimed at indicating situations of tilt or collision with the LED. When these events occur, the LED will illuminate to alert the user, and it will turn off when the event ends.

vibration sensor arduino

Table of Contents

Required Components and Wiring

  • 1 × Arduino UNO R3 (along with a compatible USB data cable)
  • 1 × Breadboard
  • 4 × Colorful jumper wires
  • 1 × 5mm LED light
  • 1 × 220-ohm resistor
  • 1 × Ball switch SW200D

Vibration sensor arduino wiring

Arduino Vibration Sensor Project Code

				
					int SensorLED = 10;       // Define LED as digital pin 10
int SensorINPUT = 3;      // Connect vibration switch to interrupt 1, which is digital pin 3
unsigned char state = 0;
void setup() {
  pinMode(SensorLED, OUTPUT);         // Set LED as an output
  pinMode(SensorINPUT, INPUT);        // Set the vibration switch as an input
  // When transitioning from a low level to a high level, trigger interrupt 1, and call the blink function
  attachInterrupt(1, blink, RISING);
}
void loop() {
  if (state != 0) {              // If state is not 0
    state = 0;               // Set state to 0
    digitalWrite(SensorLED, HIGH);   // Turn on the LED
    delay(500);          // Delay for 500ms
  } else {
    digitalWrite(SensorLED, LOW);     // Otherwise, turn off the LED
  }
}
void blink() {                // Interrupt function blink()
  state++;             // Once the interrupt is triggered, continuously increment the state
}

				
			

Code Explanation

int SensorLED = 10; and int SensorINPUT = 3;: These lines define two integer variables, SensorLED and SensorINPUT. SensorLED is set to 10, which corresponds to digital pin 10, and SensorINPUT is set to 3, which corresponds to digital pin 3.

unsigned char state = 0;: This line declares an unsigned character variable called state and initializes it to 0. This variable will be used to keep track of the LED’s state.

void setup() { … }: The setup function is executed once when the Arduino starts. It’s used for initialization.

  • pinMode(SensorLED, OUTPUT);: This line sets the SensorLED (digital pin 10) as an output, indicating that it will control the LED.
  • pinMode(SensorINPUT, INPUT);: This line sets the SensorINPUT (digital pin 3) as an input, indicating that it’s connected to the vibration switch.
  • attachInterrupt(1, blink, RISING);: This line configures an interrupt. It specifies that when there’s a rising edge (transition from low to high) on digital pin 3 (interrupt 1), it should call the blink function. This is how vibration is detected and processed.

void loop() { … }: The loop function is the main program loop, and it runs repeatedly after the setup function.

  • if (state != 0) { … } else { … }: This conditional statement checks the value of state. If it’s not equal to 0 (meaning there was a vibration detected), it does the following:
  • state = 0;: Resets state to 0, preparing for the next detection.
  • digitalWrite(SensorLED, HIGH);: Turns on the LED by setting the digital pin 10 (LED) to HIGH.
  • delay(500);: Delays the program for 500 milliseconds, keeping the LED on.
  • If state is 0 (no vibration detected), it simply turns off the LED by setting the digital pin 10 (LED) to LOW.

void blink() { … }: This is the interrupt service routine (ISR) named blink. When an interrupt is triggered (vibration detected), this function is called.

  • state++;: It increments the state variable, indicating that vibration was detected.

More content you may be interested in

How to Enable Multi-Core on ESP32 Microcontroller
How to Enable Multi-Core on ESP32 Microcontroller

This comprehensive guide elucidates the process of enabling multi-core functionality on the ESP32 microcontroller, leveraging its dual-core architecture for enhanced parallel processing capabilities. Through detailed

Arduino MQ-3 Alcohol Sensor Project
Arduino MQ-3 Alcohol Sensor Project

Creating an alcohol sensing project with an MQ-3 sensor and Arduino allows for accurate alcohol concentration monitoring in environments like bars and factories. The MQ-3

Arduino Temperature Alarm Project
Arduino Temperature Alarm Project

This Arduino temperature alarm project effectively demonstrates how to create a practical temperature monitoring system. By utilizing the LM35 temperature sensor and a buzzer, it

Arduino Breathing LED Project
Arduino Breathing LED Project

A breathing light is an LED lighting effect that simulates the human breathing process, gradually increasing or decreasing in brightness within a specific cycle. This

Scroll to Top