Lesson 14: Detecting Vibrations with Arduino Using a Vibration Sensor
In this tutorial, we learn how to use a simple vibration sensor module with an Arduino to detect and respond to mechanical vibrations. Unlike motion sensors which detect movement or acceleration, this sensor specifically responds to vibrations—such as knocks, taps, or mechanical impacts. When such vibrations are detected, the module outputs a signal that can be used to trigger other devices.
The sensor has a basic three-pin interface: VCC, GND, and Digital OUT. When a vibration occurs, the digital output pin goes HIGH for a moment. This makes it very easy to interface with the Arduino using a standard digital input pin. In this tutorial, the vibration signal is used to trigger a buzzer, though it can just as easily activate a relay, alarm light, fan, or another actuator for custom projects like anti-theft systems, industrial machines, or notification systems.
The code for this project is very straightforward. It checks for a HIGH signal on the digital pin connected to the sensor. When a vibration is detected, the Arduino responds by sending a HIGH signal to the buzzer (or any other connected device). This minimal logic makes the system highly responsive and efficient for real-time reaction. The tutorial also covers how to debounce false triggers and add delays if needed for stable operation.
This video is part of a hands-on step-by-step Arduino course by Robojax, and is perfect for beginners or intermediate makers who want to experiment with real-world sensor input. All wiring, code, and explanations are clearly shown. Make sure to watch the full video to see it in action, and you’ll find the downloadable code and additional resources below.
📚 Chapters / Sections
[00:00] Introduction
[00:27] Vibration Sensor Explained
[00:38] How It Works (Not Motion, but Vibration)
[00:44] Example Use Cases (Buzzer, Relay, Light, etc.)
[01:05] Hardware Pins and Connection
[01:30+] Wiring Guide
[02:00+] Arduino Code Overview
[03:00+] Live Demonstration
/*
* Lecture 14 Vibration Sensor
* Arduino code for vibration sensor
* turn a relay ON which the relay
* can turn a light or alarm ON
* vibration sensor is connected to pin 2
* Relay is connected to pin 8
Please watch video instruction here https://youtu.be/wuNrzQ8rpYw
This code is available at https://robojax.com/tutorial_view.php?id=569
with over 100 lectures Free On YouTube. Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
* Written by Ahmad Shamshiri for Robojax, robojax.com http://youtube.com/@robojax
* Date: Dec 05, 2017, in Ajax, Ontario, Canada
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
int vibrarationInPin = 2;// define pin 2 for vibration sensor
int relayPin = 8;// pin connected to relay
int onTime= 1000; // time in millisecond ON and wait before make another reading.
int offTime= 200; // time in millisecond OFF and wait before make another reading.
void setup() {
Serial.begin(9600);
//Robojax Step By Step Arduino Course http://robojax.com/L/?id=338
// out pins
pinMode(vibrarationInPin, INPUT);// define vibrarationInPin as input
pinMode(relayPin, OUTPUT);// define relayPin as output for relay
Serial.println("Robojax vibration Sensor");
}
void loop() {
// read the vibration sensor
if(digitalRead(vibrarationInPin) == HIGH){
Serial.println("shaken! ");
digitalWrite(relayPin, HIGH); // Turn the relay ON
delay(onTime);// keep the relay or switch ON for the onTime
}else{
digitalWrite(relayPin, LOW);// Turn the relay OFF
Serial.println("peace");
delay(offTime);// wait offTime before reading again
}
//Robojax Step By Step Arduino Course http://robojax.com/L/?id=338
}// loop
资源与参考
尚无可用资源。
文件📁
没有可用的文件。