Make a tilt alarm with a buzzer

Use the tilt switch and the active buzzer module to build an alarm that beeps when something is moved.
  • Level:Beginner
  • Duration: 30 min
  • Ages 10+
  • Platform: Arduino

What you'll need

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Make a tilt alarm with a buzzer – EEMENTOR STEM™ lesson

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

Breadboard wiring diagram: Make a tilt alarm with a buzzer
Breadboard view. Check each connection against the wiring table below.

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.

Step 1 of 3: Power rails: 5V and GND from the UNO
Step 1. Power rails: 5V and GND from the UNO
Step 2 of 3: Active buzzer: S → pin 8
Step 2. Active buzzer: S → pin 8
Step 3 of 3: Tilt switch: S → pin 2
Step 3. Tilt switch: S → pin 2
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.

FREE

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.

The parts for this circuit laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. Wire the parts using the wiring table above. Pins with no wire show red.
  2. Press Run, click the module to tilt it, and watch the Serial monitor.
  3. 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.

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

Serial Monitor at 9600 baud printing 1 while upright and 0 while tilted, next to the buzzer's beep pattern: three 150 ms beeps, then again while tilted.

How it works

Inside the tilt switch a metal ball rolls onto or away from two contacts; with INPUT_PULLUP pin 2 reads 1 or 0, and a timing chart shows the three-beep loop.

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 HIGH and LOW in the beep loop.
  • The buzzer only clicks: you're using the passive buzzer. Use the active one, or use tone(BUZZER_PIN, 1000) and noTone(BUZZER_PIN).