Arduino Mega - SW520D Tilt Sensor

This guide shows how to use the Arduino Mega and a SW520D tilt sensor to detect tilt. We will cover the following topics:

Arduino Mega SW520D tilt sensor

Later, you can change the code to turn on an LED or a light (using a relay) when it detects tilt, or even make a servo motor move.

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×SW520D Tilt Sensor Module
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of SW520D Tilt Sensor

You can use a SW520D tilt sensor to detect when something is tilted or moved. Inside the module, there is a small metal ball that rolls between two electrical contacts depending on the angle. The module gives a simple ON or OFF digital signal.

The SW520D Tilt Sensor Pinout

The SW520D tilt sensor has three pins.

  • VCC pin: connect to power (3.3V to 5V).
  • GND pin: connect to ground (0V).
  • DO pin: this is an output pin. It goes HIGH when the sensor is upright and goes LOW when it is tilted. Connect this pin to an input pin on the Arduino Mega.
SW520D Tilt Sensor Pinout
image source: diyables.io

The SW520D tilt sensor module also has two LED lights.

  • One LED light for power
  • One LED light for tilt state: on when the sensor is upright, off when it is tilted

How It Works

  • When the sensor is upright, the metal ball closes the contact, and the output pin goes HIGH.
  • When the sensor is tilted, the metal ball opens the contact, and the output pin goes LOW.

Wiring Diagram

The wiring diagram between Arduino Mega SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW520D Tilt Sensor

  • Sets a pin on the Arduino Mega as a digital input using the pinMode() function. For example, pin 8.
pinMode(8, INPUT);
  • Uses the digitalRead() function to see a pin's state on the Arduino Mega.
int tiltState = digitalRead(8);

Arduino Mega Code - Detecting the tilt

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-sw520d-tilt-sensor */ #define SENSOR_PIN 8 // The Arduino Mega pin connected to the DO pin of the SW520D tilt sensor int prev_tilt_state = HIGH; // the previous state from the input pin int tilt_state; // 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 input pin: tilt_state = digitalRead(SENSOR_PIN); if (prev_tilt_state == HIGH && tilt_state == LOW) Serial.println("The tilt has been detected"); else if (prev_tilt_state == LOW && tilt_state == HIGH) Serial.println("The tilt has disappeared"); // save the the last state prev_tilt_state = tilt_state; }

Detailed Instructions

Do these steps one by one.

  • Connect the SW520D tilt sensor to the Arduino Mega following the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Select the correct board: Arduino Mega, and the right COM port.
  • Copy the code and open it in the Arduino IDE.
  • Click Upload to send the code to the Arduino Mega.
  • Tilt the SW520D sensor back and forth.
  • Check the results in the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' 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 Mega Or... on COM15
2

Troubleshooting

If the SW520D tilt sensor isn't working well, try these steps:

  • Check the orientation: The SW520D is sensitive to its mounting angle. Make sure it is installed in the correct upright position for reliable detection.
  • Reduce vibrations: The tilt sensor can also react to mechanical vibrations. Fix it firmly to a stable surface to reduce false triggers.
  • 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 consistent readings.

Video Tutorial

Function References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!