Go Down

Topic: Vibration Sensor (Read 1 time) previous topic - next topic


Dec 18, 2011, 12:08 pm Last Edit: Dec 18, 2011, 12:27 pm by Mastino Reason: 1
Hello everybody,
I have bought this vibration sensor: http://i40.tinypic.com/vpksxh.jpg
At first I thought that it will work like a button(if vibrates it give digital HIGH, else LOW).
But in reality it works only with this code:
Code: [Select]

#define SensorLED     13
#define SensorINPUT   2

unsigned char state = 0;

void setup()
 pinMode(SensorLED, OUTPUT);
 pinMode(SensorINPUT, INPUT);
 attachInterrupt(0, blink, FALLING);//D2 interface: interrupt 0. call blink()

void loop()
       state = 0;

void blink()//when input falling edge, this interrupt function works.

//the result is the LED( PIN 13 ) will light 500ms after the Shock.

Can somebody explain me how this code works? Is it possible to change sensors sensitivity with the code?
Thank you very much!


The interrupt routine is activated whenever pin 2 goes from HIGH to LOW, and increments the counter called "state".

The loop() code checks state every half second and if its not zero (indicating some joggling of the sensor in that 1/2 second) it turns the LED on (and clears the state back to zero).

This code is not perfect (for instance if there are 256 joggles of the sensor state wraps round to zero again and it will miss it).

I suspect the sensor is a simple tremble switch.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


Thank you very much for your answer!
Is it possible to change sensors sensitivity with the code?


i dont think u can distinguish strong vibration from soft vibration... the amplitude can just be "too low" and "high enough"...

but maybe u can draw some conclusions out of the frequency of the vibration?


You may want to look into using an accelerometer with tap detection instead. 

Go Up