Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Sensors & Modules

Tilt Sensor SW-520D Tutorial: Arduino Tilt Detection Project

Tilt Sensor SW-520D Tutorial: Arduino Tilt Detection Project

March 11, 2026 /Posted byJayesh Jain / 0

The SW-520D tilt sensor is one of the simplest yet most useful components in an electronics maker’s toolkit. Also called a ball tilt switch or mercury-free tilt sensor, it detects orientation changes and triggers a digital signal whenever the sensor is tilted beyond a certain angle. In this complete Arduino tutorial, we cover everything from how the SW-520D works to building real tilt detection projects with code examples.

Table of Contents

  1. What Is the SW-520D Tilt Sensor?
  2. How It Works
  3. Specifications & Pin Details
  4. Wiring to Arduino
  5. Basic Arduino Code
  6. Project: Tilt Alarm System
  7. Project: Anti-Theft Vehicle Alarm
  8. SW-520D vs Accelerometer: When to Use Which
  9. Debouncing Tilt Sensors
  10. Applications in India
  11. FAQ
  12. Conclusion

What Is the SW-520D Tilt Sensor?

The SW-520D is a mechanical tilt switch that contains two conductive balls inside a small cylindrical housing. When the sensor is in its upright position, the balls rest at the bottom of the cylinder and bridge the two electrical contacts, closing the circuit. When tilted beyond approximately 45°, the balls roll away from the contacts and the circuit opens.

This makes the SW-520D perfect for:

  • Detecting when an object has been moved or tilted
  • Building simple alarms triggered by disturbance
  • Orientation detection in simple robotics
  • Anti-theft alarm systems
  • Automatic display rotation (basic)

Unlike accelerometers (which give continuous 3-axis angle data), the SW-520D gives a simple binary signal — tilted or not tilted — making it extremely easy to use with any microcontroller.

How It Works

Inside the SW-520D sensor body there are two small metal balls (sometimes copper balls) and two contact pins that extend from one end of the cylinder. The principle is straightforward:

  • Upright position: Balls rest at the closed end (near the pins) → circuit CLOSED → LOW signal (if pull-up used)
  • Tilted position: Balls roll away from pins → circuit OPEN → HIGH signal (with pull-up)

The SW-520D has no specific trigger axis — it responds to tilt in any direction as long as the balls roll far enough to lose contact. The typical trigger angle is 30–45 degrees from the upright axis, though this varies between individual units.

Why is it better than mercury switches? The SW-520D uses metal balls instead of liquid mercury, making it completely non-toxic and safe to use in consumer and educational electronics. The European RoHS directive banned mercury tilt switches, so the SW-520D became the standard replacement.

Specifications & Pin Details

Parameter Value
Operating Voltage 3V – 24V DC
Current Rating Max 60mA (continuous)
Contact Resistance < 10Ω (closed)
Insulation Resistance > 10MΩ (open)
Trigger Angle ~30–45° from upright
Operating Temperature -10°C to +70°C
Lifetime > 50,000 cycles
Dimensions Approx 10mm × 6mm

The SW-520D has two legs (pins) — it is a two-terminal passive switch with no polarity. Either pin can connect to signal or ground.

Wiring to Arduino

There are two common wiring configurations:

Method 1: Internal Pull-Up Resistor (Recommended)

Use Arduino’s internal 20kΩ pull-up resistor — no external resistor needed:

  • Pin 1 of SW-520D → Arduino Digital Pin D2
  • Pin 2 of SW-520D → Arduino GND

In code, use pinMode(2, INPUT_PULLUP). When upright (closed), you read LOW. When tilted (open), you read HIGH.

Method 2: External Pull-Down Resistor

  • Pin 1 of SW-520D → Arduino 5V
  • Pin 2 of SW-520D → Arduino Digital Pin D2 (also connect a 10kΩ resistor from this pin to GND)

When closed (upright): reads HIGH. When open (tilted): reads LOW.

Basic Arduino Code

// SW-520D Tilt Sensor Basic Example
// Using internal pull-up resistor

const int TILT_PIN = 2;
const int LED_PIN  = 13; // onboard LED

void setup() {
  Serial.begin(9600);
  pinMode(TILT_PIN, INPUT_PULLUP); // Enable internal pull-up
  pinMode(LED_PIN, OUTPUT);
  Serial.println("SW-520D Tilt Sensor Ready");
}

void loop() {
  int state = digitalRead(TILT_PIN);

  if (state == LOW) {
    // Circuit closed = upright
    Serial.println("Status: UPRIGHT");
    digitalWrite(LED_PIN, HIGH); // LED ON when upright
  } else {
    // Circuit open = tilted
    Serial.println("Status: TILTED !");
    digitalWrite(LED_PIN, LOW);
  }

  delay(200);
}

Open the Serial Monitor at 9600 baud and tilt the sensor to see the output change.

Project: Tilt Alarm System

This project sounds a buzzer when the sensor is tilted — useful for protecting fragile equipment or as a simple disturbance detector.

Components

  • Arduino Uno
  • SW-520D tilt sensor
  • Active piezo buzzer
  • Red LED
  • 220Ω resistor (for LED)

Wiring

  • SW-520D: Pin1 → D2, Pin2 → GND (internal pull-up)
  • Buzzer: Positive → D8, Negative → GND
  • LED: Anode → D9 (via 220Ω), Cathode → GND
const int TILT_PIN  = 2;
const int BUZZER    = 8;
const int LED_RED   = 9;

bool alarmActive = false;
unsigned long lastDebounce = 0;
const int DEBOUNCE_MS = 50;

void setup() {
  pinMode(TILT_PIN, INPUT_PULLUP);
  pinMode(BUZZER, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(TILT_PIN);
  unsigned long now = millis();

  if ((now - lastDebounce) > DEBOUNCE_MS) {
    if (reading == HIGH) { // TILTED
      alarmActive = true;
    } else {
      alarmActive = false;
    }
    lastDebounce = now;
  }

  if (alarmActive) {
    tone(BUZZER, 2000); // 2kHz alarm tone
    digitalWrite(LED_RED, HIGH);
    Serial.println("ALARM: Tilt Detected!");
  } else {
    noTone(BUZZER);
    digitalWrite(LED_RED, LOW);
  }

  delay(50);
}

Project: Anti-Theft Vehicle Alarm

Mount the SW-520D on a two-wheeler or bicycle frame to trigger an alarm if someone attempts to move the vehicle. The tilt sensor detects when the bike is lifted, tilted, or rolled.

How It Works

  1. SW-520D is mounted vertically on the vehicle frame
  2. When the vehicle is upright and stationary, the circuit is closed (no alarm)
  3. If the bike is tilted, pushed over, or lifted — the sensor opens and the alarm sounds
  4. A latching mechanism in code keeps the alarm active even after the sensor returns to upright
// Anti-theft tilt alarm with latching
const int TILT_PIN  = 2;
const int BUZZER    = 8;
const int RESET_BTN = 4; // Secret reset button

bool alarmed = false;

void setup() {
  pinMode(TILT_PIN, INPUT_PULLUP);
  pinMode(BUZZER, OUTPUT);
  pinMode(RESET_BTN, INPUT_PULLUP);
}

void loop() {
  int tilt = digitalRead(TILT_PIN);
  int reset = digitalRead(RESET_BTN);

  // Latch alarm on tilt
  if (tilt == HIGH) alarmed = true;

  // Secret reset button clears alarm
  if (reset == LOW) alarmed = false;

  if (alarmed) {
    tone(BUZZER, 3000);
    delay(200);
    tone(BUZZER, 1500);
    delay(200);
  } else {
    noTone(BUZZER);
    delay(100);
  }
}
Capacitive Soil Moisture Sensor

Capacitive Soil Moisture Sensor

Another versatile sensor for Arduino projects — pair it with a tilt sensor for smart plant monitoring that detects pot movement and soil dryness.

View on Zbotic

SW-520D vs Accelerometer: When to Use Which

Feature SW-520D Tilt Sensor Accelerometer (MPU-6050)
Output Type Binary (on/off) Analog (continuous)
Axis Information None (any tilt) 3-axis (X, Y, Z)
Cost ~₹10–30 ~₹150–300
Code Complexity Very simple Moderate to complex
Best For Simple alarm, disturbance detection Drones, robotics, orientation
Precision Low (threshold only) High (±2°)

Rule of thumb: If you just need to know whether something has been disturbed (yes/no), use the SW-520D. If you need to know the exact angle, speed, or direction of movement, use an accelerometer/gyroscope like the MPU-6050.

Debouncing Tilt Sensors

One challenge with the SW-520D is that the metal balls can bounce when transitioning, causing multiple false triggers. This is called contact bounce and is similar to button debouncing.

There are two ways to debounce:

Software Debouncing

Use a time delay to ignore transitions that happen too quickly:

unsigned long lastChange = 0;
int lastState = LOW;
const unsigned long DEBOUNCE_DELAY = 50; // 50ms

void loop() {
  int current = digitalRead(TILT_PIN);
  if (current != lastState) {
    lastChange = millis();
    lastState = current;
  }
  if ((millis() - lastChange) > DEBOUNCE_DELAY) {
    // Stable reading - use current state
  }
}

Hardware Debouncing

Add a 0.1µF capacitor across the sensor pins (in parallel). This smooths out the signal transitions and reduces bouncing at the hardware level. A simple RC filter with a 10kΩ resistor and 100nF capacitor also works well.

Applications in India

  • Tool safety: Detect when a power tool is tilted or dropped
  • Locker alarm: Trigger an alert when a safe or cabinet is disturbed
  • Earthquake sensor: A simple tilt chain can detect vibrations (though professional seismometers are far more complex)
  • Smart medication reminder: Detect when a medicine bottle is picked up
  • Student projects: Ball maze games, tilt-controlled robots
MQ-135 Air Quality Gas Detector Sensor Module

MQ-135 Air Quality/Gas Detector Sensor

Build a complete safety monitoring system — combine tilt detection with gas sensing to detect tampered equipment and potential gas leaks simultaneously.

View on Zbotic

Frequently Asked Questions

Q: Can SW-520D detect vibration?

Yes, to some extent. The metal balls are sensitive to vibration and will briefly open the circuit when the sensor is shaken. However, for precise vibration measurement, use a dedicated vibration sensor (SW-420) which is a spring-based sensor with more sensitivity to vibration.

Q: What angle does the SW-520D trigger at?

The SW-520D typically triggers at approximately 30–45 degrees from the vertical axis. This varies between units due to manufacturing tolerances. If you need a specific trigger angle, you should characterize your specific unit or use an accelerometer for precise angle control.

Q: Is SW-520D waterproof?

No, the standard SW-520D is not waterproof. However, being an enclosed switch, it has moderate protection against dust. For outdoor or wet applications, pot the sensor in epoxy resin or use a waterproof enclosure.

Q: Can I use multiple SW-520D sensors for 2D tilt detection?

Yes! Mount two SW-520D sensors at 90 degrees to each other to detect tilt in two orthogonal directions. This gives you basic X-Y tilt information with two binary signals. For true 3-axis sensing, an accelerometer like the MPU-6050 is a better choice.

Q: What’s the difference between SW-520D and SW-520?

The SW-520 and SW-520D are virtually identical in function. The “D” designation typically refers to the package type (cylindrical vs flat). Both contain two metal balls inside a cylindrical housing and operate on the same principle. They are interchangeable for most DIY projects.

Conclusion

The SW-520D tilt sensor is one of the most affordable and versatile components for Arduino beginners. Its simple two-pin interface and digital output make it easy to integrate into any project without complex libraries or calibration. Whether you’re building a simple tilt alarm, an anti-theft device, or a fun tilt-controlled game, the SW-520D provides reliable and fast tilt detection.

The key takeaways from this tutorial: use internal pull-up resistors for simplest wiring, implement software debouncing for stable readings, and consider the SW-520D vs accelerometer tradeoff carefully — sometimes simple is better. For more complex projects requiring precise angle data, upgrade to the MPU-6050 or a dedicated IMU module.

Build Your Arduino Projects with Zbotic

Browse our complete range of sensors, modules, and Arduino accessories — everything you need for your next maker project, delivered across India.

Shop Sensors & Modules

Tags: arduino tilt project, ball switch sensor, SW-520D arduino, tilt detection arduino, tilt sensor
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Lead Acid vs Lithium: Which Ba...
blog lead acid vs lithium which battery for solar projects 596374
blog alcohol sensor mq3 breathalyzer build with arduino 596378
Alcohol Sensor MQ3: Breathalyz...

Related posts

Svg%3E
Read more

Encoder Module: Position and Speed Measurement with Arduino

April 1, 2026 0
A rotary encoder converts the angular position and rotation speed of a shaft into electrical signals that Arduino can count... Continue reading
Svg%3E
Read more

Infrared Obstacle Sensor: Line Follower and Object Detection

April 1, 2026 0
Infrared obstacle sensors are the building blocks of line-following robots, edge detection systems, and proximity triggers. These tiny modules emit... Continue reading
Svg%3E
Read more

Rain Sensor and Raindrop Detection Module: Arduino Guide

April 1, 2026 0
A rain sensor module detects the presence of water droplets on its surface, giving Arduino a simple signal to trigger... Continue reading
Svg%3E
Read more

LiDAR Sensor TFmini: Distance Measurement Beyond Ultrasonic

April 1, 2026 0
When ultrasonic sensors hit their limits — range too short, accuracy too coarse, outdoor sunlight causing interference — LiDAR sensors... Continue reading
Svg%3E
Read more

Pressure Sensor BMP280: Weather Station and Altitude Meter

April 1, 2026 0
The BMP280 barometric pressure sensor by Bosch measures atmospheric pressure with ±1 hPa accuracy and temperature with ±1°C precision. These... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now