Labs / Bipes + MicroPython / 10 Comprehensive Case

10.7 Vibration Alarm Guardian

Author: Li Zisheng Β· Source: FreakStudio open documentation Β· License: CC BY 4.0

Contents

Young makers, have you ever thought about installing a "little security guard against theft" for your books and stationery?

Today we will use the SW-18010P vibration sensor to make a mini vibration alarm guardian πŸ”’ β€” whenever someone secretly moves your belongings, it will immediately trigger a buzzer alarm and light up the red LED. Just like a loyal guardian, it ensures your items will never be taken away without a trace again!

1. Case Introduction

1.1 Modules Used

1.2 Creative Gameplay

Affix the vibration sensor to books, pencil cases or valuables with double-sided adhesive tape, then activate the "Guard Mode":

1.3 Core Learning Points (Essential for Beginners to Master)

  1. Learn the trigger logic of the vibration sensor (outputs a low level when vibration is detected, and uses a "not" operation to determine trigger alarm);
  2. Master the security scenario logic of " trigger alarm + delayed reset ";
  3. Understand the application of the " NOT " logic in sensor detection (for handling sensors triggered by low level);
  4. Implement the linked alarm function of buzzer plus LED (dual reminder of sound and visual);
  5. Develop a programming mindset for security scenarios (converting "object movement" into "alarm signals").

2. Wiring Instructions

This case requires connecting 3 modules according to the standard interfaces of the development board, and you can directly plug them into the corresponding interfaces.

AIN0 DIO0 DIO1

2.1 Wiring of SW-18010P Vibration Sensor

We connect the SW-18010P vibration sensor module to the AIN0 interface of the development board, and only use the DIN pin (the second pin of the module's interface) for vibration detection.

2.2 Wiring of Passive Buzzer Module

We connect the passive buzzer module to the DIO0 interface on the development board.

2.3 Wiring of Piranha LED Light Module

We connect the Piranha LED light module to the DIO1 interface of the development board.

πŸ“Œ Wiring Tips

  1. Sensitivity Adjustment: The potentiometer on the vibration sensor allows adjustment of the detection sensitivity β€” rotating clockwise increases sensitivity (triggers even with minor vibration); rotating counterclockwise decreases sensitivity (requires stronger vibration to trigger).
  2. Fixed sensor: When attaching it to an item, secure it with double-sided adhesive or hot melt glue to avoid looseness that may lead to inaccurate detection.
  3. Notes on Shared Pins: If the buzzer and LED share the DIO0 pin, take care not to let the current be too high (it is recommended to connect them to separate pins, for example, connect the buzzer to DIO0 and the LED to DIO1 for higher stability).
  4. The GND of all modules must be connected to the common ground of the development board; otherwise, the sensor signals will jump erratically, and the buzzer/LED will fail to work.

3. Program Description

The core of this case lies in the process of "continuous vibration detection β†’ alarm triggering β†’ delayed reset". We break it down in the order of building block assembly, so even beginners can follow along step by step:

3 0 20
  1. Hardware Initialization Block (Pre-step, Critical!)
    • Vibration sensor initialization: Set the AIN0 interface to "input mode + pull-down resistor (Pull-up= false)" to ensure that it outputs a high level when there is no vibration and a low level when triggered.
    • Buzzer / LED Initialization: Set the DIO0 interface to "output mode + PWM function" to prepare for controlling the buzzer to sound and the LED to turn on and off.
  2. Infinite Loop Block (repeat while true)
    • Function: Enables the system to continuously detect vibration signals, standing guard around the clock to protect items.
    • Location: All detection and alarm logic are placed inside this loop.
  3. Vibration detection judgment block (if not read digital input)
    • Configuration: Detect the level of the AIN0 interface, not means "when the sensor outputs a low level (i. e., vibration is detected)", it is judged as "true".
    • Logic: When the SW-18010P detects vibration, it outputs a low level;not low level = true, triggering an alarm; when no vibration is detected, it outputs a high level, not high level = false, maintaining the normal state.
  4. Alarm trigger block (if within the branch)
    • Terminal prompt: print "Trigger spring"(can be modified to "Vibration detected! Alarm triggered" for better intuitiveness);
    • Buzzer Ring: PWM #7 block sets the frequency to 1000Hz and the duty cycle to 800, making the buzzer continuously emit a loud prompt tone;
    • Red light turns on: set output pin DIO0 to true, to turn on the red LED;
    • Delay Reset: delay 3 seconds, which enables the alarm to automatically resume after lasting for 3 seconds.
  5. Normal state block (else branch)
    • Buzzer Mute: PWM #7 The block sets the duty cycle to 0 to stop sound output;
    • Red light turns off: set output pin DIO0 to false, turn off the LED;
    • Debounce Delay: delay 20 milliseconds, which prevents frequent detection caused by minor jitter and makes the system more stable.

Program running effect:

C2184.MP4 60.0 MB

Directly importable building blocks:

Vibration alarm little guard.xml 5 KB

A Beginner's Guide to Avoiding Common Pitfalls

  1. Is the vibration detection insensitive / false triggers occur frequently?
    • Adjust the potentiometer on the sensor: if the sensitivity is too high (false triggering), turn it counterclockwise to decrease the sensitivity; if the sensitivity is too low (failure to detect), turn it clockwise to increase the sensitivity.
    • Ensure that the sensor is firmly attached to the object to avoid insufficient vibration transmission caused by loosening.
  2. Buzzer does not sound / LED does not light up?
    • Check whether the pins are connected correctly (DIO0) and whether the PWM frequency is in the range of 1000-3000Hz (the buzzer produces the clearest sound within this range).
    • If shared pins result in insufficient current, it is recommended to connect the buzzer and the LED to separate DIO ports (e. g., connect the buzzer to DIO0 and the LED to DIO1).
  3. Is it impossible to reset automatically after an alarm is triggered?
    • Check delay 3 seconds whether the block is correctly added to the alarm branch, and ensure that the reset logic is executed after the alarm lasts for 3 seconds.

✨ Interactive Mini Quiz

  1. Think about it: Why do we use " not read digital input " to detect vibration in the program, instead of directly using " read digital input "? (Answer: Because the SW-18010P outputs a low level when it detects vibration, and only when not low level = true can the alarm be triggered; if you directly use read digital input, it will be judged as false at low level, which will not trigger the alarm instead)
  2. Try it out: add an "alarm count recording" function to the case β€” use the variable count to record the number of triggers. After each alarm, print "Alarm has been triggered X times". Can you add a variable increment block in the alarm branch?