Tilt Alarm (Arduino)
Beep when something is tipped or moved, using a ball tilt sensor. Learn to read an orientation switch.
Beginner15 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
tilt_alarm.ino
// Beep when the tilt sensor is tipped past its angle.
const int tiltPin = 2;
const int buzzer = 8;
void setup() {
pinMode(tiltPin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
}
void loop() {
// Upright the ball usually closes the contacts (LOW); tilted = open (HIGH).
if (digitalRead(tiltPin) == HIGH) {
tone(buzzer, 800, 100);
delay(120);
} else {
noTone(buzzer);
}
}
Steps
- Wire the tilt sensor: one leg to D2, the other to GND.
- Wire the buzzer: + to D8, − to GND.
- Upload — tipping the sensor sets off short beeps.
- If it's triggered upright instead, flip the HIGH/LOW logic.