Skip to content

Sensors & Signal Processing

Build an Arduino shake alarm: catch the jolt, latch it

To build an Arduino shake alarm, read an accelerometer, average a few readings to tame the noise, and compare each average with the last one. A jump bigger than a threshold is a shake: set an alarm flag that stays set until a reset button clears it. A slow tilt changes the reading too, but only a little each pass, so it never trips.

Parts and wiring

  • Accelerometer on a parcel, its analog output on A2: noisy at rest, a jump when shaken, a slow drift when tilted
  • Alarm lamp or buzzer on pin 9
  • RESET push-button from pin 2 to GND, with INPUT_PULLUP

The plan

  1. Tame the noise: average 4 readings each pass.
  2. Catch the shake: if this average differs from the last by more than 100, raise the alarm and say shake once.
  3. Remember it: a bool alarm, set by the shake and cleared only by RESET.
  4. Show it: write the lamp from alarm every pass, with only a short delay(10) so no shake is missed.

The code

int smooth() {
  long sum = 0;
  for (int i = 0; i < 4; i++) sum = sum + analogRead(A2);
  return sum / 4;
}

void loop() {
  int reading = smooth();
  if (abs(reading - last) > 100) {
    if (!alarm) Serial.println("shake");   // say it once
    alarm = true;
  }
  last = reading;
  if (digitalRead(2) == LOW) alarm = false; // RESET
  digitalWrite(9, alarm ? HIGH : LOW);
  delay(10);
}

Set last = smooth() in setup() too, or the first pass compares a real reading with 0 and raises a false alarm.

Live experiment

Test the alarm first

A second at rest, a shake at 1 s, RESET at 2.5 s, then a slow tilt. Pick the line on the jump and whether the alarm remembers. A line at 25 trips on the noise, 450 misses the shake's jump of about 412, and an alarm that only follows the jump is off a pass later. 100 with a latch passes all four checks.

Try it yourself: free lesson

“Project: Shake alarm” is the project that ends Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.

Why each piece is there

  • The average shrinks the noise, so the delta at rest stays around 30 at most. Without it, single readings wobble enough that a sensible line would trip.
  • The delta ignores where the reading is and looks only at how fast it moved. A shake jumps about 400 in one pass; a slow tilt moves it about 20 at a time.
  • The latch keeps the alarm on. The jump lasts one sample, so an alarm that just follows it would flash for 10 ms and vanish.

Testing it

Leave it still for a second: no alarm. Shake it: alarm on, and still on a second later. Press RESET: off. Tilt it slowly: no alarm. Those four checks cover the noise, the jump, the latch and the drift.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Project: Shake alarm” is the project that ends Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The parcel is tilted slowly, 20 counts a step. The averaged reading's delta each pass is well under 100, so…
    Show the answer

    Answer: No alarm: a slow tilt isn't a shake. The detector is on delta, and a slow change makes small deltas. Only a jump makes one over 100.

    • Why not “The lamp flickers”? Nothing crosses the line, so nothing touches the lamp; it stays off.
    • Why not “The alarm goes off”? The alarm is on delta, and every delta of a slow tilt is well under the line of 100.
    • Why not “The total resets”? There's no total in a delta rule, only this reading minus the last.
  2. Question 2 of 3The jump lasts one sample but the alarm must stay on until RESET. So the alarm is…
    Show the answer

    Answer: A bool set by the jump and cleared only by the button. A latch: a variable that remembers. The jump sets it true; nothing but the button sets it false; the lamp follows it every pass.

    • Why not “The average”? The average is the reading, not the alarm. It settles back as soon as the shake ends.
    • Why not “The delta itself”? The delta is big for one sample only, so an alarm that follows it is off again a pass later.
    • Why not “A delay(500)”? A delay gives half a second of alarm and then it's off, and it freezes the program meanwhile.
  3. Question 3 of 3A delta rule catches…
    Show the answer

    Answer: Sudden jumps, and ignores slow drift. Big delta, sudden change.

    • Why not “Every reading”? Only a reading that jumps past the line since the last one fires it.
    • Why not “Slow drift”? A slow drift makes many small deltas, each under the line, so a delta rule ignores it.
    • Why not “Only zero”? A delta of zero means nothing changed. The rule fires on big deltas.
Keep practising: free lesson

“Project: Shake alarm” is the project that ends Signal Sands, Sensors & Signal Processing World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.

Questions people ask

Which accelerometer should I use for a shake alarm?

Any will do. An analog one such as the ADXL335 gives a voltage per axis that analogRead can measure directly. Digital ones such as the ADXL345 or MPU-6050 talk over I2C and need a library, but the alarm logic is the same: average, delta, latch.

Why does my shake alarm go off by itself?

The threshold sits inside the noise. Even averaged, the reading wobbles from pass to pass, and if that wobble ever beats the line, it trips. Print the deltas at rest, set the line well above the biggest, or average more readings.

Why not just alarm when the reading passes a level?

Because tilting the parcel changes one axis's reading as gravity moves onto it, so a level threshold trips on a gentle tilt. A delta threshold only fires on a fast change, which is what a shake is.

Every topic