ESP32 S3 - SW520D Tilt Sensor

SW520D tilt sensors detect orientation changes and enable your ESP32 S3 to create interactive, tilt-responsive projects like alarms that activate when objects are moved or servo motors that respond to orientation. This tutorial shows you how to connect and program a SW520D tilt sensor with your ESP32 S3 board.

What you'll build:

  1. A working circuit connecting the SW520D tilt sensor to the ESP32 S3
  2. Code that reads the tilt sensor's digital output in real time
  3. Serial Monitor output showing tilt detection and disappearance events
  4. A foundation for tilt-triggered applications such as alarms, relays, and servo control
ESP32 S3 SW520D tilt sensor

After completing this tutorial, you can modify the code to trigger actions when tilt is detected - turn on an LED, activate a relay to control lights, or rotate a servo motor based on tilt input.

Hardware Preparation

1×ESP32 S3 WROOM N16R8
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×Breadboard
1×Jumper Wires
1×Optionally, 5V Power Adapter

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 is a detection module that senses tilt and orientation changes using a simple ball switch mechanism. Inside the module, a small metal ball rolls between two electrical contacts depending on the angle of the sensor. It outputs a digital HIGH or LOW signal directly readable by the ESP32 S3, making integration straightforward with no analog conversion or calibration required.

Key Specifications

  • Simple digital output — no analog reading required
  • Works with 3.3V to 5V microcontrollers like ESP32 S3
  • LED indicators for power and tilt state
  • Low cost and easy to integrate
  • No calibration needed

The SW520D Tilt Sensor Pinout

The SW520D tilt sensor exposes three connection pins that map directly to your ESP32 S3.

  1. VCC pin: Connect to power supply (3.3V to 5V)
  2. GND pin: Connect to ground (0V)
  3. DO pin: Digital output pin - outputs HIGH when the sensor is upright, LOW when the sensor is tilted - connect to ESP32 S3 digital input pin
SW520D Tilt Sensor Pinout
image source: diyables.io

Additional features:

  • Power LED indicator shows when the sensor is powered
  • Status LED indicator reflects the tilt state — on when upright, off when tilted

How It Works

The SW520D tilt sensor uses a ball switch mechanism that your ESP32 S3 can read.

Digital output behavior:

  • When the sensor is upright: metal ball closes the contact → output pin goes HIGH
  • When the sensor is tilted: metal ball opens the contact → output pin goes LOW

Wiring Diagram

Connect your SW520D tilt sensor to the ESP32 S3 using three wires as shown in the diagram below. Double-check every connection before powering on to protect both the sensor and your ESP32 S3.

Safety Notes

Ensure that the VCC wire goes to the correct 3.3V or 5V pin on your ESP32 S3 and that the GND wire is firmly seated, as a loose ground connection is the most common source of erratic readings. Avoid bending or straining the sensor module leads, since mechanical stress can cause intermittent contact inside the ball switch.

The wiring diagram between ESP32 S3 SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

SW520D Tilt Sensor Pin ESP32 S3 Pin
VCC 3.3V or 5V
GND GND
DO D16 (GPIO16)

How To Program For SW520D Tilt Sensor

Programming the ESP32 S3 to read a SW520D tilt sensor is straightforward and requires only two steps — configuring the pin as a digital input and then polling its state in a loop. The following code configures pin D16 as an input, reads the tilt sensor state on every loop iteration, and prints a descriptive message to the Serial Monitor whenever the tilt state changes.

Step 1 - Initialize the pin:

pinMode(D16, INPUT);

Step 2 - Read the sensor state:

int tiltState = digitalRead(D16);

ESP32 S3 Code - Detecting the tilt

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-sw520d-tilt-sensor */ #define SENSOR_PIN 16 // The ESP32 S3 pin 16 connected to the DO pin of the SW520D tilt sensor int prev_tilt_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(115200); // initialize the ESP32's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the ESP32's input pin tilt_state = digitalRead(SENSOR_PIN); if (prev_tilt_state == HIGH && tilt_state == LOW) Serial.println("The tilt has been detected"); else if (prev_tilt_state == LOW && tilt_state == HIGH) Serial.println("The tilt has disappeared"); // save the the last state prev_tilt_state = tilt_state; }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Check the wiring: Verify all connections match the wiring diagram above
  3. Copy the code: Copy the provided code and open it in Arduino IDE
  4. Select your board: Choose ESP32 S3 from the boards menu
  5. Upload the code: Click the Upload button to transfer code to your ESP32 S3
  6. Open Serial Monitor: Set baud rate to 115200 in the Serial Monitor
  7. Test the sensor: Tilt the SW520D sensor back and forth
  8. Observe the output: Check the Serial Monitor for tilt detection messages
  9. Pro Tip: Mount the sensor firmly when testing — even slight table vibrations can trigger false readings on a loose breadboard setup
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
09:14:32.120 - The tilt has been detected 09:14:33.875 - The tilt has disappeared 09:14:35.441 - The tilt has been detected 09:14:36.208 - The tilt has disappeared 09:14:38.763 - The tilt has been detected 09:14:39.540 - The tilt has disappeared
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Now you can customize the code to trigger actions when tilt is detected - activate an LED, control a relay to switch lights, or rotate a servo motor in response to tilt. Check the application ideas and challenge sections below for inspiration.

Application and Project Ideas

The SW520D paired with an ESP32 S3 opens up a wide range of practical tilt-responsive applications across home automation, security, and robotics.

  1. Tilt Alarm: Build a tilt-activated alarm system using a buzzer or relay module
  2. Servo Controller: Create a tilt-controlled servo motor that responds to orientation changes
  3. Anti-Tamper System: Design an anti-tamper system for enclosures or equipment
  4. WiFi Notification: Build a tilt-triggered notification system using WiFi
  5. Digital Level: Create a simple digital level indicator with LED bar graph

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

These challenges will help you deepen your understanding of tilt sensing and push your ESP32 S3 project further.

  1. Beginner: Add an LED that lights up when tilt is detected and turns off when upright
  2. Beginner: Display the tilt state on an LCD screen instead of Serial Monitor
  3. Intermediate: Create a tilt counter that counts how many times the sensor has been tilted
  4. Intermediate: Combine the tilt sensor with a relay to build a tilt-activated switch
  5. Advanced: Build a multi-axis tilt detection system using multiple SW520D sensors

Troubleshooting

If your ESP32 S3 SW520D tilt sensor isn't working properly, try these solutions.

Common issues and fixes:

  • Check orientation: The SW520D is sensitive to its mounting angle — make sure it is installed in the correct upright position for reliable detection
  • Reduce vibrations: Mount the tilt sensor on a stable, vibration-free surface — mechanical vibrations can cause false triggers
  • Verify power supply: Ensure clean, stable power for consistent readings
  • Check wiring: Verify all connections match the wiring diagram — loose or incorrect connections cause erratic behavior
  • Test LED indicators: Power LED should be on constantly; status LED should reflect the current tilt state

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!