Arduino Nano 33 IoT - SW520D Tilt Sensor

The SW520D tilt sensor can detect when something changes its orientation or is tilted. It can be used to create cool projects that react to movement, like an alarm that turns on when an object is moved or a servo motor that responds to tilt.

In this guide, we will learn how to use the Arduino Nano 33 IoT and a SW520D tilt sensor to detect tilt. We will look at the following topics:

After that, you can easily change the code so that it turns on an LED or a light (with a relay) when it detects a tilt, or even makes a servo motor move.

Arduino Nano 33 IoT SW520D tilt sensor

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×SW520D Tilt Sensor Module
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

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

The SW520D tilt sensor module is used to detect tilt or orientation changes. 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

This SW520D tilt sensor has three pins.

  • VCC pin: Connect this pin to a power supply between 3.3V and 5V.
  • GND pin: Connect this pin to ground (0V).
  • DO pin: This pin sends a signal — it shows HIGH when the sensor is upright and LOW when it is tilted. Connect it to an input pin on the Arduino Nano 33 IoT.
SW520D Tilt Sensor Pinout
image source: diyables.io

The SW520D tilt sensor module also includes two LED lights.

  • One LED light shows if the power is on.
  • Another LED light shows the tilt state — it lights up when the sensor is upright and turns off when it is tilted.

How It Works

This module uses a simple ball switch mechanism. Here is how the sensor's output pin works:

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

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW520D Tilt Sensor

  • This sets the Arduino Nano 33 IoT pin to act as a digital input using the pinMode() function. For example, you can use pin D3.
pinMode(3, INPUT);
  • It checks if the Arduino Nano 33 IoT pin is HIGH or LOW using the digitalRead() function.
int tiltState = digitalRead(D3);

Arduino Nano 33 IoT Code - Detecting the tilt

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-sw520d-tilt-sensor */ #define SENSOR_PIN 3 // The Arduino Nano 33 IoT pin D3 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 the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano 33 IoT's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the Arduino Nano 33 IoT's 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

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the above code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano 33 IoT.
  • Tilt the SW520D sensor back and forth.
  • Check the result in the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano 33 IoT
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano 33 IoT' 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 Nano 33 IoT on COM15
2

Now, we can change the code to make an LED or light turn on when a tilt is detected. We can even make a servo motor move depending on the tilt. For more details and easy step-by-step instructions, check the tutorials at the end of this guide.

Troubleshooting

If you have any problem with the SW520D tilt sensor, please try these steps to fix it:

  • 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: Mechanical shaking and vibrations can cause false triggers. To lessen these issues, mount the sensor on a solid, steady surface.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Make sure the power supply is clean and free from interference 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!