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:

Arduino 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.

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.
SW520D Tilt Sensor Pinout
image source: diyables.io

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

Arduino SW520D Tilt Sensor 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
pinMode(5, INPUT);
  • Reads the state of the Arduino pin by using digitalRead() function.
int tiltState = digitalRead(5);

Arduino Code - Detecting the tilt

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-sw520d-tilt-sensor */ // Arduino's pin connected to DO pin of the SW520D tilt sensor #define SENSOR_PIN 5 int lastState = HIGH; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if (lastState == HIGH && currentState == LOW) Serial.println("The tilt has been detected"); else if (lastState == LOW && currentState == HIGH) Serial.println("The tilt has disappeared"); // save the the last state lastState = currentState; }

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.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
The tilt has been detected The tilt has disappeared The tilt has been detected The tilt has disappeared
Ln 11, Col 1
Arduino Uno on COM15
2

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.

Function References

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES