ESP32 MicroPython SW-420 Vibration Sensor

This tutorial shows you how to pair a SW-420 vibration sensor with an ESP32 running MicroPython so you can catch shocks and vibration in your own project. Here's what you'll pick up along the way:

ESP32 MicroPython SW-420 vibration sensor

Once it's working, you can build on this same code to sound a buzzer, flash an LED, or fire off a notification whenever vibration is picked up.

Overview of SW-420 Vibration Sensor

A SW-420 vibration sensor module is handy whenever you need to notice a knock, bump, or shake on whatever it's mounted to. Inside the module, a small spring-based switch hovers close to a metal contact; the onboard LM393 comparator keeps watch over that switch and outputs a clean digital signal the instant vibration or shock disturbs it. A small potentiometer on the board lets you dial in exactly how much shaking it takes before the output fires.

Pinout

The SW-420 vibration sensor module has three pins:

  • VCC pin: connect to VCC (3.3 volts to 5 volts)
  • GND pin: connect to GND (0 volts)
  • DO pin: this output pin sits LOW while things are still, and jumps HIGH the moment vibration or shock is detected. Connect it to an input pin on your ESP32.
SW-420 Vibration Sensor Pinout
image source: diyables.io

The module also carries two onboard LEDs.

  • One LED simply confirms the board is powered
  • One LED lights up whenever the DO pin goes HIGH, meaning vibration was detected

How It Works

Underneath the cover sits a tiny spring switch positioned next to a fixed contact.

  • While the module is at rest, the spring stays clear of the contact, so the output pin reads LOW.
  • When vibration or a shock jolts the spring into the contact, the comparator drives the output pin HIGH.
  • Turning the onboard potentiometer raises or lowers how much shaking it takes to trigger that switch, so you can tune it from light taps up to hard knocks.

Wiring Diagram

  • How to wire an ESP32 to a SW-420 vibration sensor using breadboard
The wiring diagram between ESP32 MicroPython SW-420 Vibration Sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and SW-420 vibration sensor

How To Program For SW-420 Vibration Sensor

  • Configures the ESP32 pin as a digital input.
sensor_pin = Pin(SENSOR_PIN, Pin.IN)
  • Reads the current state of that pin on the ESP32.
vibration_state = sensor_pin.value()

ESP32 MicroPython Code - Detecting vibration

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-sw-420-vibration-sensor """ from machine import Pin import time SENSOR_PIN = 18 # The ESP32 pin GPIO18 connected to DO pin of the SW-420 vibration sensor prev_vibration_state = 0 # the previous state from the input pin vibration_state = 0 # the current reading from the input pin # Initialize the sensor pin as an input sensor_pin = Pin(SENSOR_PIN, Pin.IN) # Main loop while True: # Read the state of the input pin vibration_state = sensor_pin.value() if prev_vibration_state == 0 and vibration_state == 1: print("Vibration detected!") elif prev_vibration_state == 1 and vibration_state == 0: print("Vibration stopped") # Save the last state prev_vibration_state = vibration_state time.sleep(0.1) # Delay for 100 milliseconds

Detailed Instructions

Follow these steps to get the MicroPython script running on your ESP32 through Thonny IDE:

  • Install Thonny IDE on your computer, if it isn't there already.
  • Make sure your ESP32 board already has MicroPython firmware flashed onto it.
  • First time pairing MicroPython with an ESP32? Work through the ESP32 MicroPython Getting Started guide before continuing.
  • Wire the SW-420 vibration sensor to the ESP32 following the diagram above.
  • Plug the ESP32 into your computer using a USB cable.
  • Launch Thonny IDE.
  • In Thonny, go to Tools Options.
  • On the Interpreter tab, pick MicroPython (ESP32) from the dropdown menu.
  • Make sure the right port is selected. Thonny usually finds it on its own, but you may need to pick it manually (such as COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the MicroPython code below and paste it into Thonny's editor.
  • Save the script onto your ESP32:
    • Click the Save button or press Ctrl+S.
    • In the save dialog, pick MicroPython device.
    • Name the file main.py.
  • Press the green Run button (or hit F5) to start the script.
  • Tap or shake the SW-420 sensor to give it a vibration to catch.
  • Watch the Shell pane at the bottom of Thonny for the printed messages.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Vibration detected! Vibration stopped Vibration detected! Vibration stopped
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Troubleshooting

If the SW-420 vibration sensor isn't behaving as expected, try the following steps:

  • Adjust the sensitivity: Turn the onboard potentiometer to make the sensor more or less sensitive, then test again with a light tap and a firmer knock.
  • Isolate it from stray vibration: Mount the sensor firmly and away from motors, fans, or surfaces that shake on their own, so it doesn't trigger from ambient noise.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly and securely.
  • Check the power supply: Make sure the ESP32 is receiving stable power, since voltage dips can cause erratic readings.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!