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

Build it step by step
Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.



| 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.
Build the tilt switch part of this project in EEMENTOR Lab
Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.
Build it yourself
- Wire the parts using the wiring table above. Pins with no wire show red.
- Press Run, click the module to tilt it, and watch the Serial monitor.
- Want the whole project? Add the other parts from the parts list, wire them from the table, then paste this lesson’s sketch into the Code tab and press Compile & Run.
Stuck? See the finished circuit →
EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab
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);
}
What you should see

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).