Raspberry Pi Pico - SW520D Tilt Sensor

This guide will teach us how to operate the Raspberry Pi Pico and a SW520D tilt sensor to detect tilt. We will discuss the following points:

Raspberry Pi Pico SW520D tilt sensor

Later, you can change the code to make an LED or a light turn on (using a relay) when it detects tilt, or to make a servo motor rotate.

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×SW520D Tilt Sensor Module
1×Jumper Wires
1×Breadboard
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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

You can use a SW520D tilt sensor 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

The SW520D tilt sensor has three pins:

  • VCC pin: Connect this to VCC, between 3.3V and 5V.
  • GND pin: Connect this to GND, which is 0V.
  • DO pin: This is the output pin. It is HIGH when the sensor is upright and LOW when it is tilted. Connect it to the input pin on your Raspberry Pi Pico.
SW520D Tilt Sensor Pinout
image source: diyables.io

The SW520D tilt sensor module also has two LED indicators.

  • One LED light shows power is on
  • One LED light for tilt state: it lights up when upright, turns off when tilted

How It Works

The module uses a simple ball switch mechanism.

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

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico SW520D Tilt Sensor

This image is created using Fritzing. Click to enlarge image

How To Program For SW520D Tilt Sensor

  • Sets the Raspberry Pi Pico pin as a digital input.
sensor_pin = Pin(SENSOR_PIN, Pin.IN)
  • It checks the status of a pin on the Raspberry Pi Pico.
tilt_state = sensor_pin.value()

Raspberry Pi Pico Code - Detecting the tilt

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-sw520d-tilt-sensor """ from machine import Pin import time SENSOR_PIN = 0 # The Raspberry Pi Pico pin (GP0) connected to DO pin of the SW520D tilt sensor prev_tilt_state = 1 # the previous state from the input pin tilt_state = 1 # the current reading from the input pin # Initialize the sensor pin as an input sensor_pin = Pin(SENSOR_PIN, Pin.IN) # Main loop while True: # Read the state of the input pin tilt_state = sensor_pin.value() if prev_tilt_state == 1 and tilt_state == 0: print("The tilt has been detected") elif prev_tilt_state == 0 and tilt_state == 1: print("The tilt has disappeared") # Save the last state prev_tilt_state = tilt_state time.sleep(0.1) # Delay for 100 milliseconds

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the SW520D tilt sensor to the Raspberry Pi Pico according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Tilt the SW520D sensor back and forth.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The tilt has been detected The tilt has disappeared The tilt has been detected The tilt has disappeared
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonny's Shell.

Troubleshooting

If the SW520D tilt sensor is not functioning properly, follow these steps:

  • 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: The tilt sensor might react to mechanical vibrations. Fix it firmly to a stable surface to avoid false triggers.
  • Check the wiring: Make sure the VCC, GND, and DO pins are connected correctly.
  • Check the power supply: Ensure that the power supply is consistent for reliable readings.

Video Tutorial

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!