Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The MH-Z19 NDIR CO2 Sensor is a non-dispersive infrared sensor that measures the concentration of carbon dioxide (CO2) in the air. Here's an example code in Arduino that reads the CO2 concentration from the sensor:
- Note: This code assumes that the MH-Z19 NDIR CO2 Sensor is connected to pins 2 and 3 of the Arduino board for the software serial connection. If you're using different pins or a hardware serial connection, you'll need to modify the SoftwareSerial initialization and co2Serial object accordingly. Also, note that the sensor requires a warm-up time of at least 3 minutes after power-on before providing accurate readings, so be sure to wait for this period before reading from the sensor. Finally, note that the sensor is designed to detect carbon dioxide specifically and may not detect other gases or vapors.
- */
- #include <SoftwareSerial.h>
- SoftwareSerial co2Serial(2, 3); // RX, TX pins of the software serial connection to the sensor
- void setup() {
- Serial.begin(9600);
- co2Serial.begin(9600); // start the software serial connection to the sensor
- }
- void loop() {
- co2Serial.write("\xff\x01\x86\x00\x00\x00\x00\x00\x79"); // request a CO2 concentration reading from the sensor
- delay(10);
- while (co2Serial.available() > 0) {
- if (co2Serial.read() == '\xff') {
- if (co2Serial.read() == '\x86') {
- int co2_concentration = co2Serial.read() * 256;
- co2_concentration += co2Serial.read();
- Serial.print("CO2 Concentration: ");
- Serial.print(co2_concentration);
- Serial.println(" ppm");
- }
- }
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement