Arduino UNO R4 - SW520D Tilt Sensor

In this guide, we will learn how to use the Arduino UNO R4 and a SW520D tilt sensor to detect tilt. We will cover the following details:

Arduino UNO R4 SW520D tilt sensor

Later, you can adjust the code so that it turns on an LED or a light (using a relay) when it detects a tilt, or even makes a servo motor turn.

Overview of SW520D Tilt Sensor

The SW520D tilt sensor module (also known as the ball switch tilt sensor module or angle sensor module) is used to detect tilt or orientation changes. Inside the sensor, there is a small metal ball that closes or opens an electrical contact depending on the tilt angle. The module gives a simple ON or OFF digital signal, which makes it easy to use with the Arduino UNO R4.

The SW520D Tilt Sensor Pinout

The SW520D tilt sensor has three pins:

  • VCC pin: Connect to VCC (3.3V to 5V).
  • GND pin: Connect to GND (0V).
  • DO pin: This is an output pin. It shows HIGH when the sensor is upright and LOW when it is tilted. Connect this pin to the input pin of Arduino UNO R4.
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 tilt: on when the sensor is upright, off when it is tilted

How It Works

  • When the sensor is upright (not tilted), 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

The wiring diagram between Arduino UNO R4 SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

How To Program For SW520D Tilt Sensor

  • Sets up the Arduino UNO R4 pin as a digital input using the pinMode() function. For instance, pin 8
pinMode(8, INPUT);
  • Uses the digitalRead() function to check the status of a pin on the Arduino UNO R4.
int tilt_state = digitalRead(8);

Arduino UNO R4 Code - Detecting the tilt

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-sw520d-tilt-sensor */ #define SENSOR_PIN 8 // The Arduino UNO R4 pin connected to 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 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

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the SW520D tilt sensor to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to send the code to the Arduino UNO R4.
  • Tilt the SW520D sensor back and forth.
  • Check the results on the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' 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 R4 WiFi on COM15
2

Troubleshooting

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

  • Check the orientation: The SW520D is sensitive to its mounting orientation. Make sure it is installed in the correct upright position for proper detection.
  • Reduce vibrations: The tilt sensor can pick up vibrations and small movements. It helps to attach it to a sturdy surface to lessen these vibrations.
  • 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 reliable 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!