Arduino UNO R4 - SW520D Tilt Sensor
In this guide, we will learn how to use the Arduino UNO R4 and a SW520D tilt sensor to detect tilt. We will cover the following details:
- How to connect a SW520D tilt sensor to an Arduino UNO R4
- How to write a program for Arduino UNO R4 to detect tilt using a SW520D tilt sensor

Later, you can adjust the code so that it turns on an LED or a light (using a relay) when it detects a tilt, or even makes a servo motor turn.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
| 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 (also known as the ball switch tilt sensor module or angle sensor module) is used to detect tilt or orientation changes. Inside the sensor, there is a small metal ball that closes or opens an electrical contact depending on the tilt angle. The module gives a simple ON or OFF digital signal, which makes it easy to use with the Arduino UNO R4.
The SW520D Tilt Sensor Pinout
The SW520D tilt sensor has three pins:
- VCC pin: Connect to VCC (3.3V to 5V).
- GND pin: Connect to GND (0V).
- DO pin: This is an output pin. It shows HIGH when the sensor is upright and LOW when it is tilted. Connect this pin to the input pin of Arduino UNO R4.

The SW520D tilt sensor module also has two LED indicators.
- One LED indicator for power
- One LED indicator for tilt: on when the sensor is upright, off when it is tilted
How It Works
- When the sensor is upright (not tilted), 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
See The best way to supply power to the Arduino Uno R4 and other components.
How To Program For SW520D Tilt Sensor
- Sets up the Arduino UNO R4 pin as a digital input using the pinMode() function. For instance, pin 8
- Uses the digitalRead() function to check the status of a pin on the Arduino UNO R4.
Arduino UNO R4 Code - Detecting the tilt
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the SW520D tilt sensor to the Arduino Uno R4 according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy the code and open it in Arduino IDE.
- Click the Upload button in Arduino IDE to send the code to the Arduino UNO R4.
- Tilt the SW520D sensor back and forth.
- Check the results on the Serial Monitor.
Troubleshooting
If the SW520D tilt sensor isn't working right, try these steps:
- Check the orientation: The SW520D is sensitive to its mounting orientation. Make sure it is installed in the correct upright position for proper detection.
- Reduce vibrations: The tilt sensor can pick up vibrations and small movements. It helps to attach it to a sturdy surface to lessen these vibrations.
- Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
- Check the power supply: Make sure the power supply is stable for reliable readings.