Introduction of MH-Z16 Carbon Dioxide Sensor
Using the principle of non-dispersive infrared (NDIR technology) to detect CO2 in the air, it has good selectivity and no oxygen dependence. And built-in temperature compensation, using the serial port, you can read the current CO2 gas concentration, the use is very simple, compatible with various microcontrollers. The infrared sensor is a high-performance sensor made of mature infrared absorption gas detection technology. It has the characteristics of anti-water vapor interference, no poisoning, long life, etc. It can be used in the air for a long time, avoiding long-term heating after poisoning. It can be widely used in intelligent agriculture, HVAC and indoor air quality monitoring, industrial process and safety protection monitoring, agricultural and animal husbandry production process monitoring, etc.
Sensor Pin Definition
Serial port read data command format
Arduino UNO wiring with sensor
Arduino UNO | MH-Z16 |
---|---|
D11 | RX |
D10 | TX |
5V | Wine |
GND | GND |
test program
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
unsigned char hexdata[9] = {
0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; //Read the gas density command /Don't change the order
void setup() {
Serial.begin(9600);
while (!Serial) {
}
mySerial.begin(9600);
}
void loop() {
mySerial.write(hexdata, 9);
delay(500);
for (int i = 0, j = 0; i < 9; i++)
{
if (mySerial.available() > 0)
{
long hi, lo, CO2;
int ch = mySerial.read();
if (i == 2) {
hi = ch; //High concentration
}
if (i == 3) {
lo = ch; //Low concentration
}
if (i == 8) {
CO2 = hi * 256 + lo; //CO2 concentration
Serial.print("CO2 concentration: ");
Serial.print(CO2);
Serial.println("ppm");
}
}
}
}
Open the serial port assistant and breathe in the sensor, the CO2 value that can be tested by the sensor will change.
Summary table
In this experiment, the value of the sensor is read by the serial port command, and the value measured by the sensor can also be calculated by the pulse width of the PWM.