Vibration Sensor Demo
Below is Arduino code for testing the KEMET vibration sensors sensitivity. I also attached a video of the code running with the sensor attached to a small fan. In the video you can see when I run the fan it picks up the slight vibration (I couldn't even feel). I have an LCD display attached to show the readouts.
Below is the code for vibration sensor test reading:
#include <LiquidCrystal.h> const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); int vib = A0; //Vibration sensor pin int data; //Sensor reading int average = 0; //Average reading int averageA = 0; int averageB = 0; int x = 0; //Loop count int tread = 0; //Temporary read int hread = 0; //Highest reading void setup(){ lcd.clear(); lcd.begin(16, 2); //Set screen size lcd.setCursor(0,0); //Start print location lcd.print("KEMET Vibration"); lcd.setCursor(0,1); //Start print location lcd.print("Sensor - VSBV20"); delay(5000); //5 second delay pinMode(vib, INPUT); //initialize vib sensor as INPUT Serial.begin(9600); //begin the serial monitor at 9600 baud lcd.clear(); do{ //Calibrate loop to get average vibration x = x + 1; averageA = analogRead(vib); averageB = averageB + averageA; Serial.println("Calibrating"); lcd.setCursor(0,0); lcd.print("Calibrating"); lcd.setCursor(0,1); lcd.print("XXXXXXXXXXXXXXX " + x); delay(500); } while (x < 15); average = averageB/15; delay(1000); } void loop(){ data=analogRead(vib); Serial.println(data); tread = data; x = x + 1; // Set highest reading if(hread < tread) { hread = tread; } lcd.clear(); lcd.setCursor(0,0); lcd.print("Ca="); lcd.setCursor(3,0); lcd.print(average); //Display average vibration lcd.setCursor(8,0); lcd.print("CR="); lcd.setCursor(11,0); lcd.print(data); //Displays current vibrations lcd.setCursor(8,1); lcd.print("HR="); lcd.setCursor(11,1); lcd.print(hread); //Displays highest vibration reading delay(100); }
Below is the code for my project:
#include <LiquidCrystal.h> const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); int vib = A0; //Vibration sensor pin int safelt1 = 8; //Assign warning LED int safelt2 = 9; //Assign warning LED int warnlt1 = 10; //Assign warning LED int warnlt2 = 13; //Assign warning LED int shutdwnlt = 14; //Assign warning LED int shutdwnsw = 7; //Assign shutdown switch int data; //Sensor reading int average = 0; //Average reading int averageA = 0; int averageB = 0; int x = 0; //Loop count int tread = 0; //Temporary read int hread = 0; //Highest reading void setup(){ pinMode(shutdwnsw, OUTPUT); digitalWrite(shutdwnsw, LOW); lcd.clear(); lcd.begin(16, 2); lcd.setCursor(0,0); //Start print location lcd.print("KEMET Vibration"); lcd.setCursor(0,1); //Start print location lcd.print("Sensor - VSBV20"); delay(2500); pinMode(vib, INPUT); //initialize vib sensor as INPUT Serial.begin(9600); //begin the serial monitor at 9600 baud lcd.clear(); do{ //Calibrate average vibration x = x + 1; averageA = analogRead(vib); averageB = averageB + averageA; Serial.println("Calibrating"); lcd.setCursor(0,0); lcd.print("Calibrating"); lcd.setCursor(0,1); lcd.print("XXXXXXXXXXXXXXX " + x); delay(500); } while (x < 15); average = averageB/15; delay(1000); } void loop(){ data=analogRead(vib); Serial.println(data); digitalWrite(safelt1, HIGH); tread = data; // Set highest reading if(hread < tread) { hread = tread; } if(data <= (average*1.05)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Within Limits"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("--------"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); } else if(data <= (average*1.1)){ lcd.clear(); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Within Limits High"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XX------"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); } else if(data <= (average*1.2)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Warning Limits"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XXXX----"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); } else if(data <= (average*1.35)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Warning Limits High"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XXXXXX--"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); digitalWrite(warnlt2, HIGH); } else if(data <= (average*1.5)){ lcd.clear(); digitalWrite(safelt2, LOW); digitalWrite(warnlt1, LOW); digitalWrite(warnlt2, LOW); digitalWrite(shutdwnlt, LOW); Serial.println("Shut Down Required"); lcd.setCursor(0,0); lcd.print("Ave="); lcd.setCursor(4,0); lcd.print(average); lcd.setCursor(10,1); lcd.print("HR="); lcd.setCursor(13,1); lcd.print(hread); lcd.setCursor(0,1); lcd.print("XXXXXXXX"); lcd.setCursor(10,0); lcd.print("CR="); lcd.setCursor(13,0); lcd.print(data); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); digitalWrite(warnlt2, HIGH); digitalWrite(shutdwnlt, HIGH); } else if(data > (average*1.5)){ Serial.println("Shutting Down"); lcd.clear(); digitalWrite(safelt2, HIGH); digitalWrite(warnlt1, HIGH); digitalWrite(warnlt2, HIGH); digitalWrite(shutdwnlt, HIGH); digitalWrite(shutdwnsw, HIGH); lcd.setCursor(1,0); lcd.print("Limit Exceeded"); lcd.setCursor(8,1); lcd.print("Max="); lcd.setCursor(12,1); lcd.print(data); delay(5000); lcd.clear(); lcd.setCursor(0,0); lcd.print("Press Button to"); lcd.setCursor(0,1); lcd.print("Reset Sensor"); while(0 == 0); // Causes infinate loop {} } delay(100); }
I know the code is a bit messy and can be cut down, but for now it does the job. I will upload a couple of videos of it working in the field.
Thank you for checking out my blog,
Dale W