An Arduino tilt alarm with a ball tilt sensor
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
A tilt sensor is a tiny metal can with a ball inside. Stand it upright and the ball rests on two contacts, closing a circuit. Tip it past about fifteen degrees and the ball rolls off, and the circuit opens. There is no chip and no power pin. It is a switch that gravity presses, and the SW-520D is the one in most kits.
The circuit above is an Uno with a tilt switch on pin 2, a big arcade button on pin 3, a buzzer on pin 8 and a red lamp on pin 9. Click the tilt switch to tip it over and the alarm goes off. Stand it back up and the alarm keeps going until you press the button. It runs in your browser on the sketch below.
What you need
- An Arduino Uno
- A ball tilt switch (SW-520D or a KY-020 module)
- A pushbutton (an arcade button here)
- An active buzzer
- A red LED and a 220 ohm resistor
- A half-size breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| Tilt switch, one leg | Pin 2 | Read with INPUT_PULLUP |
| Tilt switch, other leg | GND | Either leg can go either way |
| Button, one leg | Pin 3 | Also INPUT_PULLUP |
| Button, other leg | GND | |
| Buzzer + | Pin 8 | Buzzer - to GND |
| Red LED | Pin 9 | Through a 220 ohm resistor to GND |
Reading it like a button
Electrically a tilt switch is a button that stays pressed while it is upright. So it is wired like one: one leg to a pin, the other to ground, and pinMode(TILT, INPUT_PULLUP) so the pin is never left floating. Upright, the ball connects the legs and the pin reads LOW. Tipped, the switch opens, the pull-up wins and the pin reads HIGH.
A sensor module such as the KY-020 does the same thing with a resistor on the board and a signal pin. Read its signal pin the same way.
People use tilt switches for anything that only needs to know "upright or not": a box that has been knocked over, a lamp that turns on when you flip it, a game that starts when you shake it, or a heater that cuts out if it falls. Because it needs no power of its own, it is also a good way to wake a sleeping board.
Why it needs debouncing
A ball landing on a contact rattles. For a few milliseconds the pin flips between HIGH and LOW before it settles, and a sketch that reacts to the first change would fire several times for one tip. The simulated switch rattles too: by default it makes and breaks three times over 20 milliseconds each time it moves.
So the sketch only believes a reading once it has held for 50 milliseconds. You can see that wait in the circuit: even with the rattle turned off, the "TILT! alarm on" line does not appear until 50 milliseconds after the tip.
int raw = digitalRead(TILT);
if (raw != lastRaw) {
lastRaw = raw;
changedAt = millis();
} else if (raw != tilt && millis() - changedAt >= SETTLE_MS) {
tilt = raw;
if (tilt == HIGH && !alarm) {
alarm = true;
Serial.println("TILT! alarm on");
}
}
if (digitalRead(BUTTON) == LOW && alarm) {
alarm = false;
digitalWrite(BUZZER, LOW);
digitalWrite(LAMP, LOW);
Serial.println("reset");
while (digitalRead(BUTTON) == LOW) {
delay(10);
}
}Why the alarm latches
The alarm does not stop when you stand the switch back up. Once it has seen a tip, it beeps and flashes every 150 milliseconds until somebody presses the button, and then it prints "reset". That is what you want from an alarm: the point is that the box was moved, not that it is still tipped.
The little while loop after the reset waits for the button to be let go, so one long press does not reset the alarm over and over.
Try it in the editor
Click the tilt switch and set its settle property to 0 to turn the rattle off, or to 100 for a slow, noisy ball. The alarm still goes off once for each tip, because the sketch waits for the reading to hold for 50 milliseconds before it believes it.
Make it a gentle reminder instead: stop the alarm on its own when the switch is upright again, by setting alarm to false when tilt goes back to LOW.
Common mistakes
No pull-up. With pinMode(TILT, INPUT) and no resistor, a tipped switch leaves the pin floating, and it reads random values.
Reading the logic upside down. With INPUT_PULLUP, upright is LOW and tipped is HIGH.
Debouncing for microseconds. A tilt switch settles in tens of milliseconds. Wait 20 to 50 milliseconds before you trust a change.
Expecting an angle. A tilt switch is on or off. To measure how far something leans, use an accelerometer such as the MPU6050.
Questions
How do I connect a tilt sensor to an Arduino?
One leg to a digital pin and the other to GND, then pinMode(pin, INPUT_PULLUP). The pin reads LOW upright and HIGH when tipped. A three-pin module goes to 5 V, GND and a digital pin.
What angle does a tilt switch trigger at?
A typical ball tilt switch opens past about fifteen degrees, but it varies from part to part. The simulated one has no angle at all: you click it to tip it.
Can a tilt sensor tell which way it was tipped?
No, it only knows upright or not. For direction and angle, use an accelerometer. The MPU6050 tutorial reads one.
Do I need a library for a tilt sensor?
No. It is digitalRead() on one pin, plus a short debounce like the one above.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.