Projects / Tilt alarm for a bag

Tilt alarm for a bag

Beginner1 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO

    Mount the switch

    Tape it upright inside a box.

  2. NANO

    Wire the switch

    One leg to D2, the other to GND.

  3. NANO

    Wire the button

    One leg to D3, the other to GND.

  4. NANO

    Wire the buzzer

    Plus to D8, minus to GND.

  5. NANO

    Upload the sketch

    Nothing happens until you arm it.

  6. NANO

    Arm it

    Press the button and listen to the ten ticks.

  7. NANO

    Test it

    Tilt the box and hear the alarm.

  8. NANO

    Disarm it

    Hold the button for about one and a half seconds.

In short

How do you build a tilt alarm for a bag?

A tilt switch in a bag beeps when the bag is lifted or turned over while armed, a first motion-sensing project. You need Arduino Nano, Tilt sensor (ball switch, SW-520D), Passive buzzer, Tactile push button 6 mm, 9 V battery, 9 V battery snap with DC jack and Breadboard 830 Tie Points. It takes 8 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Tilt switch leg 1Nano D2Fix the switch upright with tape
Tilt switch leg 2Nano GNDEither leg works
Button leg 1Nano D3Arm and disarm button
Button leg 2Nano GNDUses the internal pull-up
Buzzer plusNano D8Passive buzzer
Buzzer minusNano GNDCommon ground
9 V battery plusNano VINThrough the snap lead
9 V battery minusNano GNDCommon ground

Code

A ball tilt switch is a small can with a metal ball inside. When the can is upright the ball sits across two contacts and the switch is closed, and when you turn it over the ball rolls away and the switch opens. That makes it a simple motion sensor for a bag. This build uses a small state machine with four states: disarmed, arming, armed and alarm. A short press starts a ten second arming countdown, with a tick each second, so you have time to close the bag and put it down. After that the sketch remembers the current switch state as the baseline. If the switch changes, after a short debounce, the buzzer sounds a two-tone alarm. A long press of about a second and a half disarms it. This is a learning project and a deterrent at best: it is not a substitute for a lock or a tracker, and it will trigger on bumps and bus rides. Use it only on your own belongings and do not leave it somewhere it could startle other people.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int TILT = 2, BTN = 3, BUZ = 8;
enum State { DISARMED, ARMING, ARMED, ALARM };
State state = DISARMED;
unsigned long t0 = 0;
bool baseline = false;

bool pressed() { return digitalRead(BTN) == LOW; }

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  pinMode(BTN, INPUT_PULLUP);
  pinMode(BUZ, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  unsigned long now = millis();

  if (pressed()) {
    unsigned long start = now;
    while (pressed() && millis() - start < 1200) { }   // wait to see if it is a long press
    bool longPress = (millis() - start >= 1200);
    while (pressed()) { }                              // wait for release
    delay(50);
    if (state == DISARMED) {
      state = ARMING; t0 = millis();                   // start the 10 second countdown
    } else if (longPress) {
      state = DISARMED; noTone(BUZ);                   // long press disarms
    }
  }

  switch (state) {
    case DISARMED:
      break;
    case ARMING:
      if ((now - t0) % 1000 < 80) tone(BUZ, 1000); else noTone(BUZ);   // tick each second
      if (now - t0 >= 10000) {
        baseline = digitalRead(TILT);
        state = ARMED;
        tone(BUZ, 2000, 300);
      }
      break;
    case ARMED:
      if (digitalRead(TILT) != baseline) {
        delay(50);                                      // debounce the ball
        if (digitalRead(TILT) != baseline) { state = ALARM; t0 = now; }
      }
      break;
    case ALARM:
      tone(BUZ, ((now / 200) % 2) ? 2500 : 1800);       // two-tone beeping
      break;
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Armed and stillSilent
Armed and tiltedAlarm sounds
Long press during alarmAlarm stops
Short press when disarmedTen second countdown ticks
Tilt during the countdownIgnored

If it does not work

Alarm never sounds

The switch was tilted already when arming finished, so the baseline is the tilted state.

Alarm sounds on its own

The switch bounces, move it to a steadier spot.

No sound at all

Check the buzzer polarity and that it is a passive type.

Cannot disarm

Hold the button for longer than a second.

Resets on its own

The battery is low.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the tilt alarm for a bag?

Arduino Nano, Tilt sensor (ball switch, SW-520D), Passive buzzer, Tactile push button 6 mm, 9 V battery, 9 V battery snap with DC jack and Breadboard 830 Tie Points.

How long does the tilt alarm for a bag take to build?

About 60 minutes for a beginner maker, in 8 steps.

How do I know the tilt alarm for a bag works?

Armed and still: Silent. Armed and tilted: Alarm sounds. Long press during alarm: Alarm stops.

Alarm never sounds: what should I check?

The switch was tilted already when arming finished, so the baseline is the tilted state.

Alarm sounds on its own: what should I check?

The switch bounces, move it to a steadier spot.

No sound at all: what should I check?

Check the buzzer polarity and that it is a passive type.

Cannot disarm: what should I check?

Hold the button for longer than a second.