Projects / Bike lock tamper alarm

Bike lock tamper alarm

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNO

    Load ArduinoISP on the Uno

    Examples, ArduinoISP.

  2. UNOATTINY85

    Wire the ATtiny85 to the Uno

    Pins as in the table.

  3. UNO

    Burn the bootloader

    Sets 8 MHz internal clock.

  4. UNO

    Upload the sketch

    Using Upload Using Programmer.

  5. UNO+

    Move it to the cell

    Disconnect the Uno.

  6. UNO

    Test with a tap

    Three taps start the siren.

  7. UNO+

    Measure sleep current

    Multimeter in series with the cell.

  8. UNO

    Mount it in a box on the bike

    Away from the rain.

In short

How do you build a bike lock tamper alarm?

A vibration sensor and an ATtiny85 sleep on one cell, and a loud buzzer sounds when someone shakes or hits the lock three times. You need ATtiny85-20PU microcontroller, Vibration sensor module (SW-420), Buzzer 12 V continuous (piezo alarm), BC547 NPN transistor, Resistor 1 kΩ, LED 5 mm red, Resistor 330 Ω, Tactile push button 6 mm, 18650 cell 2600 mAh (branded), Battery holder 18650 single, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and Arduino Uno R3. It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
13

Parts list

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

Wiring

FromToWhy
ATtiny85 pin 8 (VCC) and pin 4 (GND)18650 holder plus and minus (use a cell with a protection board)Power, 3 to 4.2 V
SW-420 VCC, GNDBattery plus and minusSensor power
SW-420 DOATtiny85 pin 7 (PB2)Vibration input, wakes the chip
Push buttonATtiny85 pin 2 (PB3) to GNDSilence and disarm, internal pull-up
LED anodeATtiny85 pin 5 (PB0) through 330 Ω, cathode to GNDArmed blink
ATtiny85 pin 6 (PB1)1 k to BC547 baseBuzzer switch
BC547 emitterGNDSwitch ground
Buzzer plusBattery plusSupply
Buzzer minusBC547 collectorSwitched by the transistor
Uno as ISP (D13, D12, D11, D10 to pins 7, 6, 5, 1, plus 5 V and GND)Programming only, remove afterwardsATtiny85 pin 7 SCK, pin 6 MISO, pin 5 MOSI, pin 1 RESET

Code

The ATtiny85 spends almost all its time in power-down sleep, where it draws well under a microamp. A pin-change interrupt on the vibration pin wakes it. When woken it counts how many times the sensor flips in the next eight seconds, and three or more flips mean real shaking rather than a passing bump, so the siren sounds for thirty seconds. The push button silences it. The weak point for battery life is the SW-420 module: its comparator and power LED draw a few milliamps all the time, so one 2600 mAh cell lasts roughly a month, not years. Desolder the module's power LED to help, and measure the real current with a multimeter in series (the last learning step). Some SW-420 boards output high at rest and low on vibration, and the code counts either edge, so both types work. The 12 V piezo is only quietly loud on 4 V; swap in a 5 V active piezo buzzer if you want more volume. Use a protected 18650 so the cell cannot be over-discharged. Program the chip once with an Uno as ISP: install ATTinyCore, choose ATtiny85, 8 MHz internal clock, select "Arduino as ISP" and run Burn Bootloader first. Put the unit in a small sealed box zip-tied to the frame, and treat it as a deterrent, not a substitute for a proper lock.

Runs on a virtual ATtiny85 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).

#include <avr/sleep.h>
#include <avr/interrupt.h>

// ATtiny85 pin names (PB numbers used by ATTinyCore)
const uint8_t LED = 0, BUZ = 1, VIB = 2, BTN = 3;
const uint8_t HITS_TO_ALARM = 3;                 // edges from the sensor inside the window
const unsigned long WINDOW_MS = 8000UL;          // how long we count after waking
const unsigned long ALARM_MS = 30000UL;          // siren length

ISR(PCINT0_vect) {}                              // only needed to wake the chip

void sleepNow() {
  ADCSRA &= ~(1 << ADEN);                        // ADC off saves power
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sei();
  sleep_cpu();
  sleep_disable();
}

void alarm() {
  unsigned long t = millis();
  while (millis() - t < ALARM_MS) {
    digitalWrite(BUZ, HIGH);
    digitalWrite(LED, HIGH);
    delay(250);
    digitalWrite(BUZ, LOW);
    digitalWrite(LED, LOW);
    delay(100);
    if (!digitalRead(BTN)) break;                // button silences it
  }
  digitalWrite(BUZ, LOW);
  digitalWrite(LED, LOW);
}

void setup() {
  pinMode(BUZ, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  pinMode(VIB, INPUT);
  GIMSK |= (1 << PCIE);                          // enable pin change interrupts
  PCMSK |= (1 << PCINT2) | (1 << PCINT3);        // sensor (PB2) and button (PB3)
  for (uint8_t i = 0; i < 10; i++) {             // ten seconds to walk away
    digitalWrite(LED, HIGH); delay(100);
    digitalWrite(LED, LOW);  delay(900);
  }
}

void loop() {
  sleepNow();                                    // wake on any change
  if (!digitalRead(BTN)) {                       // button: acknowledge and re-arm
    while (!digitalRead(BTN)) delay(10);
    delay(50);
    return;
  }
  uint8_t hits = 1;
  bool last = digitalRead(VIB);
  unsigned long t = millis();
  while (millis() - t < WINDOW_MS) {
    bool now = digitalRead(VIB);
    if (now != last) { hits++; last = now; delay(20); }
    if (!digitalRead(BTN)) return;
  }
  if (hits >= HITS_TO_ALARM) alarm();
}

Test checklist

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

CheckExpected
Power onLED blinks for ten seconds, then quiet
Tap the lock onceNo siren
Shake the lock firmly for a few secondsSiren sounds for thirty seconds
Press the button during the sirenSiren stops
Measure standby currentFew milliamps, mostly the sensor module

If it does not work

Siren never starts

Sensor sensitivity screw on the SW-420 too low; turn it until it triggers on a firm tap.

Siren starts at random

Too sensitive; turn the screw the other way.

Upload fails

Check the ISP wiring and the 10 uF capacitor trick between Uno RESET and GND during upload.

Chip does not wake

Pin-change mask or wiring; confirm the sensor is on pin 7 (PB2).

Buzzer weak

Normal at 4 V; try a 5 V active buzzer.

Cell drains fast

Module LED and comparator; remove the LED.

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

Ask Lark AI

Common questions

What parts do I need for the bike lock tamper alarm?

ATtiny85-20PU microcontroller, Vibration sensor module (SW-420), Buzzer 12 V continuous (piezo alarm), BC547 NPN transistor, Resistor 1 kΩ, LED 5 mm red, Resistor 330 Ω, Tactile push button 6 mm, 18650 cell 2600 mAh (branded), Battery holder 18650 single, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and Arduino Uno R3.

How long does the bike lock tamper alarm take to build?

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

How do I know the bike lock tamper alarm works?

Power on: LED blinks for ten seconds, then quiet. Tap the lock once: No siren. Shake the lock firmly for a few seconds: Siren sounds for thirty seconds.

Siren never starts: what should I check?

Sensor sensitivity screw on the SW-420 too low; turn it until it triggers on a firm tap.

Siren starts at random: what should I check?

Too sensitive; turn the screw the other way.

Upload fails: what should I check?

Check the ISP wiring and the 10 uF capacitor trick between Uno RESET and GND during upload.

Chip does not wake: what should I check?

Pin-change mask or wiring; confirm the sensor is on pin 7 (PB2).