ESP8266 - SW520D Tilt Sensor

The SW520D tilt sensor module has the capability to detect tilt or orientation changes in its surroundings. It can be employed to create projects that respond to tilt, like an alarm that activates when an object is disturbed or a servo motor that responds to orientation changes.

This tutorial instructs you how to use the ESP8266 and a SW520D tilt sensor to detect tilt. We will explore:

ESP8266 NodeMCU NodeMCU SW520D tilt sensor

Subsequently, you have the option to modify the code to trigger the activation of an LED or a light (using a relay) upon tilt detection, or even make a servo motor rotate.

Hardware Preparation

1×ESP8266 NodeMCU ESP-12E
1×Recommended: ESP8266 NodeMCU ESP-12E (Uno-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×SW520D Tilt Sensor Module
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for ESP8266
1×Recommended: Power Splitter for ESP8266 Type-C

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of SW520D Tilt Sensor

The SW520D tilt sensor module can be used to detect tilt or orientation changes in the environment around it. Inside the module, there is a small metal ball that rolls between two electrical contacts depending on the tilt angle. The module outputs a simple digital signal (ON/OFF), which makes it easy to interface with the ESP8266.

The SW520D Tilt Sensor Pinout

The SW520D tilt sensor has three pins:

  • VCC pin: needs to be connected to VCC (3.3V to 5V)
  • GND pin: needs to be connected to GND (0V)
  • DO pin: is an output pin: HIGH when the sensor is upright and LOW when the sensor is tilted. This pin needs to be connected to ESP8266's input pin.
SW520D Tilt Sensor Pinout
image source: diyables.io

The SW520D tilt sensor module comes with two LED indicators:

  • One LED indicates the power status.
  • Another LED indicates the tilt state: it turns on when the sensor is upright and off when it is tilted.

How It Works

Here's how the sensor behaves:

  • When the sensor is upright, the metal ball inside closes the contact, and the output pin is set to HIGH.
  • When the sensor is tilted, the metal ball inside opens the contact, and the output pin is set to LOW.

Wiring Diagram

  • The wiring diagram between ESP8266 and SW520D tilt sensor when powering via USB port
The wiring diagram between ESP8266 NodeMCU and SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between ESP8266 and SW520D tilt sensor when powering via Vin
The wiring diagram between ESP8266 NodeMCU and SW520D Tilt Sensor Vin

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For SW520D Tilt Sensor

  • Initializes the ESP8266 pin to the digital input mode by using pinMode() function. For example, pin D7
pinMode(D7, INPUT);
  • Reads the state of the ESP8266 pin by using digitalRead() function.
int tiltState = digitalRead(D7);

ESP8266 Code - Detecting the tilt

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-sw520d-tilt-sensor */ #define SENSOR_PIN D7 // The ESP8266 pin D7 connected to the DO pin of the SW520D tilt sensor int prev_state = HIGH; // The previous state from the input pin int tilt_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP8266's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the ESP8266's input pin tilt_state = digitalRead(SENSOR_PIN); if (prev_state == HIGH && tilt_state == LOW) Serial.println("The tilt has been detected"); else if (prev_state == LOW && tilt_state == HIGH) Serial.println("The tilt has disappeared"); // save the the last state prev_state = tilt_state; }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Tilt the SW520D sensor back and forth
  • Check out the result on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
The tilt has been detected The tilt has disappeared The tilt has been detected The tilt has disappeared
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Now, with the code customized, we can make it activate an LED or a light when a tilt is detected. We can even make a servo motor rotate. For detailed instructions and additional information, please refer to the tutorials provided at the end of this guide.

Troubleshooting

If you encounter issues with the SW520D tilt sensor not functioning correctly, try the following troubleshooting steps:

  • Check the orientation: The SW520D is sensitive to its mounting orientation. Make sure it is installed in the correct upright position for reliable detection.
  • Reduce vibrations: Mechanical vibrations can affect the performance of the tilt sensor. Mounting it on a stable surface can help minimize false triggers.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Ensure that the power supply is clean and stable for consistent readings.

Video Tutorial

Function References

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!