ESP32 UNO - SW520D Tilt Sensor
Want to know when an object leans over? Connect a SW520D tilt sensor to the ESP32 Uno form board and print a message each time it tips. The module is a ball switch (also called an angle sensor) and needs no library, only a simple digital read.
By the end, you can:
- Wire a SW520D tilt sensor to the ESP32 Uno
- Read the sensor output with digitalRead()
- Detect when a tilt starts and when it stops
- Show the events in the Serial Monitor

Later, you can change the sketch to switch on an LED, a lamp through a relay, or a servo motor when a tilt happens.
Components You Need
| 1 | × | ESP32 Uno-form Board | |
| 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 |
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
By the end of this part, you know what the module does and what each pin is for.
Inside the SW520D is a small metal ball. When the sensor stands upright, the ball touches two contacts and the circuit closes. When you tilt it, the ball rolls away and the circuit opens. The module turns this into a clean HIGH or LOW signal.
| Pin | Role |
|---|---|
| VCC | Supply pin, accepts 3.3V to 5V. Pick 3.3V on this board |
| GND | Ground (0V) |
| DO | Signal pin. Reads HIGH at rest and LOW once tilted |

The board has two indicator LEDs. One shows power. The other is on while the sensor is upright and off while it is tilted.
How It Works
| Sensor position | Ball | DO output |
|---|---|---|
| Upright | Closes the contact | HIGH |
| Tilted | Opens the contact | LOW |
Good to know: The ESP32 Uno input pins use 3.3V logic. Power the module from 3.3V so the DO signal is also 3.3V.
ESP32 Uno Pinout
Every Uno header pin on the ESP32 Uno form board is tied to a GPIO number, and your sketch uses that number. Look at the diagram first, then connect the wires.

How to Connect
By the end of this part, the three wires are in place.

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
The wiring diagram may not show the correct pin. On the ESP32 Uno, DO goes to D7 (GPIO14), not D8 as the picture may show. We will update the diagram soon. Until then, follow the wiring table and the code on this page, not the picture.
| SW520D Tilt Sensor Pin | ESP32 Uno Pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| DO | D7 (GPIO14) |
Good to know: In code you write the GPIO number (14), not the label D7.
How To Program For SW520D Tilt Sensor
The sketch needs just two functions.
| Function | Use |
|---|---|
| pinMode(14, INPUT) | Makes D7 (GPIO14) an input, done once in setup() |
| digitalRead(14) | Returns HIGH or LOW from the sensor, called in loop() |
ESP32 Uno Code - Detecting the tilt
The sketch compares each new reading with the last one and prints a message only when the state changes.
| Code Part | What It Does |
|---|---|
| SENSOR_PIN 14 | Names the pin that reads DO (D7 is GPIO14) |
| prev_tilt_state | Stores the reading from the last pass |
| digitalRead() | Gets the new reading each pass |
| HIGH to LOW | Prints "The tilt has been detected" |
| LOW to HIGH | Prints "The tilt has disappeared" |
The result is one line per change, not a flood of lines.
Upload and Test
Do these steps one by one:
- First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
- Build the circuit by following the wiring table above.
- Plug in the USB Type-C cable between the ESP32 Uno and your computer.
- Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
- Copy the code into Arduino IDE.
- Upload the code by clicking Upload.
- Open the Serial Monitor and set 9600 baud.
- Move the sensor back and forth and read the messages.
- Pro tip: Press the board against a table edge and rock it slowly. A slow rock gives fewer false events than a quick shake.
Video Tutorial
See the ESP32 Uno build in action in the video below.
ESP32 Uno vs UNO R4 for This Project
By the end of this part, you know what changes when you move this tilt sensor from the UNO R4 to the ESP32 Uno.
| Item | Arduino UNO R4 | ESP32 Uno |
|---|---|---|
| Logic voltage | 5V | 3.3V |
| DO pin | D8 (pin 8) | D7 (GPIO14) |
| Pin number in code | 8 | 14 |
| Library | None | None |
| Arduino IDE board | Arduino UNO R4 WiFi | ESP32 Dev Module |
| USB chip | Built in | CH340 |
Power the module from 3.3V. The DO wire goes to D7 (GPIO14) here, not D8 as on the UNO R4, because D8 (GPIO12) is a boot pin and an upright sensor could stop the upload.
Getting Steady Readings
| Problem | Fix |
|---|---|
| Wrong direction | Mount the module upright so it reads HIGH at rest |
| Vibration | Fix the sensor to a firm surface |
| Loose wires | Check VCC, GND and DO |
| Weak power | Use a stable USB port and cable |
Go Wireless: Tilt Alerts on Your Phone
By the end of this part, you have an idea to see tilt events without a cable.
The ESP32 Uno has WiFi and Bluetooth, so the tilt state can go to a web page or a phone app. You could watch a "tilted" message from across the room, or get a log of every tip.
Try These Next
- Turn on an LED when a tilt is detected
- Sound a buzzer when something falls over
- Count how many times the sensor tips
Function References
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port appears | CH340 driver missing or charge-only cable | Install the CH340 driver and use a data USB cable |
| Failed to connect or Waiting for download | Board not in upload mode | Hold the BOOT button while the upload starts |
| Nothing in the Serial Monitor | Wrong baud rate | Set 9600 baud and the correct port |
| State never changes | DO wire on the wrong pin or loose power wires | Check DO on D7 (GPIO14), VCC on 3.3V, GND on GND |
| Many messages from one small move | Ball bounces inside the switch | Add delay(50) or debounce logic |
| Messages are swapped | Sensor mounted upside down | Mount the module upright |
| Board acts strangely | Module powered from 5V sends 5V into the pin | Power from 3.3V or add a voltage divider |
FAQ
How does a SW520D ball switch detect tilt?
A small metal ball sits inside. Upright, it closes two contacts. Tilted, it rolls away and opens them. The module outputs HIGH or LOW from this.
Can I power the SW520D from 5V with the ESP32 Uno?
The module accepts 3.3V to 5V, but the ESP32 Uno pins are not 5V tolerant. At 5V the DO pin gives 5V, which can harm the board. Use the 3.3V pin.
Why does the code use 14 and not 7?
The label D7 is only the header position. On the ESP32 Uno it is connected to GPIO14, and Arduino code uses the GPIO number.
Can the SW520D measure the exact angle?
No. It only switches on or off. A real angle needs an accelerometer such as the MPU6050.
Do I need a library for the tilt sensor?
No. The sketch uses only pinMode(), digitalRead() and Serial.
Which other pin can I use for the tilt sensor?
Any normal digital pin, such as D2 (GPIO26) or D3 (GPIO25). Avoid D0 and D1 (USB serial) and strapping pins. Change the number in the sketch.
Do I have to change the sketch when I leave the UNO R4?
The program flow matches. Just the pin number differs, from 8 to 14, and you pick ESP32 Dev Module in Arduino IDE.
Can I use the SW520D with a UNO shield on the ESP32 Uno?
Yes, if the shield uses the same header pins and 3.3V logic. Check that it does not hold D7 (GPIO14) in a way that blocks the sensor.
Can I show the tilt state on a web page with the ESP32 Uno?
Yes. The ESP32 Uno has WiFi and Bluetooth, so you can send the state to a web page or a phone app.
What if my project needs more pins than the Uno headers offer?
The ESP32 Uno has a few extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they work with WiFi on) and GPIO15 (I/O, PWM, analog on ADC2). Add pin headers by soldering, then use the GPIO number in your sketch. For a tilt sensor, GPIO32 or GPIO33 are the best choice. Two warnings. First, GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code, so use GPIO32/33 first. Second, never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11). They are wired to the flash chip and using them crashes the board.