Arduino - SW520D Tilt Sensor
The SW520D tilt sensor module (also known as a ball switch or angle sensor) is capable of detecting tilt or orientation changes. It can be used to create tilt-reactive projects, such as an alarm that triggers when an object is moved or a servo motor that responds to orientation.
In this tutorial, we are going to learn how to use Arduino and a SW520D tilt sensor to detect tilt. In detail, we will learn:
- How to connect the SW520D tilt sensor to Arduino
- How to program Arduino to detect tilt using the SW520D tilt sensor

Afterward, you can tweak the code to make it activate an LED or a light (via a relay) when it detects a tilt, or even make a servo motor rotate.
Hardware Required
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables .
About SW520D Tilt Sensor
The SW520D tilt sensor module can be used to detect tilt or orientation changes in the surrounding environment. 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 Arduino.
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 Arduino's input pin.

The SW520D tilt sensor module also has two LED indicators:
- One LED indicator for power
- One LED indicator for the tilt state: on when the sensor is upright, off when it is tilted
How It Works
- When the sensor is upright, the metal ball inside closes the contact, and the output pin is HIGH.
- When the sensor is tilted, the metal ball inside opens the contact, and the output pin is LOW.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
How To Program For SW520D Tilt Sensor
- Initializes the Arduino pin to the digital input mode by using pinMode() function. For example, pin 5
- Reads the state of the Arduino pin by using digitalRead() function.
Arduino Code - Detecting the tilt
Quick Steps
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Tilt the SW520D sensor back and forth
- See the result on Serial Monitor.
Now we can customize the code to activate an LED or a light when a tilt is detected, or even make a servo motor rotate. You can find more information and step-by-step instructions in the tutorials provided at the end of this tutorial.
Troubleshooting
If the SW520D tilt sensor is not working properly, try the following 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: The tilt sensor can also pick up mechanical vibrations. Mounting it on a solid 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 stable for consistent readings.
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.