Raspberry Pi practice problem · Projects
Bike lock alarm
HardBudget $12Python · gpiozero
A motion alarm for a parked bike: an MPU-6050 (I²C 0x68) on the frame, an arm button on GPIO26, a red armed LED on GPIO17 and an active buzzer on GPIO25 (may draw up to 25 mA).
Tilt comes from the accelerometer: roll = atan2(y, z) and pitch = atan2(x, √(y² + z²)), in degrees.
- A press arms the alarm: LED on, and the current roll and pitch become the reference.
- While armed, the bike counts as moved when roll or pitch is more than 15° away from the reference.
- When it is first moved, give a warning: two short beeps (0.1 s on, 0.1 s off, twice).
- If it is still moved 2 s after the warning started, sound the alarm: buzzer on continuously.
- If it is back within 15° before then, nothing more happens (it can warn again later).
- A press while armed disarms: LED off, buzzer off. Moving the bike while disarmed does nothing.
The bike leans at 5° of roll. The test arms at 1 s, bumps it to 25° at 2 s and back to 6° at 2.8 s, tips the pitch to 30° at 4.5 s, and disarms at 8 s.