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

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.

You Might Be Interested

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 warns users with an audible

LED Blinking S.O.S Signal with Arduino
Simulating an LED Blinking S.O.S Signal with Arduino

This Arduino project demonstrates how to create the internationally recognized Morse code distress signal “S.O.S” using an LED. By controlling the LED’s duration of illumination, it represents the pattern of

How to Download and Install Arduino IDE
How to Download and Install Arduino IDE

Learn how to easily download and install the Arduino IDE on Windows, Linux, and macOS! This step-by-step guide will walk you through the entire process, from downloading the software to

Simple Arduino UNO Projects
6 Simple Arduino UNO Projects

This article introduces six beginner-friendly projects for Arduino UNO, a popular microcontroller board. These projects cover a range of applications, from marquee lights and stepper motor control to ultrasonic ranging,

what is an arduino
What is an Arduino?

Discover the wonders of Arduino, an open-source electronics platform that simplifies electronic design and empowers people to create their own projects. From mesmerizing LED displays to intelligent home automation systems,

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 steps and code examples, developers

Scroll to Top