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:
- A working circuit connecting the SW520D tilt sensor to the ESP32 S3
- Code that reads the tilt sensor's digital output in real time
- Serial Monitor output showing tilt detection and disappearance events
- A foundation for tilt-triggered applications such as alarms, relays, and servo control

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) |
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.
- VCC pin: Connect to power supply (3.3V to 5V)
- GND pin: Connect to ground (0V)
- 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

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.

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:
Step 2 - Read the sensor state:
ESP32 S3 Code - Detecting the tilt
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Check the wiring: Verify all connections match the wiring diagram above
- Copy the code: Copy the provided code and open it in Arduino IDE
- Select your board: Choose ESP32 S3 from the boards menu
- Upload the code: Click the Upload button to transfer code to your ESP32 S3
- Open Serial Monitor: Set baud rate to 115200 in the Serial Monitor
- Test the sensor: Tilt the SW520D sensor back and forth
- Observe the output: Check the Serial Monitor for tilt detection messages
- Pro Tip: Mount the sensor firmly when testing — even slight table vibrations can trigger false readings on a loose breadboard setup
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.
- Tilt Alarm: Build a tilt-activated alarm system using a buzzer or relay module
- Servo Controller: Create a tilt-controlled servo motor that responds to orientation changes
- Anti-Tamper System: Design an anti-tamper system for enclosures or equipment
- WiFi Notification: Build a tilt-triggered notification system using WiFi
- 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.
- Beginner: Add an LED that lights up when tilt is detected and turns off when upright
- Beginner: Display the tilt state on an LCD screen instead of Serial Monitor
- Intermediate: Create a tilt counter that counts how many times the sensor has been tilted
- Intermediate: Combine the tilt sensor with a relay to build a tilt-activated switch
- 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