Using the SW-420 Motion Vibration Sensor with a WeMos D1 Mini (ESP8266)
In this blog post I talk about how you can use a SW-420 Motion Vibration sensor with a WeMos D1 Mini (ESP8266) using the Arduino IDE.
Summary of the SW-420 Motion Vibration Sensor
This sensor mounts a motion detection component with a small micro-controller and a potentiometer. This has a single output pin which is high when the motion level is reached.
The potentiometer allows changing of the amount of motion required to trigger the digital out becoming high.
The single input combined with the easy to change motion threshold makes this sensor simple to use and implement in projects.
The VCC accepts between 3.3 and 5 volts which makes it ideal for use by the WeMos running at 3.3 volts.
Important differences compared with using the SW-420 Motion Vibration Sensor on an Arduino
There is one major difference to bear in mind when using the SW-420 Motion Vibration Sensor on a WeMos D1 Mini (ESP8266) or similar chipset.
When using the Arduino IDE with the WeMos chips, the standard pin numbers do not refer to the pin numbers on the WeMos. For example, instead of using 1
to refer to digital pin 1
, you need to use the constant D1
.
I go into more details for this in my blog post Pin numbering for WeMos D1 Mini (ESP8266). If you haven’t read this and are having trouble connecting your SW-420 Motion Vibration sensor properly I recommend having a read.
Wiring it up to a WeMos D1 Mini
Here I am connecting the SW-420 motion vibration sensor to a WeMos D1 mini.
The pins on the SW-420 motion vibration sensor, from left to right, are Digital Out, GND, and VCC
It is important to note that the WeMos’s digital pins can only accept up to 3.3 volts maximum on their inputs. This means that the SW-420 motion vibration sensors VCC pin must be connected to the 3.3 volts and not the 5 volt line like when using the Arduino.
Programming the WeMos to use the SW-420 motion vibration sensor
The vibration sensor is triggered when the configured motion (set by the potentiometer) is exceeded. When this occurs the digital pin is set to HIGH representing motion being detected.
#define VIBRATION_SENSOR_PIN D2
int motion_detected = LOW;
void setup() {
Serial.begin(115200);
}
void loop() {
motion_detected = digitalRead(VIBRATION_SENSOR_PIN);
Serial.println(motion_detected);
delay(100);
}
Since we only need to read the digital input all the setup does is configure the serial connection.
In the loop we continually read the value of the vibration sensor and print it out. This piece of code can be used to configure the potentiometer so that the sensor triggers at the motion level required.
Summary
The SW-420 motion vibration sensor is a simple sensor that uses a single data pin to detect motion. In addition to this, the onboard potentiometer provides a simple way to adjust the level of motion needed to trigger the digital pin.
No ESP8266 specific libraries are required as this only uses the single digital pin. However it is important to ensure you connect the VCC pin to the 3.3 volts line. This is because this is the maximum voltage accepted by the digital pins.
For a full review of the SW-420 motion vibration sensor have a read of my previous post.