Mh Z19
Обзор инфракрасного датчика CO2 MH-Z19
#include <SoftwareSerial.h>; #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DHT.h> byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; unsigned char response[9]; #define PWM_INPUT_PIN 5 #define DHTPIN 8 #define DHTTYPE DHT22 SoftwareSerial mySerial(A0, A1); // A0 - к TX сенсора, A1 - к RX LiquidCrystal_I2C lcd(0x27, 16, 2); // Устанавливаем дисплей DHT dht(DHTPIN, DHTTYPE); void setup() { lcd.init(); lcd.backlight();// Включаем подсветку дисплея Serial.begin(9600); mySerial.begin(9600); lcd.println("PPM="); } void loop() { readPPM_dig(); readPPN_ang(); // readTemp(); delay(15000); } void readTemp() { float h = dht.readHumidity(); float t = dht.readTemperature(); lcd.setCursor(0, 1); if (isnan(h) || isnan(t)) { lcd.println("Failed read DHT!"); return; } lcd.print(""); lcd.print(h); lcd.print("% "); lcd.print(""); lcd.print(t); lcd.print("*C "); } void readPPM_dig() { mySerial.write(cmd, 9); memset(response, 0, 9); mySerial.readBytes(response, 9); int i; byte crc = 0; for (i = 1; i < 8; i++) crc += response[i]; crc = 255 - crc; crc++; if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) { String errText = "CRC error:" + String(crc) + "/" + String(response[8]); Serial.println(errText); lcd.setCursor(0, 0); lcd.print(errText); } else { unsigned int responseHigh = (unsigned int) response[2]; unsigned int responseLow = (unsigned int) response[3]; unsigned int ppm = (256 * responseHigh) + responseLow; Serial.print("Digital: "); Serial.println(ppm); lcd.setCursor(4, 0); lcd.print(ppm); lcd.print("d "); } } void readPPN_ang() { unsigned long lowCounter = 0; unsigned long highCounter = 0; // wait for low while (high()) { } // wait for high while (low()) { } // count high iterations while (high()) { highCounter++; } // count low iterations while (low()) { lowCounter++; } int ppm = 5000.0 * (993.0 * highCounter - 2.0 * lowCounter) / 1000.0 / (highCounter + lowCounter); Serial.print("Analog: "); Serial.println(ppm); lcd.setCursor(11, 0); lcd.print(ppm); lcd.print("a "); } boolean low() { return LOW == digitalRead(PWM_INPUT_PIN); } boolean high() { return HIGH == digitalRead(PWM_INPUT_PIN); }