SW-420 Motion Vibration Sensor Review and code examples
This post reviews the SW-420 motion vibration sensor. Here I review, test and provide code to use the sensor.
Overview of the Vibration Sensor
This motion vibration sensor detects small movements and reports this back as a digital signal. This digital signal represents whether there is motion above the configured value or not.
The amount of motion to trigger back a HIGH (true, motion is detected) is set by using the onboard potentiometer. By changing the potentiometer the level of motion required to trigger is changed. Looking at the sensor face on, turning clockwise will increase the level of motion needed. Turning this anticlockwise will reduce the level of motion needed.
There are two LED’s present on the sensor, the left LED is lit when no motion is detected according to the sensor. When the digital out reports HIGH (motion detected) this left LED will not be lit, when it reports LOW it will be lit. The right LED is the power indicator and will be lit all the time the sensor has power. If this is not lit then you will want to check your circuit for power.
To confirm you are turning the potentiometer the correct way you can use the left LED to help. When turned fully anticlockwise the LED will never be lit showing that all motion is detected. When turned fully clockwise the LED will be always be lit no matter the amount of motion, showing no motion is detected.
To set the level of detection you will want to set the potentiometer fully anticlockwise. Then slowly turn it clockwise until it has been set to the level you require.
Wiring it up to an Arduino
Here the sensor only requires three pins to connect to the Arduino. As in the diagram to the left the three pins are labelled DO, GND and VCC. These relate digital out, ground and voltage common collector respectively.
In the diagram I wire up both the 5V Arduino line to VCC and GND to the ground pin. The digital out pin is connected to Pin 3 on the Arduino. This however can be connected to any digital pin of the Arduino.
Pin 3 will be set into input mode using the Arduino IDE so that we can read the value from the sensor. This pin will be HIGH when motion is detected and low otherwise.
Programming the Arduino
Since the vibration sensor is triggered when the motion reaches the configured value reading this is simple. You only need one digital pin on the Arduino to check its output. In the below Arduino code I read the digital pin 10 times a second printing the value of the pin.
#define VIBRATION_SENSOR_PIN 3 int motion_detected = LOW; void setup() { Serial.begin(115200); } void loop() { motion_detected = digitalRead(VIBRATION_SENSOR_PIN); Serial.println(motion_detected); delay(100); }
You can improve this by taking action only if motion has been detected. Possibly triggering some kind of alarm with a piezo or lights.
Final Review
The sensor works well to detect motion at any level. Using the onboard potentiometer you can easily define at what level you want the device to trigger. This has a disadvantage as you need to manually set the detection level and only have a binary check of whether its detected or not.
You will need to have a quick loop checking whether motion is detected depending on the amount of motion you want to detect. For detecting short periods of motion this loop will need to be quite fast.
If you are looking for a vibration/motion sensor that returns an analogue signal you will want to look at the 801S Wide Range Vibration Sensor. This has an improvement over the SW-420 Motion Vibration Sensor as it gives both a digital and anlogue pin out.
Overall this is a nice cheap movement sensor that needs little electronics to get to work. Since it provides a simple on/off detection it can be easily joined with other electronics.
Would buy again.
i am looking at building a circuit for my a college module that would receive data from an accelerometer fitted to dc motor to provide vibration data
it would then store the vibration data before transferring the data via Bluetooth to a laptop/tablet.
This idea is based on the transfer of Health Usage Monitoring Ststem (HUMS) IN a Wildcat Helicopter. Instead of using a PCMCIA card the data would be transferred via Bluetooth
I have a rough idea on what components would be required but have never done any coding before and am struggling massively with this
I suggest you look at starting with a basic Arduino project such as the one above with a vibration sensor. What you describe is relatively complex and if you have never done any coding it will likely be beyond your initial abilities. But it wouldn’t need too much work to get it to work once you have an idea about how it all fits together.
I suggest:
Have a look at what an Arduino can do, this can provide an introduction to digital and analogue electronics.
(At this point you probably need some kind of device to dev with, I would recommend an Arduino but there are many)
Once you have something you can dev with I would then try getting it log the data recieved from your sensor and report it back via serial (this is what the above code would do).
Once this is working you can then look to add bluetooth functionality to report back the data. There will be Arduino modules to do bluetooth but you might also want to look at wifi. There are a number of dev boards that include wifi as standard, wemos for example.
Hopefully this gives some feedback as to where you can go with such a project. Best of luck!
Excuse my knowledge. What is “to dev with”?
To develop with.
Is there a site you would recommend for sketches/codes that may cover areas of what I’m hoping to achieve? I will work my way through the varying levels of difficulty but really was after confirmation that what I’m hoping to do can be done.
Sorry Mark I didnt see your reply!
It might be worth buying a beginner arduino Kit. I have this kit: https://store.arduino.cc/genuino-starter-kit
Which gives you a good set of electronics and a book to give examples.