Introduction
What you will need - Hardware
|
For this tutorial you will need:
|
The Circuit
The connections are pretty easy.
|
|
The code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /* Vibration Sensor (Shake Switch) - Testing with buzzer In this tutorial we will use one vibration sensor (or shake switch) to make a beep sound from a buzzer while we shake our breadboard. Find more info and video here: http://bit.ly/1iDbc2Q Dev: Michalis Vasilakis - Date: 9/9/2015 - www.ardumotive.com */ const int buzzer = 8; //Buzzer connected to pin 8 of Arduino uno / mega int sensor; //Variable to store analog value (0-1023) void setup() { Serial.begin(9600); //Only for debugging pinMode(buzzer, OUTPUT); } void loop() { sensor = analogRead(A0); //While sensor is not moving, analog pin receive 1023~1024 value if (sensor<1022){ tone(buzzer, 500); Serial.print("Sensor Value: "); Serial.println(sensor); } else{ noTone(buzzer); Serial.print("Sensor Value: "); Serial.println(sensor); } delay(100); //Small delay } |
Download the code from here and open it with Arduino IDE. Open the serial monitor from tools menu of Arduino IDE to test it.
|
|
Well done!
You have successfully completed one more "How to" tutorial and you learned how to use a vibration (or shake) sensor with Arduino.
I hope you liked this, let me know in the comments!
I hope you liked this, let me know in the comments!