Arduino Shake Alarm with MPU6050 and Relay
Level 64 · It Shook · 35 minutes
Build a classroom shake alarm: detect vibration, sound a buzzer and switch a warning lamp through a relay.

Preview the project free; a course seat is required to run and submit. No hardware kit needed.
Your project challenge
Read all three acceleration axes. Convert counts to g by dividing by 16384.0. Detect abs(sqrt(x*x+y*y+z*z)−1)>0.15. Keep the alarm on for one second after the last detection: relay D7 LOW and buzzer D8 at 1000 Hz. Otherwise turn both off. Tilt alone must stay quiet.
Components you’ll use
- Arduino Uno
- MPU6050 Tilt Sensor
- Relay Module
- Piezo Buzzer
- Low-voltage DC Lamp
- Battery
What you’ll build
A classroom vibration alarm with a buzzer and a battery-powered warning lamp. Follow all twelve wiring steps. Start the simulation, raise Shake to 100%, then return it to zero. The alarm should stop after one quiet second.
How it works
Gravity is about 1 g even when the sensor is tilted. Read signed X, Y and Z values from register 0x3B. Divide each by 16384.0 and calculate their combined magnitude. Shaking adds acceleration; this model applies an 8 Hz vibration along X.
The key idea
Compare the magnitude with 1 g, not one axis with zero. Remember the last strong reading using millis so brief gaps do not chatter the relay. Try Tilt at either extreme with Shake at zero: it should stay quiet.
Watch out
Wake the sensor at 0x68 first. LOW energises the relay. Its control wiring cannot power the lamp: connect battery + through COM and NO, then return the lamp to battery −. This is a learning model, not an earthquake detector or safety system.
The interactive editor includes the wiring guide, code editor and simulation controls.