SW-520D arduino tilt sensor wiring

Arduino Tilt Sensor Experiment

Tilt sensors are essential components in security alarm systems today. Standalone tilt sensors sense tilt angle or movement. Tilt sensors can be implemented using mercury and roller ball technology, and can be mounted using mechanical threading, magnets, or adhesives, depending on what type of surface they are being mounted to.

Recent technological advancements in the manufacturing of tilt sensors have improved accuracy, reduced cost, and increased lifetime. The type SW-520D is a commonly available roller-ball type tilt sensor consists of two conductive elements (poles) and a conductive free mass (rolling ball), encapsulated in the same case. When the tilt sensor is oriented so that that end is downwards, the mass rolls onto the poles and shorts them, acting as a switch stroke. Microcontroller-compatible tilt sensor modules based on SW-520D are also available at affordable costs.

Related Products: Accelerometer Angular and Linear Position Sensor

Electronics circuitry behind this tiny module is usually centered around the dual- comparator chip LM393. The module features a tilt sensor, a signal amplifier, a standard 4-pin header, a power indicator that signals that the module is correctly powered, and a status indicator that lights up when a tilt is detected by the tilt sensor.

When the tilt sensor is in its upright position, the ball inside the tilt sensor bridges the two contacts, completing the circuit. When the board is tilted, the ball moves, and the circuit opens. When upright, the module outputs 0V (L) and when it is tilted, it outputs 5V (H) through the digital output (DO) terminal of the 4-pin header. If the analog output (AO) of the module is connected to an analog input (for example A0) on the Arduino you can expect to read a value of 0 (0V) when in its upright position and 1023 (5V) when it is tilted.

SW-520D prototype

SW-520D tilt sensor module

Hook Up

The tilt sensor module can be connected to arduino using suitable jumper wires. First of all connect the power supply lines; VCC and GND of the module to 5V and GND of the Arduino respectively. Next link the digital output (DO) of the module to digital pin 2 (D2) and analog output (AO) to analog input 0 (A0) of the arduino. The whole hardware should be powered by a 9V DC / USB source through the DC IN /USB socket of the Arduino board. Keep the tilt switch position in upright position as indicated in the figure shown below.

SW-520D arduino tilt sensor wiring

Sketch: Digital

This example code wakes the onboard indicator (LED at D13) of the Arduino when a tilt is inputted by the tilt sensor module through the occupied digital input (D2). Just copy-paste this code into your Arduino IDE, compile, and upload it to your Arduino as usual.

const int statusLED = 13;
const int switchTilt = 2;
int val = 0;
void setup(){
   pinMode (statusLED,OUTPUT);
   pinMode (switchTilt,INPUT);
void loop(){
   val = digitalRead(switchTilt);
   if (val == HIGH){
   else {

Note that this code does not include a “software-debounce” feature commonly used with button/switch inputs. This is not necessary here because the tilt sensor module have a built-in (1ms) “hardware debounce” arrangement using a simple RC network (R-10K & C-100n).

arduino tilt sensor prototype

Sketch: Analog

This example code lights up the onboard indicator (LED at D13) of the Arduino when a tilt is inputted by the tilt sensor module through the occupied analog input (A0). Again, copy-paste this code into your Arduino IDE, compile, and upload it to your Arduino as done earlier.

int lightPin = 13;
int tiltPin = 0;
void setup() {
void loop() {
   int analogValue = analogRead(tiltPin);
   if (analogValue<512) {
      analogWrite(lightPin, 0);
   else {
      analogWrite(lightPin, analogValue);

SW-520D prototype
Many readers might wonder why I opted for somewhat “strange” sketches here. It’s for nothing; first of all test your hardware with the included sketches, and then start the brainstorming. Replace the above described sketches with your own favourite sketches – merely a little home work for you!

Frankly speaking, with the sketches presented here, these experiments can also be conducted using a tilt sensor (SW-520D) only, ie. without the whole tilt sensor module. If you have an independent tilt sensor component at hand, make a try with the following hardware instead of the dedicated module.

SW-520D hardware debounce

Sketch: Interrupts

Interrupt – incorporated since the 0007 version of the Arduino IDE – breaks in in the execution of the main code. On the hardware front, Arduino is equipped with two interrupt ports so Arduino can sense those pins for an event to wake up and resume execution of code. It is even possible to execute special code depending on which pin triggered the wake up (the interrupt). In short, interrupt is a method by which a microcontroller can execute its normal program while continuously monitoring for some kind of interrupt (event). This interrupt can be triggered by some sort of sensor, or input like a switch.

When the interrupt occurs, the microcontroller takes immediate notice, saves its execution state, runs a small chunk of code often called the interrupt handler or interrupt service routine, and then returns back to whatever it was doing before. The set up in the program defines where the microcontroller should start executing code if a particular interrupt occurs. In Arduino, we use a function called “attachInterrupt()” to do this. This function adopts three parameters. The first is the number of the interrupt, which tells the microprocessor which pin to monitor. The second parameter of this function is the location of code we want to execute if this interrupt is triggered. And the third, tells it what type of trigger to look for, a logic high, a logic low or a transition between the two. You can find detailed articles/tutorials on Arduino Interrupts (prepared by me) elsewhere in this website.

Following is a basic example sketch to demonstrate the interrupt function in an Arduino connected with the tilt sensor. This code looks for interrupts on “interrupt pin 0” (D2) of the Arduino to control the onboard indicator at its output (D13).

#define LED_PIN 13
volatile boolean state = HIGH;
void setup() {
   pinMode(LED_PIN, OUTPUT);
   attachInterrupt(INTERRUPTPIN, inputChange, CHANGE);
void loop() {
   digitalWrite(LED_PIN, state);
void inputChange()
   state = !state;

Unfortunately, this loose sketch is prone to mischief because of the switch-bounce problems. Replacing the 100nF capacitor (C) with a 1uF capacitor might solve this to a certain extent (hardware-debounce). Otherwise opt for a sketch filled with “debounce” code lines (software-debounce).

SW-520D oscilloscope bounce

You can see just how much bouncing occurs in this oscillogram of the input from a switch. Trace “A” is the voltage appearing at the input pin of the Arduino. Instead of the expected smooth transition, a series of pulses lasting over 1 mS are generated. Each one of these pulses would generate an (unnecessary) interrupt. However, if we attach some debouncing hardware to the switch then the bouncing effect is filtered out. Notice how the “A” trace is a smooth curve, with a gradual transition!


Join the conversation!

Error! Please fill all fields.
  • hafiz

    I use the coding for analog but it said the ‘lt’ was not declared in this scope. Can anyone explain to me? Thanks..

    • T.K.Hareendran

      Possibly there’s an encoding issue when you copy-paste the code from here to your Arduino IDE. Please send the full error message to rectify the issue!

  • T.K.Hareendran

    @David: It’s my policy to follow up articles published by me in online/print media. Due to time constraints often I can’t reply to comments in right time, but response assured. Thanks for your attention, and feedback!

  • David

    Thanks for your continued interest.

  • T.K.Hareendran

    @ Davide: Basic circuit diagram of the above mentioned tilt sensor module (only for your reference) is attached here!

  • David

    Early on in the article the author mentions the inclusion in the (typical) module of an LM393 dual comparator chip. But the author doesn’t explain the purpose of the comparator and, in fact, the tilt switch module’s operation is explained without reference to the comparator.
    Can the author extend the explanation to include the role of the comparator chip?

    • T.K.Hareendran

      @David: The module is a pre-wired type from China. You can read its working description in paragraph 3 & 4 of this article. Actually, I think the LM393 chip in the module (and its support) is not very crucial for this experiment. I will try to add its circuit diagram here (as a seperate comment) for your ready reference. Thanks for the feedback!