In this blog post I talk about how you can use a SW-18010P Motion Vibration sensor with a WeMos D1 Mini (ESP8266) using the Arduino IDE.
Summary of the SW-18010P Motion Vibration sensor
This motion sensor works by encasing a small spring inside a metal casing. Whenever the sensor moves the spring makes contact with the metal casing and changes resistance.
This change in resistance is measured and output as a analogue signal in the sensor.
In addition to the analogue output, the sensor can be configured to output a digital signal when a threshold value is reached.
Since the circuit is relatively simple it will draw little power making it ideal for IoT projects running on batteries.
The sensors pins, left to right are analogue out (AO), digital out (DO), ground (GND) and input voltage (VCC). The analogue out will give the raw data from the sensor module.
When motion is detected above the threshold, digital out will be LOW. This level can be changed by changing the value of the onboard potentiometer.
The analogue out pin gives a relative value for the level of vibration. This value is 0 when there is a lot of vibration and 1023 when no motion is detected.
Important differences compared with using SW-18010P Motion Vibration sensor on an Arduino
If you are using the digital output you need to bear in mind that for 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-18010P Motion Vibration sensor properly I recommend having a read.
In addition to this, it is important to only wire the sensor’s VCC pin up to 3.3 volts. This is because the analogue and digital pins are only rated for 3.3 volts. Wiring it up to the 5 volt pin may damage the WeMos microcontroller.
Wiring it up to a WeMos D1 Mini
Here the leftmost pin, the analogue connection, is connected to the single analogue pin on the WeMos.
The VCC pin is connected to the 3.3 volts on the WeMos, and GND to the ground pins.
As noted above, it is important to ensure that the VCC pin is only connected to the 3.3 volt pin on the WeMos.
This is because the analogue pin is only rated for 3.3 volts, connecting it to the 5 volt line may damage the WeMos.
Programming the WeMos to use the vibration sensor
I am going to use the analogue pin to record the level of vibration and print it on the serial line.
Since the WeMos D1 mini only has one analogue pin we have to connect the sensor to analogue pin 0.
Inside the loop method, the analogue pin is read and printed out to the serial connection. At the end of the loop we delay the code by 10 milliseconds before it will loop and read the next value.
The SW-18010P Motion Vibration sensor is a nice small sensor which can be configured to use either a digital or analogue pin. This is a very useful to use with the WeMos as there is only one analogue pin.
No ESP8266 specific libraries are required as this only uses the single analogue or 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 analogue and digital pins.