Labs / Bipes + MicroPython / 10 Comprehensive Case

10.4 Fire Safety Sentinel

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

Contents

Young makers, safety always comes first!

Today, we are going to create an extremely practical "Fire Safety Sentinel" πŸš’ β€” it works just like a home smoke alarm, capable of keenly "detecting" open flames and smoke. Once a hazard is detected, it will trigger an alarm, and you can also manually silence the alarm via a button. Through this project, we will not only learn how to combine and use different sensors, but also develop a strong awareness of fire safety~

1. Case Introduction

1.1 Modules Used

1.2 Creative Gameplay

This "little sentry" can stand guard 24 hours a day to monitor hazards:

1.3 Core Learning Points

  1. Master dual-sensor collaborative detection (flame + smoke, covering the two core hazard signals of fires);
  2. Understand the trigger logic of security alerts ("OR condition" judgment: an alert will be triggered as long as there is any hazard signal);
  3. Master clearing the alarm via key press interrupt (no need to wait for the loop to end, the alarm stops immediately when the key is pressed);
  4. Proficient in using infinite loops + state reset (resume monitoring after the alarm is cleared without interrupting the "sentry duty");
  5. Develop programming thinking for fire safety scenarios (integrating code with real-world safety requirements).

2. Wiring Instructions

10.4 Fire Safety Sentinel

This scenario requires connecting 4 modules, and the wiring method of each module is clearly broken down, so even beginners can get the connection right on the first try:

1. Wiring of PT334 Flame Sensor

We connect the PT334 flame 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 threshold judgment.

2. Wiring of MQ-1 Smoke Sensor

We connect the MQ-1 smoke sensor module to the DIO0 interface of the development board, and only use the DIN port (the second pin of the interface) for threshold judgment.

3. Wiring of the mechanical key module

We connect the mechanical key module to the I2C0 interface of the development board.

4. Wiring of Passive Buzzer Module

We connect the passive buzzer module to the DIO1 interface of the development board.

πŸ“Œ Wiring Tips

Important Wiring Knowledge

βœ… Why can a keypad (digital input module) be connected to an I2C interface?

Essentially, the I2C interface is a "multi-purpose universal channel": it can not only transmit dedicated I2C signals, but also recognize simple "high level / low level" just like a DIO port β€” just like a multi-functional classroom at school, which can host professional courses requiring projectors as well as regular Chinese language classes. Since the key module only needs to transmit the simple signal of "pressed = low level / not pressed = high level", connecting it to the I2C interface works perfectly fine.

❌ Why can't I2C modules (such as OLED displays) be connected to ordinary DIO ports?

The I2C module requires the transmission of "bidirectional data with clock synchronization" β€” just as dedicated microscopes and beakers are required for lab classes β€” whereas ordinary DIO ports can only recognize simple high/low logic levels and lack the critical function of "clock synchronization". Connecting them would be equivalent to conducting a chemistry experiment on a regular classroom blackboard: without the core equipment, it is impossible to function properly at all.

Brief summary: Simple "digital signal modules"(buttons, LEDs, ordinary sensors) can be connected to the I2C interface as a makeshift solution, but I2C-specific modules (such as OLED displays, PCA9685 servo drivers) can only be connected to the I2C interface and cannot be connected to ordinary DIO ports!

3. Program Description

10.4 Fire Safety Sentinel

The core of this case follows the logic of "real-time monitoring β†’ hazard alarm β†’ button release β†’ continuous standby", which we break down in the order of building block assembly so that even beginners can understand it step by step:

  1. hardware initialization block
    • Sensor Initialization: Set PT334 (Pin10) and MQ-1 (Pin12) to "input mode + pull-up resistor" to ensure stable voltage level;
    • Key initialization: Set the mechanical key (Pin28) to "input mode + pull-up resistor", and enable the "falling edge interrupt"(triggered immediately when the key is pressed);
    • Buzzer initialization: Set Pin 9 to "output mode + PWM function" to prepare for outputting the alarm sound.
  2. Global state variables (hidden but core)
    • Add an alarm_state variable with its initial value set to false (meaning "no alarm");
    • Function: Remember whether the current state is an alarm state, so as to facilitate the release via key press.
  3. Infinite Loop Block (repeat while true)
    • Function: Enables the system to continuously monitor flame and smoke signals, standing guard without ever stopping;
    • Location: All detection and alarm logics are placed within this loop.
  4. Danger detection and judgment block (if or conditional judgment)
    • Configuration: Judge "PT334 detects flame OR MQ-1 detects smoke"(i. e. read digital input Pin10 = false OR read digital input Pin12 = false);
    • Function: The alarm will be triggered as long as there is any danger signal.
  5. Alert trigger block (within the if branch)
    • Set alarm status: set alarm_state to true;
    • Buzzer Ring: Use the PWM #6 block to output a PWM signal of 2000Hz with a 50% duty cycle, making the buzzer continuously emit a loud alarm sound;
    • Terminal prompt: add print "Danger detected! Fire alarm triggered" to facilitate checking the trigger cause.
  6. Button to Cancel Alarm Block (Interrupt Handling)
    • Configuration: Add an "External Event (Interrupt)" block to the mechanical key, with the trigger mode set to IRQ_FALLING (triggered when the key is pressed);
    • Interrupt Context Operations:
      • Set alarm_state to false;
      • Turn off the buzzer (set output pin Pin9 to false);
      • print "Alarm cleared, confirm hazard handled".
  7. Normal state block (else branch)
    • If no hazard is detected, set alarm_state to false, turn off the buzzer, and print "Environment is safe, continuous monitoring in progress";
    • Add a delay of 500 milliseconds to lower the loop frequency and reduce the power consumption of the development board.

Program running effect:

10.4 Fire Safety Sentinel

Directly importable building blocks:

Fire safety little sentinel.xml 6 KB

A Beginner's Guide to Avoiding Common Pitfalls

  1. Buzzer not sounding / low volume?
    • Check whether the buzzer pin is connected to Pin9, and whether the PWM frequency is set to 1000-3000Hz (the loudest sound falls in this range);
    • Make sure the VCC of the buzzer is connected to 5V; insufficient power supply will result in weak sound.
  2. Can the sensor fail to detect hazards?
    • The PT334 flame sensor should be aligned with the open flame (do not keep it too far away; a distance of 5-10 cm is recommended) to avoid strong light interference;
    • The MQ-1 smoke sensor needs to be preheated for 2-3 minutes first, then blow smoke gently onto it with a lighter (do not burn the module directly).
  3. Does pressing the button fail to deactivate the alarm?
    • Check whether the key has the pull-up resistor enabled (Pull-up= true) and whether the pin is connected to Pin28;
    • Confirm that the interrupt trigger mode is IRQ_FALLING, not IRQ_RISING.

✨ Interactive Mini Quiz

  1. Think about it: What will happen if the "OR" in hazard detection is changed to "AND"? (Answer: The alarm will only be triggered when both flame and smoke are detected simultaneously, which reduces safety, because a single flame or smoke alone is also a danger signal!)
  2. Try it out: Add an "emergency indicator light" to the case β€” when the alarm is triggered, the piranha LEDs will flash synchronously. Can you add the LED block into it?