A tilt switch contains a tiny metal ball. When the switch tilts, the ball rolls and opens or closes the contact. Combine it with a buzzer and you have an alarm for your desk drawer or treasure box.
What you'll learn
- How to read a digital input
- What a pull-up resistor does
- How to drive an active buzzer
What you need
- UNO R3 board (or any Arduino-compatible board) and USB cable
- Tilt switch module from the 45-in-1 kit
- Buzzer module (the active one, which beeps on its own) from the kit
- 6 jumper wires
Wiring

| Module | Module pin | UNO pin |
|---|---|---|
| Tilt switch | S (signal) | 2 |
| Tilt switch | Middle (+) | 5V |
| Tilt switch | - (ground) | GND |
| Buzzer | S (signal) | 8 |
| Buzzer | Middle (+) | 5V |
| Buzzer | - (ground) | GND |
The UNO has only one 5V pin, so use the breadboard's power rails to share 5V and GND between the two modules.
Step 1: Find the "tilted" value
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
}
void loop() {
Serial.println(digitalRead(2));
delay(200);
}
Open the Serial Monitor. Note whether the value is 0 or 1 when the module stands upright, and what it changes to when you tilt it.
Step 2: The alarm
Set TILTED to the value you saw when the module was tilted.
const int TILT_PIN = 2;
const int BUZZER_PIN = 8;
const int TILTED = LOW; // the value you saw when tilted
void setup() {
pinMode(TILT_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
if (digitalRead(TILT_PIN) == TILTED) {
// beep three times
for (int i = 0; i < 3; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(150);
digitalWrite(BUZZER_PIN, LOW);
delay(150);
}
}
delay(50);
}
How it works
INPUT_PULLUP turns on a resistor inside the UNO that holds the pin at 5 V (HIGH) until the switch connects it to ground. Without it, an unconnected pin "floats" and reads random values. The active buzzer has its own oscillator, so a simple HIGH is enough to make it sound.
Try this next
- Make the alarm keep beeping until you press the button module from the kit.
- Swap the tilt switch for the shock switch or the knock sensor. They work the same way.
Troubleshooting
-
The buzzer beeps all the time: some buzzer modules turn on with LOW. Swap
HIGHandLOWin the beep loop. -
The buzzer only clicks: you're using the passive buzzer. Use the active one, or use
tone(BUZZER_PIN, 1000)andnoTone(BUZZER_PIN).