Arduino Nano - SW520D Tilt Sensor
The SW520D tilt sensor module has the capability to detect tilt or orientation changes in its surroundings. It can be employed to create projects that respond to tilt, like an alarm that activates when an object is disturbed or a servo motor that responds to orientation changes.
This tutorial instructs you how to use the Arduino Nano and a SW520D tilt sensor to detect tilt. We will explore:
- How to connect the SW520D tilt sensor to the Arduino Nano
- How to program the Arduino Nano to detect tilt using the SW520D tilt sensor.

Subsequently, you have the flexibility to modify the code to trigger an LED or a light (through a relay) upon tilt detection, or even enable the rotation of a servo motor.
Hardware Preparation
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 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 Nano.
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 Nano's input pin.

The SW520D tilt sensor module also features two LED indicators:
- One LED indicates the power status.
- The other LED indicates the tilt state: it turns on when the sensor is upright and off when it is tilted.
How It Works
The SW520D tilt sensor module uses a simple ball switch mechanism. Here's how the output pin of the sensor behaves:
- When the sensor is upright, the metal ball inside closes the contact, and the output pin is set to HIGH.
- When the sensor is tilted, the metal ball inside opens the contact, and the output pin is set to LOW.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
How To Program For SW520D Tilt Sensor
- Initializes the Arduino Nano pin to the digital input mode by using pinMode() function. For example, pin D2
- Reads the state of the Arduino Nano pin by using digitalRead() function.
Arduino Nano Code - Detecting the tilt
Detailed Instructions
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino Nano
- Tilt the SW520D sensor back and forth
- Check out the result on the Serial Monitor.
Now we have the flexibility to personalize the code and make it trigger an LED or a light when a tilt is detected. We can even make a servo motor rotate. For more detailed information and step-by-step instructions, please refer to the tutorials provided at the end of this guide.
Troubleshooting
If you encounter issues with the SW520D tilt sensor not functioning properly, you can follow these steps for troubleshooting:
- 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. To minimize false triggers, try mounting the sensor on a stable surface.
- Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
- Check the power supply: Ensure that the power supply is clean and free from noise for consistent readings.
By following these steps, you can address common issues and improve the performance of the SW520D tilt sensor.