ESP32 C3 Super Mini - SW520D Tilt Sensor

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

In this tutorial, you'll learn:

ESP32 C3 Super Mini 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 C3 Super Mini
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.

Key features:

  • Simple digital output — no analog reading required
  • Works with 3.3V to 5V microcontrollers like ESP32 C3 Super Mini
  • 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 has three connection pins for easy wiring.

  • 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 C3 Super Mini 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 C3 Super Mini 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 C3 Super Mini following the diagram below.

Important notes:

  • Note: Double-check all connections before powering on to avoid damaging your components
  • Note: The SW520D tilt sensor uses only 3 wires
The wiring diagram between ESP32 C3 Super Mini SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

SW520D Tilt Sensor Pin ESP32 C3 Super Mini Pin
VCC 3.3V or 5V
GND GND
DO D3 (GPIO3)

How To Program For SW520D Tilt Sensor

Programming the ESP32 C3 Super Mini to read a SW520D tilt sensor involves two simple steps.

What the code does:

  • Configures the ESP32 C3 Super Mini pin as a digital input
  • Reads the tilt sensor state (HIGH or LOW)
  • Determines if tilt is detected based on the pin state
  • Displays the detection status on Serial Monitor

Step 1 - Initialize the pin:

pinMode(D3, INPUT);

Step 2 - Read the sensor state:

int tiltState = digitalRead(D3);

ESP32 C3 Super Mini Code - Detecting the tilt

/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-sw520d-tilt-sensor */ #define SENSOR_PIN D3 // The ESP32 C3 SuperMini pin D3 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(9600); // 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

  • New to ESP32 C3 Mini? Complete our Getting Started with ESP32 C3 Mini tutorial first to set up your development environment.
  • 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 C3 Super Mini from the boards menu
  • Upload the code: Click the Upload button to transfer code to your ESP32 C3 Super Mini
  • Open Serial Monitor: Set baud rate to 9600 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
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
14:23:15.342 - The tilt has been detected 14:23:16.891 - The tilt has disappeared 14:23:18.127 - The tilt has been detected 14:23:19.004 - The tilt has disappeared 14:23:21.556 - The tilt has been detected 14:23:22.332 - The tilt has disappeared
Ln 11, Col 1
ESP32C3 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

Use your ESP32 C3 Super Mini SW520D tilt sensor setup to create interactive tilt-responsive projects.

  • Build a tilt-activated alarm system using a buzzer or relay module
  • Create a tilt-controlled servo motor that responds to orientation changes
  • Design an anti-tamper system for enclosures or equipment
  • Build a tilt-triggered notification system using WiFi
  • Create a simple digital level indicator with LED bar graph

Video Tutorial

Watch the video below for a visual walkthrough of this project.

Challenge Yourself

Take your ESP32 C3 Super Mini SW520D tilt sensor skills to the next level with these challenges.

  • Easy: Add an LED that lights up when tilt is detected and turns off when upright
  • Easy: Display the tilt state on an LCD screen instead of Serial Monitor
  • Medium: Create a tilt counter that counts how many times the sensor has been tilted
  • Medium: 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 C3 Super Mini 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!