You are on page 1of 6

Vibration sensor (SW-520D)

Summary
We will use the vibration sensor to control the blinking of a LED light in this project.

Materials
Arduino Uno x 1
Vibration sensor SW-520D x 1
5mm LED light x 1
220 Ohm resistor x 2
Breadboard x 1
DuPont wires

Production Description

The SW-520D is a ball type tilt vibration sensor with a unidirectional trigger switch. The
switch is made of metal. The electrical characteristics are similar to the mercury switch, but
it's more safe and environmentally friendly than a mercury switch and it has the same
characteristics of unidirectional conduction when the device is shaken.

The base state for the switch is Off when its angle is below 15 degrees. Above this angle the
switch turns On. When shaken the switch will turn on and off rapidly, or On continuously.
When there is no shaking the device should be orientated such that it reads Off. If the device
is positioned at an angle of greater than 15 degrees it is more difficult to trigger by shaking.
The switch is suitable for triggering a small current, it is NOT suitable for a power switch;

These Ball switches are widely used due to their small size and light weight in devices such as
digital photo frames, rotating screens, cell phone gravity sensors ,anti-theft devices and
intelligent systems.
Specification

Maximum voltage (Vmax): 12v


Rated heating current:5 mA
Open circuit resistance: more than 10M
On resistance: less than 5 Ohm
Ambient temperature less than 100 degrees
Life expectancy: 500,000 activations.

Product Dimensions

Project Design

The vibration sensor detects the vibration and generates a signal. The project combines the
vibration sensor with a LED. The LED lights up when the sensor detects the object vibration.
The LED turns off when the vibration stops. This experiment uses the external interrupt
function.
Wiring Diagram

Note: The vibration switch requires a pull-down resistor and the LED requires a current-limiting
resistor.
Sample code:

int led = 13; //define led pin


int sensorSwitch = 3; //define sensorSwitch pin
unsigned char state = 0;
void setup()
{
pinMode(led,OUTPUT);//Defining the LED interface
pinMode(sensorSwitch,INPUT);//Defining the sensorSwitch interface
attachInterrupt(1,blink,RISING);
}
void loop()
{
if(state != 0) //state is 1;
{
state = 0;
digitalWrite(led,HIGH);//led on
delay(500);//delay 5ms
}
else
{
digitalWrite(led,LOW); //led off
}
}
void blink()
{
state++;
}
Results

The LED light is off when no vibration is detected as shown below.

LED light is on when vibration is detected, as shown below.


Notes on the code

attachInterrupt (): interrupt function, you need to pass three parameters:


attachInterrupt (interrupt, function, mode)
interrupt: interrupt number 0 or 1.
If 0 is selected, connect to digital pin 2 and if select 1,connect to digital pin 3.

function: Name of the interrupt function to be called.


mode: Interrupt type to trigger on.

There are only four specific situations:

LOW: Trigger interrupt when pin is low (not very useful).


CHANGE Trigger interrupt when pin level changes.
RISING Trigger interrupt when pin goes from low to high.
FALLING Trigger the interrupt when the pin goes from high to low

When writing an interrupt function, you need to remember the following three points:

1 - When we write the interrupt function, the function can not contain parameters and return
values. In other words, if there is no return value of the function.

2 - Do not use the delay () and millis () functions in interrupt functions because the interrupts
will not be picked up.

3 - Do not read the serial port interrupt function, the serial port received data may be lost.

You might also like