Co2-Messung mit dem MH-Z19 und Anzeige auf dem 7735 TFT-Display 1,8 Zoll

7735 lcd displa mit mh-z21

Hier nun mal ein Sketch um die Werte live auf einem preiswerten TFT-Display anzuzeigen.

/*
  Arduino TFT text example

   http://www.arduino.cc/en/Tutorial/TFTDisplayText

 */

#include <TFT.h>  // Arduino LCD library
#include <SPI.h>

// pin definition for the Uno
#define cs   10
#define dc   9
#define rst  8

// pin definition for the Leonardo
// #define cs   7
// #define dc   0
// #define rst  1

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[6];

void setup() {

  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);

  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Sensor Value :\n ", 0, 0);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(5);

  #define AIQ_SENSOR_ANALOG_PIN 5
}
void loop() {
  //CO2
  
  unsigned long duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH, 2000000);
  
  //Serial.print(duration/1000); Serial.println(" ms ");
  //from datasheet
  //CO2 ppm = 2000 * (Th - 2ms) / (Th + Tl - 4ms)
  //  given Tl + Th = 1004
  //        Tl = 1004 - Th
  //        = 2000 * (Th - 2ms) / (Th + 1004 - Th -4ms)
  //        = 2000 * (Th - 2ms) / 1000 = 2 * (Th - 2ms)
 
  long co2ppm = 5 * ((duration/1000) - 2);
  Serial.print(co2ppm);

 
String sensorVal = String(co2ppm);
 sensorVal.toCharArray(sensorPrintout, 6);
  TFTscreen.stroke(255,255,255);
  TFTscreen.text(sensorPrintout, 20, 50);

  delay(1000);
  TFTscreen.stroke(0,0,0);
  TFTscreen.text(sensorPrintout, 20, 50);
}

Belegung ST7735
Belegung ST7735

4SensorenSketch V0.13 mit dem MH-Z19 für die CO2-Messung

Hier nun die Fortsetzung meines alten 4 Sensoren Sketches. Nun ist der MH-Z19 für die CO2 Messung integriert und ich habe außerdem noch eine Steuerung für eine LED-Warnung eingebaut. Der Sketch funktioniert genau so mit dem MH-Z21.

//Sens4Sketch (Motion, Temperature, Humidity, CO2)

//v0.13 LED blink co2
//v0.12 MH-Z19 integration
//v0.11 Motion corrected
//v0.10 ---- installation co2 value specification outside----under construction
//v0.9 Autoset r0 off, vaule < 400 possible!!!!
//v0.8 Corr. Lib von rstoica (set r0, correction) impl
//v0.7 set r0 to eeprom
//v0.6 SetR0-Button
//v0.5 Motion
//v0.4 autosetr0 if co2<400
//v0.3 api 2.0, redesign
//v0.2 CorrectedRZero basis for calculating r0
//G. Krocker K.-H. Wind et. al. ++++JH
//Caution: The Sketch erorder an amended mq135.h Library

#define MY_DEBUG    // Enables debug messages in the serial log
#define MY_RADIO_NRF24
//#define MY_REPEATER_FEATURE  // Enables repeater functionality for a radio node
#include <SPI.h>
#include <MySensors.h>
#include "DHT.h"
//----------------------------------------------------------------------------
// Timer
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
//-----------------------------------------------------------------------------
//MOTION
#define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-3 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID_MOTION 16   // Id of the sensor child
MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
//-----------------------------------------------------------------------------
// DHT22
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define DHTPIN 4     // what digital pin we're connected to
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
float lastTemp;
float lastHum;
boolean metric = true; 
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
//-------------------------------------------------------------------------
#define CHILD_ID_AIQ 0
#define AIQ_SENSOR_ANALOG_PIN 5
float valAIQ =0.0;
float lastAIQ =0.0;
MyMessage msg(CHILD_ID_AIQ, V_LEVEL);
MyMessage msg2(CHILD_ID_AIQ, V_UNIT_PREFIX);
//-----------------------------------------------------------------------------

void setup() {
//--------------------------------------------------------------------- 
 dht.begin();
pinMode(DIGITAL_INPUT_SENSOR, INPUT);
pinMode(7, OUTPUT);
}
//-----------------------------------------------------------------------------
void presentation(){
sendSketchInfo("Sens4", "v0.13");
present(CHILD_ID_TEMP, S_TEMP);
present(CHILD_ID_HUM, S_HUM);
present(CHILD_ID_MOTION, S_MOTION);
present(CHILD_ID_AIQ, S_AIR_QUALITY);  
//--------------------------------------------------------------------
}

void loop(){



//-----------------------------------------------------------------------------
//TEMP_FEUCHTE 
  float temperature = dht.readTemperature();
   send(msgTemp.set(temperature, 1));
    Serial.print("T: ");
    Serial.println(temperature);
 
  float humidity = dht.readHumidity();

      send(msgHum.set(humidity, 1));
      Serial.print("H: ");
      Serial.println(humidity);
//-----------------------------------------------------------------------------  
//CO2
  
  unsigned long duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH, 2000000);
  
  //Serial.print(duration/1000); Serial.println(" ms ");
  //from datasheet
  //CO2 ppm = 2000 * (Th - 2ms) / (Th + Tl - 4ms)
  //  given Tl + Th = 1004
  //        Tl = 1004 - Th
  //        = 2000 * (Th - 2ms) / (Th + 1004 - Th -4ms)
  //        = 2000 * (Th - 2ms) / 1000 = 2 * (Th - 2ms)
 
  long co2ppm = 5 * ((duration/1000) - 2);
  Serial.print(co2ppm);

    send(msg.set(co2ppm,1));
 
if (co2ppm>1000){
    digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
}
if (co2ppm<550) {
    digitalWrite(7, LOW);
}

//-----------------------------------------------------------------------------
//Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
 Serial.print("Motion: ");
Serial.println(tripped);
send(msgMotion.set(tripped?"1":"0"));  
//-----------------------------------------------------------------------------
wait(SLEEP_TIME);
}
//---------------------------------------------------------------------------