SW-520D tilt sensor with Arduino

FunduinoLast updated on

Mit zwei Metallkugeln erkennen, ob etwas gekippt wurde

Manchmal reicht eine einzige Information völlig aus: gekippt oder nicht gekippt. Genau dafür gibt es den Neigungssensor SW-520D – ein Bauteil, in dem zwei Metallkugeln rollen und dabei zwei Kontakte öffnen oder schließen.

Für den Arduino verhält er sich damit wie ein Taster, den die Schwerkraft bedient. In dieser Anleitung lässt du eine LED aufleuchten, sobald der Sensor um mehr als etwa 45 Grad geneigt wird.

Small cylindrical component with two lead wires
The SW-520D tilt sensor
Photo of the breadboard with Arduino, tilt sensor, and LED
The completed setup on the breadboard
1 / 2

You need this

SW-520D tilt sensor - mechanical, switching angle 45° SW-520D tilt sensor - mechanical, switching angle 45° 0,70 € *Item no. F23106767About the product — SW-520D tilt sensor - mechanical, switching angle 45°
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 4 parts together 16,46 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Material

For this project, you need:

  • 1× microcontroller with a suitable USB cable
  • 1× breadboard
  • 1× SW-520D tilt sensor
  • 1× LED
  • 1× 220-ohm series resistor for the LED
  • 1× 10 kΩ pull-down resistor for the sensor
  • Male-to-male jumper wires

How the sensor works

There are several ways to measure tilt with an Arduino. An accelerometer provides precise readings at almost any angle. A tilt sensor - also called a tilt switch - is much simpler, but only has two states: tilted or not tilted.

Two small metal balls roll around inside the housing. When the sensor is upright, they rest on the two contacts and connect them - just like a pressed push button, allowing current to flow. If you tilt the sensor by more than about 45 degrees, the balls roll away and the connection is broken.

The setup and sketch are therefore very similar to the project with a simple push button.

Small cylindrical component with two lead wires
The SW-520D tilt sensor
Cross-sectional drawing of the tilt sensor with two balls and two contacts
Inside, two metal balls roll around - when they are on the contacts, the circuit is closed.
1 / 2

Structure

Insert the sensor upright into the breadboard. Connect one lead to 5V and the other to pin 7. Also connect a 10-kilohm resistor from pin 7 to GND.

Connect the LED in series with its 220-ohm resistor to pin 6, and connect the LED’s shorter lead to GND.

The 10 kΩ resistor is not optional. As soon as the balls open the contact, pin 7 would otherwise be left floating and read random values - the LED would then flicker even when you aren't tilting anything. At that moment, the resistor reliably pulls the pin LOW.

Circuit diagram with Arduino, tilt sensor, pull-down resistor, and LED
Circuit diagram: Sensor on pin 7, LED with series resistor on pin 6
Photo of the breadboard with Arduino, tilt sensor, and LED
The completed setup on the breadboard
1 / 2

Switch the LED when tilted

The task: The LED should light up as soon as the sensor is tilted by more than about 45 degrees.

The sketch reads pin 7. When the sensor is upright, digitalRead returns HIGH - the LED stays off. When it is tilted, the value switches to LOW and the LED turns on.

Carefully tilt the breadboard to the side and observe at what angle the LED switches.

Sketch: Turn on LED when tilted
int LED = 6;       // Das Wort "LED" steht jetzt fuer den Wert 6.
int TILT = 7;      // Der Neigungssensor wird an Pin 7 angeschlossen.
int NEIGUNG = 0;   // Hier wird spaeter gespeichert, ob eine Neigung vorliegt.

void setup()
{
  pinMode(LED, OUTPUT);   // Der Pin mit der LED ist ein Ausgang.
  pinMode(TILT, INPUT);   // Der Pin mit dem Neigungssensor ist ein Eingang.
}

void loop()
{
  // Pin 7 auslesen. Steht der Sensor aufrecht, verbinden die Kugeln
  // seine beiden Kontakte: am Pin liegt Spannung an, der Wert ist HIGH.
  // Wird der Sensor gekippt, rollen die Kugeln weg, die Verbindung
  // ist unterbrochen und der Wert ist LOW.
  NEIGUNG = digitalRead(TILT);

  if (NEIGUNG == HIGH)        // Sensor steht aufrecht ...
  {
    digitalWrite(LED, LOW);   // ... dann bleibt die LED aus.
  }
  else                        // Ansonsten ist der Sensor gekippt ...
  {
    digitalWrite(LED, HIGH);  // ... und die LED leuchtet.
  }
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.