Interfacing MH-Z19 Infrared CO2 Sensor Module with Arduino
MH-Z19 Infrared CO2 Sensor Module
The MH-Z19 Infrared CO2 Sensor Module is designed to detect carbon dioxide concentration in the air. It's widely used in indoor air quality monitoring, HVAC systems, and smart agriculture to measure CO2 levels accurately.
Working Principle of MH-Z19 CO2 Sensor
The MH-Z19 sensor operates based on non-dispersive infrared (NDIR) technology. It measures the absorption of infrared light by CO2 molecules, allowing for reliable and precise CO2 concentration readings.
Types of CO2 Sensors
NDIR CO2 Sensor
- Emits infrared light through a gas sample.
- CO2 molecules absorb specific wavelengths of light.
- The sensor calculates the concentration based on absorption.
Chemical-based CO2 Sensor
- CO2 reacts with a chemical compound in the sensor.
- The reaction changes conductivity or pH.
- The output signal correlates to CO2 concentration.
Requirements
1. Arduino
2. MH-Z19 CO2 Sensor Module
3. 100 ohm resistor
4. Jumper wires
Pin Configuration of MH-Z19 CO2 Sensor
MH-Z19 Sensor Module
- VCC: Connect to +5V on Arduino.
- GND: Connect to GND on Arduino.
- TX: Connect to Arduino RX for serial communication.
- RX: Connect to Arduino TX for sending commands.
Wiring the MH-Z19 CO2 Sensor to Arduino
To connect the MH-Z19 CO2 Sensor to Arduino, connect the VCC and GND to +5V and GND respectively. Use the TX and RX pins for serial communication, ensuring proper data exchange between the sensor and Arduino.
Algorithm
Initialize Components
- Connect the VCC and GND pins of the MH-Z19 sensor to +5V and GND on the Arduino.
- Connect the TX and RX pins to Arduino RX and TX respectively.
Write the Code
- Set up serial communication in the setup() function.
- Send read commands to the sensor in the loop() function.
- Parse the serial data to extract CO2 concentration values.
Display Values or Control Devices
- Print the CO2 values to the serial monitor.
- Use the readings to trigger ventilation or alerts when CO2 levels exceed a threshold.
Test the Project
- Upload the code to the Arduino.
- Expose the sensor to different air samples and observe the changes in CO2 readings.
1#include <SoftwareSerial.h>
2
3// Define software serial pins (you can use other digital pins too)
4SoftwareSerial mySerial(10, 11); // RX, TX
5
6byte cmd_get_sensor[] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; // Command to read CO₂
7unsigned char response[9]; // Response buffer
8
9void setup() {
10 Serial.begin(9600); // Start Serial Monitor
11 mySerial.begin(9600); // Start communication with MH-Z19 sensor
12 Serial.println("MH-Z19 CO₂ Sensor Initialized...");
13}
14
15void loop() {
16 mySerial.write(cmd_get_sensor, 9); // Send command to request CO₂ concentration
17 delay(10); // Wait for response
18
19 // Read 9-byte response
20 if (mySerial.available()) {
21 int i = 0;
22 while (mySerial.available() && i < 9) {
23 response[i] = mySerial.read();
24 i++;
25 }
26
27 // Check if the response is valid
28 if (i == 9 && response[0] == 0xFF && response[1] == 0x86) {
29 int co2ppm = response[2] * 256 + response[3]; // Combine two bytes to get CO₂ ppm
30 Serial.print("CO₂ Concentration: ");
31 Serial.print(co2ppm);
32 Serial.println(" ppm");
33 } else {
34 Serial.println("Invalid response from sensor.");
35 }
36 }
37
38 delay(2000); // Wait for 2 seconds before next read
39}
40
Applications of MH-Z19 CO2 Sensors
- Indoor air quality monitoring
- Smart HVAC systems
- Greenhouse CO2 management
- Smart agriculture and crop optimization
- Environmental sensing projects
- Educational and IoT-based experiments
Conclusion
Interfacing the MH-Z19 CO2 Sensor with Arduino enables precise air quality monitoring for smart homes, agriculture, and environmental applications. With simple wiring and reliable output, it's a great addition to any sensor-based project.