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