STM32F103-Based Home Fire Alarm and Firefighting System
STM32F103-Based Home Fire Alarm and Firefighting System
1. Introduction
1.1 Background With the advancement of technology, significant risks have emerged from industrial production and daily life activities. To detect and report fires early, prevent and mitigate fire hazards, and protect lives and property, it is essential to have reliable fire detection and alarm systems. These systems play a crucial role in safeguarding the progress of socialist modernization by preventing fires from causing devastating incidents like explosions, which could lead to severe economic losses and endanger lives. To reduce such accidents, it is imperative to perform real-time on-site detection of smoke, using advanced and reliable safety monitoring instruments. This ensures that potential hazards are identified early, allowing for timely intervention to prevent accidents, thereby ensuring industrial and home safety. Therefore, research into smoke detection methods and the development of smoke alarms have become a market-driven trend in sensor technology.
1.2 Design Overview The microcontroller and smoke sensor are the two core components of a smoke alarm system. The microcontroller acts as a bridge, connecting sensors with the alarm circuit. In recent years, microcontrollers have been increasingly applied across various sectors of industrial and agricultural production as well as in daily life. Various types of microcontrollers have been developed to meet societal demands. A microcontroller is essentially a device-level computing system, functioning as a microcontroller unit (MCU) or microprocessor. Due to its full functionality, small size, and low cost, it can be applied to all electronic systems. Similarly, it can be widely used in the field of alarm technology to enhance the functionality of alarm devices and significantly improve reliability to meet the needs of societal development. Sensors, being the "sensory organs" of an information technology system, are crucial. Without these "sensory organs" to perceive information, or if they are not sensitive enough, it would be difficult to form a high-precision, high-speed control system. Therefore, selecting a suitable, accurate, and cost-effective smoke sensor and microcontroller chip is essential. The key components in this design are the STM32F103 microcontroller and the MQ-2 semiconductor gas smoke sensor.
1.3 Design Task Analysis Development of a small fire alarm and firefighting system:
- Overall system planning and structural design.
- Using the STM32F103 microcontroller as the central processor, with built-in 12-bit AD conversion, eliminating the need for external AD circuits and reducing potential instability in the hardware circuit. The overall functionality is achieved using a smoke sensor, DHT-11 module, relay module, and voltage reduction module.
- Software programming. The software is divided into main programs, initialization subroutines, serial screen send/receive programs, and alarm limit firefighting subroutines. The programs are written in a modular format, ensuring strong portability and the ability to add sensor modules for future system upgrades.
- Comprehensive hardware and software debugging.
2. Device Selection and Overall System Design
The small fire alarm and firefighting system is designed to detect smoke concentrations in the environment and provide alarm functionality. The basic components of this alarm system include: signal acquisition and analog-to-digital conversion circuits, microcontroller control circuits, character display circuits, audio-visual alarm circuits, and safety protection circuits.
To meet the safety requirements of explosive and flammable smoke in homes and industrial settings, the designed smoke alarm features a display of the alarm status. The alarm adopts a delayed operation mode. The smoke detection alarm is controlled by the STM32F103 microcontroller, using the MQ-2 semiconductor gas smoke sensor to collect smoke concentration information and collaborating with peripheral circuits to form the smoke alarm system. This design includes both hardware and software components.
From the design requirements, the structure must include the following sections: smoke detection (AD acquisition), STM32F103 control unit, temperature and humidity acquisition, display unit, and alarm and firefighting units. The overall circuit block diagram is shown in Figure 1:

The entire system operates under the control of system software. Smoke detection probes placed at monitoring points convert detected smoke into electrical signals, outputting analog signals for the microcontroller to convert into data. Within the microcontroller, the software evaluates the current voltage value and other parameters in real-time to issue a smoke alarm status control signal. This signal drives the buzzer and relay to control a water pump, releasing water to extinguish the fire.
2.1 Smoke Detection Sensor Selection and Introduction
2.1.1 Introduction to Smoke Sensors The smoke sensor is the primary component of the measurement device and control system. The signal acquisition for the small fire alarm and firefighting system is handled by the smoke sensor. Smoke sensors can convert information related to the type and concentration of gases into electrical signals. Based on the strength of these electrical signals, information about the presence of the target gas in the environment can be obtained, thereby enabling detection, monitoring, and alarm functions. Without a precise and reliable sensor, accurate and reliable automatic detection, control, and alarm systems cannot be achieved. As a crucial component in alarms, the smoke sensor determines the accuracy and reliability of the collected smoke concentration signals.
The MQ-2 semiconductor sensor is a metal oxide semiconductor (SnO2) based N-type gas-sensitive element that exhibits low conductivity in clean air. When smoke is present in the environment, the sensor's conductivity increases with the concentration of smoke in the air. In the design of the alarm, a simple circuit can convert the change in conductivity into an output signal corresponding to the gas concentration. This sensor is known for its high sensitivity, large conductivity change, short response and recovery time, strong anti-interference capability, large output signal, long lifespan, and stable operation. It is widely used in the market.
Characteristics of SnO2 semiconductor gas-sensitive elements:
- SnO2 materials have good physical and chemical stability, offering a long lifespan, good stability, and strong corrosion resistance compared to other types of gas-sensitive elements.
- SnO2 gas-sensitive elements have reversible gas detection properties, with short adsorption and desorption times, allowing for continuous long-term use.
- SnO2 gas-sensitive elements have a simple structure, low cost, high reliability, and good mechanical performance. The sensitive element is composed of a miniature AL2O3 ceramic tube, an SnO2 sensitive layer, measuring electrodes, and a heater, fixed within a chamber made of plastic or stainless steel. The heater provides the necessary working conditions for the gas-sensitive element. The encapsulated gas-sensitive element has six pin-shaped leads, four of which are used for signal output, and two for supplying heating current.
The MQ-2 semiconductor gas smoke sensor is suitable for detecting smoke, natural gas, coal gas, hydrogen, alkanes, gasoline, kerosene, acetylene, ammonia, and other gases. It is ideal for detecting flammable gases (such as CH4, C4H10, H2, etc.) and is widely used in household gas leak detectors. It is a low-cost sensor suitable for a variety of applications.
2.1.2 Smoke Sensor Circuit Connection Diagram Due to the differences in physical quantities and measurement ranges, the working principles and structures of sensors vary. Typically, the output electrical signal of a smoke sensor is an analog signal (many new sensors now output digital signals). When the signal value matches the input level of the A/D converter, amplification may not be necessary; otherwise, an amplifier is needed. To convert the smoke signal captured by the MQ-2 semiconductor gas smoke sensor into an electrical signal for the microcontroller, this electrical signal is in the form of voltage. The circuit structure is shown in the diagram.

Pin 4 provides an analog output voltage ranging from 0 to 5V, with higher concentrations resulting in higher voltage. This output voltage is compared using a voltage comparator within the circuit. If the output voltage exceeds the reference voltage, it triggers an output voltage, illuminating an LED and sending a low-level signal to the microcontroller, which can then determine if the concentration is too high. The sensitivity of the MQ-2's TTL output can be adjusted via Rp to control the concentration threshold.
Alternatively, the voltage from pin 4 can be directly connected to the AD chip, converted, and fed into the microcontroller. This design uses the STM32F103 chip, which eliminates the need for an external AD chip. Since the STM32F103 has a reference voltage of 3.3V and the MQ-2 outputs a voltage between 0 to 5V, voltage division should be considered. However, since the 3.3V threshold already represents a high concentration, the design avoids voltage division and directly sends the analog voltage to the microcontroller.
2.2 Display Device
There are many devices available for implementing the display, such as 1602, 12864, and OLED screens. However, these devices only display information and cannot set values, requiring the use of a keyboard, which complicates the circuit design. Therefore, this design utilizes a user-friendly serial screen display. This has two advantages: first, the serial screen display gives the appearance of a complete product, enhancing user-friendliness; second, it can display current temperature, humidity, and concentration threshold voltage while also allowing users to set the threshold, which enables the firefighting function. The serial screen has its own programming code, which allows for data transmission, and the STM32F103 receives the data and converts it into the appropriate format. This approach eliminates the need for a matrix keyboard, resulting in a more aesthetically pleasing design.
2.3 Sound Alarm and Firefighting Circuit
The circuit connects a resistor in series with the base of a transistor, which is then connected to the PB5 port of the microcontroller to control the buzzer. The relay is connected to the PA11 port of the STM32F103 microcontroller. When the smoke sensor's output exceeds the set threshold, the microcontroller sets PB5 and PA11 to low levels, triggering the buzzer and opening the relay to connect the 12V water pump, enabling fire extinguishing. When the value drops below the threshold, PB5 and PA11 are set to high levels, stopping the alarm and water pumping.

Buzzer Alarm Circuit

Relay Water Pump Circuit
2.4 Power Supply Circuit
The system uses a 12V battery to power the water pump, and a voltage reduction module is used to step down the 12V input to a 0-12V adjustable output. The output is adjusted to 3.3V to power the microcontroller, relay, and serial screen.

2.5 Temperature and Humidity Sensor Selection
2.5.1 Comparison of DS18B20 and DHT11 The DHT11 has a limited temperature measurement range of 0 to 50°C, while the DS18B20 has a broader temperature range. However, the DS18B20 only measures temperature, whereas the DHT11 can measure both temperature and humidity. For this experiment, the DHT11 was chosen because it allows for humidity and temperature measurements to jointly determine fire conditions. Given that temperatures above 50°C are unlikely in normal situations, the DHT11 can effectively control the water pump to humidify and cool the environment. Since temperature and humidity measurement are not the key parts of this design, the DHT11 was selected to measure indoor temperature and humidity.
2.5.2 DHT11 Module Introduction The DHT11 digital temperature and humidity sensor is a composite sensor that outputs calibrated digital signals. It uses dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure high reliability and excellent long-term stability. The sensor includes a resistive humidity sensing element and an NTC temperature sensing element.
The DHT11 digital temperature and humidity sensor adopts a single-bus data format, with one data port completing bidirectional data transmission. The data packet consists of 5 bytes (40 bits), with integer and decimal parts. The data format is explained as follows: a complete data transmission consists of 40 bits, with the high bit first.
2.6 Main Control Chip Selection
The STM32F103 chip is the heart of the fire alarm and firefighting system, receiving fire signals and triggering alarms to display and execute the corresponding alarm actions. The control functions implemented by the STM32F103 require the microcontroller to have a high processing speed so that operators and users can promptly observe real-time smoke concentration levels when the alarm system is working normally and take appropriate action. When selecting a microcontroller that meets the computing speed and interface function requirements for the alarm system, cost and size should also be considered. The STM32F103 chip is chosen over the AT89C51 because it has built-in ADC pins, eliminating the need for an external AD0809 chip, which would increase costs and complicate the circuit. Additionally, the STM32F103’s 12-bit ADC offers higher precision than the 8-bit ADC of the AD0809 chip. The STM32F103 is also available in various versions, such as the C8, RC, and ZE series, with the C8 series being more cost-effective and suitable for this design.
3. System Software Design
3.1 Main Program Design and Flowchart The main program flowchart is shown below. The first step is to preheat the sensor, as the MQ-2 semiconductor resistive smoke sensor requires a warm-up period after being powered off for some time before it can accurately collect smoke information. After initialization, the system enters a monitoring state, with the DHT11 continuously reading indoor temperature and humidity.
In the operation of the alarm system, the smoke concentration information is processed by the STM32F103's internal ADC, analyzed by the microcontroller, and used to determine whether to trigger an alarm. In the main program, temperature and humidity can also be used to determine fire conditions, but since the DHT11’s external plastic casing is not suitable for placement near a fire, it only measures indoor temperature and humidity. This is a limitation of the design. The system also displays the current status on a serial screen and allows users to set thresholds, which can trigger water spraying to increase humidity when a button is pressed.






