Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
|
Setup:
Read moreFor hardware connections simply plug the vibration sensor into A0.
Code for piezo vibration sensor
ArduinoTouch the piezo sensor to make it vibrate, of course, any way to make it vibrate would be OK too. The LED would be on when vibration detected. You can also Open the serial monitor to see the sensor outputs.
const int ledPin=12;
void setup() {
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop() {
int sensorValue = analogRead(A0);//read the vibration
Serial.println(sensorValue);//print it
delay(1000);
if(sensorValue==1023)//if you have an led it will turn on or off based on the vibration
{
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
}
Code 2 for piezo vibration sensor
ArduinoYou can directly use a digital pin, take D5 of base shield as an example, and connect LED to Pin 12.
const int ledPin=12;
void setup() {
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop() {
int sensorState = digitalRead(5);
Serial.println(sensorState);
delay(1000);
if(sensorState == HIGH)
{
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
}
Comments