Arduino MKR WiFi 1010 - SW520D Tilt Sensor

A SW520D tilt sensor is a fascinating component that detects orientation changes and tilt movements! You can create amazing tilt-activated projects with the Arduino MKR WiFi 1010 and a SW520D tilt sensor, like an alarm that activates when an object is moved, a display that changes when you tilt it, or even a servo motor that responds to orientation changes.

In this comprehensive guide, you'll learn how to use the Arduino MKR WiFi 1010 with a SW520D tilt sensor to build interactive projects. We'll cover everything you need to know about connecting the SW520D tilt sensor to the Arduino MKR WiFi 1010 and programming it to respond to tilt input.

What You'll Learn:

Real-World Applications:

Once you've mastered the basics, you can easily extend your Arduino MKR WiFi 1010 SW520D tilt sensor project to control LEDs, activate relays for high-power devices, or even trigger servo motors based on detected tilt!

Arduino MKR WiFi 1010 SW520D tilt sensor

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×SW520D Tilt Sensor Module
1×Breadboard
1×Jumper Wires

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 is a simple yet clever component that detects tilt and orientation changes. Think of it as an electronic "level sensor" for your Arduino MKR WiFi 1010 projects!

Inside the module, there is a small metal ball that rolls between two electrical contacts. When the sensor is upright, the ball rests against the contacts, closing the circuit. When the sensor is tilted past a certain angle, the ball rolls away, opening the circuit and changing the digital output.

The SW520D Tilt Sensor Pinout

The SW520D tilt sensor module has three pins that you'll connect to your Arduino MKR WiFi 1010:

  • VCC pin: Connects to your power supply. Works with both 3.3V and 5V, making it compatible with the Arduino MKR WiFi 1010's voltage levels.
  • GND pin: Connects to ground (0V) - complete the circuit by connecting this to your Arduino MKR WiFi 1010's GND pin.
  • DO pin: The digital output pin that sends the detection signal to your Arduino MKR WiFi 1010. This pin stays HIGH when the sensor is upright and goes LOW when it is tilted. Connect this to any digital input pin on your Arduino MKR WiFi 1010.
SW520D Tilt Sensor Pinout
image source: diyables.io

Helpful Features:

  • LED indicators: Two helpful LEDs on the board:
    • Power LED: Lights up when the module is powered, confirming proper connection
    • Status LED: Reflects the tilt state, making it easy to verify the sensor's behavior during testing

    How It Works

    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 MKR WiFi 1010 SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW520D Tilt Sensor

  • This sets the Arduino MKR WiFi 1010 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 MKR WiFi 1010 pin is HIGH or LOW using the digitalRead() function.
int tiltState = digitalRead(D3);

Arduino MKR WiFi 1010 Code - Detecting the tilt

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

Detailed Instructions

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  • Connect the components to the Arduino MKR WiFi 1010 board as depicted in the diagram
  • Plug your Arduino MKR WiFi 1010 into your computer's USB port
  • Launch the Arduino IDE on your computer
  • Select the Arduino MKR WiFi 1010 board and its 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 MKR WiFi 1010.
  • 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 MKR WiFi 1010
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino MKR WiFi 1010' 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 MKR WiFi 1010 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. Small adjustments to the mounting angle can significantly affect behavior.
  • Reduce vibrations: Mechanical shaking can cause the ball to bounce between states. 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!