Đ Án 1 (Thành+Dương)
Đ Án 1 (Thành+Dương)
PROJECT I
Design a device for measuring temparature,
humidity and CO concentration
HO XUAN THANH
[email protected]
VO TU DUONG
[email protected]
HA NOI, 7/2024
Abstract
This project focuses on the design and development of a compact, efficient, and
cost-effective device for measuring environmental parameters: temperature,
humidity, and carbon monoxide (CO) concentration. The device integrates advanced
sensors and microcontroller technology to ensure precise and real-time monitoring.
The primary objectives include achieving high accuracy, low power consumption,
and ease of use. The device features a user-friendly interface and supports wireless
data transmission for remote monitoring. Applications range from residential to
industrial environments, enhancing safety and comfort by providing timely alerts
and historical data analysis. This project aims to contribute to the growing need for
reliable environmental monitoring solutions.
1
TABLE OF CONTENTS
2
LIST OF FIGURES
Figure 2.1: Block diagram of the device ..................................................................... 2
Figure 2.2: Pin configuration ...................................................................................... 3
Figure 2.3: Sensor DHT11 .......................................................................................... 6
Figure 2.4: Communication process of DHT11 and MCU ......................................... 8
Figure 2.5: Start pulse ................................................................................................. 8
Figure 2.6: Response of DHT11 to MCU ................................................................... 9
Figure 2.7: Data indication .......................................................................................... 9
Figure 2.8: a) Structure of MQ-7 b) MQ-7 ............................................................. 10
Figure 2.9: Circuit of MQ-7 ...................................................................................... 11
Figure 2.10: Operation of MQ-7 ............................................................................... 11
Figure 2.11: The typical dependence of the MQ-7 on temperature and humidity.... 12
Figure 2.11: Sensitivity characteristics of MQ-7 ...................................................... 14
Figure 1.13: Real-time DS1307 module ................................................................... 15
Figure 2.14: Heating circuit for MQ-7 ...................................................................... 16
Figure 2.15: LCD 20x04 module .............................................................................. 18
Figure 2.12: a) AMS1117 b) DC-DC LM2596 ................................................. 20
Figure 3.1: Flowchart of subprogram related to sensors ........................................... 21
Figure 3.2: Flowchart for main program ................................................................... 22
3
LIST OF TABLES
4
CHAPTER 1: INTRODUCTION
In recent years, environmental monitoring has become increasingly important due
to the growing awareness of air quality and its impact on health and well-being.
Carbon monoxide (CO), a colorless and odorless gas, poses significant risks even at
low concentrations. It is produced by incomplete combustion processes, such as those
occurring in vehicles, industrial facilities, and residential heating systems. Prolonged
exposure to CO can lead to severe health issues, including headaches, dizziness, and
in extreme cases, death. Therefore, reliable CO detection and monitoring are crucial
for ensuring public safety.
This project aims to develop a system that can accurately measure the concentration
of carbon monoxide (CO) in the air, along with ambient temperature and humidity
levels. By integrating advanced sensors and data processing techniques, the system
will provide real-time monitoring and alerts, enhancing safety and comfort in various
environments. The integration of CO, temperature, and humidity measurements into
a single platform offers a comprehensive solution for environmental monitoring,
contributing to healthier and safer living and working spaces.
In the following sections, we will discuss the technical specifications of the sensors
used, the design and implementation of the measurement system, and the potential
applications and benefits of this integrated monitoring solution.
1
CHAPTER 2: HARDWARE
In this section, we present the detail of the developed system used for measuring
temperature, humidity and CO concentration. This system measure temperature and
humidity via a DHT11 sensor and utilize this information to calibrate in order to
measure CO concentration. Then, 3 main information temperature, humidity and CO
concentration to the display LCD 2004. Furthermore, we use the warning and real-
time system to display the real-time information and inform when the data refer to
that is not safety.
In particular, we first introduce the hardware of the system, then design of the
software for the system is also presented.
• Microcontroller STM32F103C8T6
• Sensor DHT11
• Sensor MQ-7
• LCD 2004 Module
• Real-time Module
• Buzzer/LED
• Other electrical device: Include converter/voltage regulation, resister,
capacitor, …
2
2.1 Microcontroller STM32F103C8T6
A medium density performance line ARM Cortex-M3 32-bit microcontroller in a
48-pin LQFP device is the STM32F103C8T6. High-performance RISC CPU working
at 72 MHz, fast embedded memories, a wide range of improved I/Os, and peripherals
coupled to two APB busses are all included. The 12-bit ADC, three general purpose
16-bit timers plus one advanced-control timer with standard and sophisticated
communication interfaces: two I2Cs and SPIs, three USARTs, a CAN and a
USB. Low power application design is made possible by a wide range of power
saving modes.
The devices function within a power supply range of 2.0 to 3.6 V. They are
available for operation in two temperature ranges: from -40 to +85°C and the extended
range from -40 to +105°C. These microcontrollers offer a wide array of power-saving
modes, enabling the creation of energy-efficient applications.
2.1.1 Structure of hardware STM32F103C8T6:
• Memory Flash & SRAM: 60KB & 20KB respectively
• Operating voltage: 2.0 to 3.6 V
• Clock, reset and supply management
• 37 GPIOs
• Debug mode: Serial wire debug (SWD) and JTAG interfaces
• Up to nine communication interfaces
2.1.2. Pin Configuration STM32F103C8T6:
The STM32F103C8T6 microcontroller comes in a 48-pin LQFP package.
3
Table 2.1: Features of each pin STM32F103C8T6
4
This above table provides a simplified view of the pin functions. The actual pin
functionalities can be more diverse due to the alternate function mapping which can
be configured via software.
Advantages:
5
2.2 Humidity & Temperature Sensor DHT11:
2.2.1. Introduction
The DHT11 is a widely used temperature and humidity sensor known for its
affordability, simplicity, and reliability. It integrates a thermistor and a capacitive
humidity sensor to measure the surrounding air and provides calibrated digital output.
The sensor uses a single-wire digital interface, which simplifies the connection to
microcontrollers and other digital devices. This single-wire interface is not only easy
and quick to implement but also reduces the number of pins required, making it ideal
for projects with limited I/O availability.
6
Table 2.2: The detailed technical specifications of sensor DHT11
The table above outlines the key technical specifications of the DHT11 sensor,
providing comprehensive details on its operational parameters, electrical
characteristics, physical dimensions, and performance metrics. This information is
crucial for understanding the sensor's capabilities and limitations, ensuring accurate
integration and optimal performance in various applications.
7
2.2.4. Communication Process: Serial Interface
The single-bus data format is used for communication and synchronization
between the MCU and the DHT11 sensor. Each communication process takes
approximately 4ms.
The data includes both decimal and integral parts, with a complete transmission
consisting of 40 bits. The data format is structured as follows: 8 bits for the integral
part of the relative humidity (RH), 8 bits for the decimal part of the RH, 8 bits for
the integral part of the temperature, 8 bits for the decimal part of the temperature,
and 8 bits for the checksum.
a) Overview Process
When MCU sends a start signal, DHT11 will change its mode from the low-
power-comsumption mode to the running-mode. When MCU complete the start
signal, DHT11 sends a response signal to MCU to transmit 40-bit data, that include
the temperature and humidity information. Once data is collected, DHT11 will
change to the low-power-consumption mode.
Data Single-bus free status is at high voltage level. To start communication with
DHT11, first we should send the start pulse to the DHT11 sensor. To provide start
pulse, pull down (low) the data pin minimum and then pull up. This process must
take at least 18ms to ensure DHT’s detection of MCU's signal and take 20-40 us to
wait for response.
8
c) Response from DHT11
After getting start pulse from, DHT11 sensor sends the response pulse which
indicates that DHT11 received start pulse. The response pulse is low for 54us and
then goes high for 80us for DHT’s preparation for sending data.
d) Data transmission
After sending the response pulse, DHT11 sensor sends the data, which contains
humidity and temperature value along with checksum.
When DHT is sending data to MCU, every bit of data begins with the 50us low-
voltage-level andthe length of the following high-voltage-level signal determines
whether data bit is "0" or "1"
9
2.3. CO Concentration Sensor MQ7:
The MQ-7 sensor is a type of gas sensor designed to detect the concentration of
carbon monoxide (CO) in the air. This sensor is very popular in safety-related
applications such as gas leak warning systems, air quality monitoring systems, and
other measuring devices. With the ability of detecting CO gas in the range of 10 ppm
to 1000 ppm, MQ7 uses a sensing element made of SnO2 (tin dioxide) whose
conductivity increases when the concentration of CO gas in the air increases. This
change is converted into an electrical signal to measure the gas concentration.
Part Meterial
1 Gas sensing layer SnO2
2 Electrode Au
3 Electrode line Pt
4 Heater coil Ni-Cr alloy
a) 5 Tubular ceramic Al2O3
6 Anti-explosion Stainless steel gauze
network (SUS316 100-mesh)
7 Clamp ring Copper plating Ni
8 Resin base Bakelite
9 Tube Pin Copper plating Ni
b)
Figure 2.8: a) Structure of MQ-7 b) MQ-7
10
b) Measuring circuit
The second is the signal output circuit, it can accurately respond changes of surface
resistance of the sensor. The output signal then feed to the STM32 to measure the CO
concentration.
The MQ-7 sensor requires a heating cycle to operate accurately, usually consisting
of two stages:
• High heating stage (60s): During this period, the sensor is heated to a high
temperature to clean the surface and prepare for gas detection.
11
• Low heating stage (90s): At a lower temperature, the sensor measures the
CO gas concentration in the air.
The high voltage level 5V is used for heating stage, at this period, the feedback
signal form the sensor is not reiable. In the low heating stage, low voltage level at
1.4V is used, the value of sensor can be measured to calculate the CO concentration.
𝑉𝐶 is the voltage source and 𝑉𝑅𝐿 is the voltage of the resistance RL, which is the output
signal can be measured through STM32. The voltage source 𝑉𝐶 = 5V and RL = 10kΩ
are used to design the measure circuit. Then the sensor resistance can be calculated
form Eq.2.3.1 as:
𝑉𝐶 −𝑉𝑅𝐿
𝑅𝑠 = . 𝑅𝐿 (2.3.2)
𝑉𝑅𝐿
After getting 𝑅𝑠 from the Eq.2.3.2 , 𝑅𝑠 must be adjusted to the standard condition
(at temp 200C, humidity 65%) through the dependence relation of sensor resistance
on temparature and humindity.
Figure 2.11: The typical dependence of the MQ-7 on temperature and humidity.
12
Assume that the measured resistance is at the condition 𝑅𝑠 (T0C, RH%) then the
mission is adjusting to the standard condition 𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑 (200C,65%). Overall, this
mission required a mid-processing convert through R0(200C, 33%). First, we build the
equation for the dependence of MQ7 resistance. For all condition of humidity, the
dependence equation has similar form as:
𝑅𝑠
= 𝑘1 . 𝑒 −𝑇 + 𝑘2 , if T < 50C
𝑅0
𝑅𝑠 , if T ≥ 50C
log ( ) = 𝑘1 . log(𝑇) + 𝑘2
𝑅0
𝑅 𝑅 ′ ′
At RH = 33%: log ( 𝑠 ) = 𝑘1′ . log(𝑇) + 𝑘2′ ⇒ ( 𝑠 )33% = 10𝑘1.log(𝑇)+𝑘2 (2.3.4)
𝑅0 𝑅0
𝑅𝑠
From the Equ.(2.3.3) and Equ.(2.3.4) , the ratio of can be computed:
𝑅0
𝑅 𝑅 85−𝑅𝐻 𝑅 𝑅
( 𝑠 )𝑅𝐻% = ( 𝑠 )85% + ( ). [ ( 𝑠 ) − ( 𝑠) ] (2.3.5)
𝑅0 𝑅0 85−33 𝑅0 33% 𝑅0 85%
13
Then the relationship between 𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑 (200C,65%) and R0(200C, 33%) can be
computed:
𝑅 ′ ′
( 𝑠 )33% = 10𝑘1 .log(𝑇)+𝑘2 = 10−0,155.log(20)+0,202 = 1
𝑅0
𝑅
( 𝑠 )85% = 10𝑘1.log(𝑇)+𝑘2 = 10−0,1067.log(20)+0,0682 = 0,85
𝑅0
𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑 𝑅 85−65 𝑅 𝑅
( )65% = ( 𝑠 )85% + ( ). [ ( 𝑠 ) − ( 𝑠) ]
𝑅0 𝑅0 85−33 𝑅0 33% 𝑅0 85%
𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑 85−65
( )65% = 0,85 + ( ). [1 − 0,85] = 0,908
𝑅0 85−33
From Equ.(2.3.5) and Equ.(2.3.6) , adjusted sensor resistance value can be computed:
𝑅𝑠 1 𝑅 85−𝑅𝐻 𝑅 𝑅
( ) = . ( 𝑠 )85% + ( ). [ ( 𝑠 ) − ( 𝑠) ]
𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑 𝑅𝐻% 0,908 𝑅0 85−33 𝑅0 33% 𝑅0 85%
𝑅𝑠
After getting the ratio , we can get the concentration of CO from the sentivity
𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑
characteristic.
14
The concentration from the Figure can be calculated with the formular:
𝑅𝑠
log(𝑝𝑝𝑚) = 𝑘3 . log ( ) + 𝑘4
𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑
Table 2.5: Value of k3, k4 in respect to each rate
𝑅𝑠
rate = Humidity: 33%
𝑅𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑
𝑘3 𝑘4
rate > 1,66 ppm = 50
1< rate ≤ 1.66 -1.3676 2
0,36< rate ≤ 1 -1.3569 2
0,226< rate ≤ 0,36 -1.9681 1.7288
0,09< rate ≤ 0,226 -2.5 1.385
rate ≤ 0,09 ppm = 4000
Due to the low accuracy of real-time clock in STM32, additional module has to be
used to save the real-time information and control the heating circuit.
15
2.5. Heating and convert circuit for MQ7
2.5.1. Heating circuit for MQ7
With the requirement of period heating and cooling condition for MQ7,
specifically:
• High heating stage (60s at 5V): During this period, the sensor is heated to
a high temperature to clean the surface and prepare for gas detection.
• Low heating stage (90s at 1.4V): At a lower temperature, the sensor
measures the CO gas concentration in the air.
Since the output of the STM32 is only 0V-3V, although using PWM method, the
requirement of 5V cannot be reached. Therefore, a heating circuit must be designed
to complete the mission. This circuit is designed mainly based on the OPAMP and
resistor to get the suitable amplifier gain:
The input R1 is the PWM signal with the Duty circle D(%), therefore:
𝑈𝑖𝑛 = 𝐷. 3𝑉
For both cases, to control the heating circuit, we use PWM signal with difference duty
circle:
16
2.5.2. Convert circuit for MQ7
The output of MQ7 has the range from 0V to 5V but the ADC in STM32F103C8T6
is 12 bits and has the maximum input voltage is 3.3V. Therefore, it is necessary to
have a convert circuit to step down the voltage from 5V to 3.3V linearly.
The simple method is using amplifier to reduce the voltage. The circuit has
following form:
𝑅2 𝑉0 3.3 33
= = =
𝑅1 + 𝑅2 𝑉𝑖 5 33 + 17
The system will have the warning message if exist some following condition:
17
2.7. Display LCD20x04 Module
An LCD 20x4 is a type of liquid crystal display that can show 20 characters per line
on 4 lines. It's commonly used in various electronics projects. Its blue or green backlit
screen offers good visibility under different lighting conditions. The LCD 2004 is
highly valued for its ease of use, reliability, and ability to enhance the functionality of
numerous DIY and professional applications.
• VSS: Ground
• VCC: Power (5V)
• Constrast Voltage (VEE): control the bright of display
• Register Select (RS): A signal for selecting registers:
• 1: Data Register (for read and write)
• 0: Instruction Register (for write), Busy flag_Address Counter (for read)
• Read/Write (R/W): Choose read/write mode
• R/W = “H”: Read mode
• R/W = “L”: Write mode
• Enable (E): An enable signal for writing or reading data
• D0-D7: 8-bit bi-directional data bus with 2 mode: 8 bit and 4 bit
• LED+: Power supply for backlight
• LED-: The backlight ground
18
2.7.2. Command Table
Table 2.6: Command table
Instruction Mode
Instruction Discription
RS RW DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
The command table for the LCD 2004 is a set of predefined instructions used to
control the display's behavior.
19
2.8. Power circuit:
Overall, the power circuit must ensure enough power and voltage for all different
input voltage of all devices. Some devices has input power form the STM32F103C8
will not be computed since they had own circuit before. The information input voltage
is listed below:
Almost devices need 5V voltage supply but it’s hard to find exactly 5V voltage
supply. Therefore, 3 battery cells (3.7V for each) were used to generate 12V power
supply for all device. Moreover, a Buck converter DC-DC LM2596 was use to
stepdown voltage to 5V to feed to the device No2,3,4. For STM32F103C8, using a
3.3V voltage regulation AMS1117 to get the input voltage for microcontroller.
a) b)
20
CHAPTER 3: SOFTWARE
We have been developed a software program based on the Keil C. The software sets
up the GPIO pins for communication with both sensors. For the DHT11, a library is
typically used to simplify the process of sending a start signal, reading the 40-bit data
stream, and interpreting the temperature and humidity values. For the MQ-7 CO
sensor, an analog read function is employed to measure the voltage level
corresponding to the CO concentration. The software includes routines to initialize
the sensors, periodically read data from them, and process this data to convert raw
sensor values into meaningful measurements. Data from the DHT11 is converted to
human-readable temperature and humidity values, while the analog value from the
MQ-7 is converted to a CO concentration level using a calibration curve. The software
also handles error checking and data validation to ensure reliable readings. Finally,
the processed data is displayed on an LCD screen.
3.1. Flowchart
a) Flowchart for sensors
a) b)
21
• Subprogram (a) starts with the initiation of the DHT11 sensor. It then waits
for a response from the DHT11. Upon receiving a response, the program reads
the data, checks the validity of the data, and stores the values into variables.
• Subprogram (b) begins by taking values from the ADC (Analog-to-Digital
Converter) and calculating the average value. The program then converts
these values to initial voltage (before amplification by OPAMP). It calculates
the sensor resistor and adjusts it based on temperature and humidity. The next
step is to calculate the ratio Rs/R0 and determine the CO concentration.
Finally, the program returns the concentration value in ppm and stops the
process.
The below flowchart outlines the main program's process. It begins with initializing
the I2C and LCD. The program then checks if the user wants to set up the time. If the
user chooses to set up the time, the program scans the key matrix and saves the time
value. If the user opts not to set up the time, the program proceeds to take the values
of temperature and humidity. Following this, it measures the CO concentration in
parts per million (ppm). The current time, along with the temperature, humidity, and
CO ppm values, are printed on the LCD. Finally, the program waits for a button press
to either restart the process or terminate it, depending on the button's function.
22
CONCLUSION
After have the research on the DHT11 and MQ7, we have a deeper knowledge
about some useful sensor, gain coding and programming technique. For DHT11, we
have deeper learning of the communication protocol between different devices. For
the MQ7, I have explored the general operating principle of gas sensors and their
dependence on external factors such as humidity and temperature.
After completing the project, several drawbacks remain unresolved. The solution
for these problems is recommended to improve the disadvantage.
• First, although the measurement range and accuracy of the DHT11 sensor
have improved compared to the LM35 sensor, in certain conditions, they
are still not wide enough and the accuracy is not high enough. The solution
is that we can consider using another sensor or use better sensor is DHT22
with higher range and accuracy.
• Second, the use of the DC-DC converter (LM2596) only achieved the
condition of reducing the voltage to 5V but did not achieve stability due to
voltage drops when connecting multiple devices together. By adding
voltage regulator 5V, we can improve the voltage supply for devices.
• Third, the usage time is not very long due to the lack of energy optimization
in the program and limitations when connecting to a battery. The power
component can be replaced by a AC-DC converter, convert from 220VAC
to 12VDC to get a sustainable voltage supply.
• Fourth, the CO concentration is not measured very frequently due to the
heating conditions of the MQ7 sensor (typically only able to check about
every 30 seconds, with a 1-minute interval before the next check), leading
to lower efficiency when the system requires continuous monitoring. A
possible solution is to use multiple MQ7 sensors simultaneously in a
staggered manner, meaning they will operate together but have alternating
measurement intervals, sequentially reporting values back to the STM32.
23