Tilt alarm on an Arduino Uno
Tip the tilt switch and a buzzer and a lamp sound off until the arcade button on pin 3 resets them. Half-size board. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
BeginnerRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Tilt alarm: knock it over and it shouts until you press the big button.
//
// pin 2 the tilt switch, other leg to GND, internal pull-up
// pin 3 the arcade button, other leg to GND, internal pull-up
// pin 8 the buzzer, other leg to GND
// pin 9 the lamp, through 220 ohm to GND
//
// A tilt switch is a metal ball in a can. Upright, the ball rests on both
// contacts and the pin reads LOW; tipped past about fifteen degrees, the ball
// rolls off and the pull-up takes the pin HIGH. That is the whole sensor, and
// the ball rattles for a good few milliseconds as it lands, which is why the
// sketch waits for the reading to hold before it believes it.
//
// # Latching
//
// The alarm does not stop when the box is stood back up. It has seen a tip,
// and it keeps sounding until somebody presses the button, which is what an
// alarm is for: the point is that it happened, not that it is still
// happening. Click the switch on the canvas to tip it, then press the button
// to reset.
const int TILT = 2;
const int BUTTON = 3;
const int BUZZER = 8;
const int LAMP = 9;
const unsigned long SETTLE_MS = 50;
const unsigned long BEEP_MS = 150;
bool alarm = false;
int lastRaw = LOW;
unsigned long changedAt = 0;
int tilt = LOW;
void setup() {
pinMode(TILT, INPUT_PULLUP);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
pinMode(LAMP, OUTPUT);
digitalWrite(BUZZER, LOW);
digitalWrite(LAMP, LOW);
Serial.begin(115200);
Serial.println("Mokxi Uno: tilt alarm armed. Tip the switch.");
}
void loop() {
// Debounce: a reading counts once it has held for SETTLE_MS.
int raw = digitalRead(TILT);
if (raw != lastRaw) {
lastRaw = raw;
changedAt = millis();
} else if (raw != tilt && millis() - changedAt >= SETTLE_MS) {
tilt = raw;
if (tilt == HIGH && !alarm) {
alarm = true;
Serial.println("TILT! alarm on");
}
}
if (digitalRead(BUTTON) == LOW && alarm) {
alarm = false;
digitalWrite(BUZZER, LOW);
digitalWrite(LAMP, LOW);
Serial.println("reset");
while (digitalRead(BUTTON) == LOW) {
delay(10);
}
}
if (alarm) {
int on = (millis() / BEEP_MS) % 2 == 0 ? HIGH : LOW;
digitalWrite(BUZZER, on);
digitalWrite(LAMP, on);
}
delay(5);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Tilt switch
- 1 × Arcade button
- 1 × Buzzer
- 1 × Resistor, 220 Ω
- 1 × LED, red
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 9; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 8; Buzzer pin 1
- Arduino Uno R3 pin 3; Arcade button pin 2
- Arduino Uno R3 pin 2; Tilt switch pin 1
- Ground: Arduino Uno R3 pin GND; Tilt switch pin 2; Arcade button pin 1; Buzzer pin 2; LED, red pin C
- Resistor, 220 Ω pin 2; LED, red pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.