Sharp dust sensor with MHZ19 C02 sensors in lcd display

trying to complie two sensors in one source code for the lcd display , and the value for the Co2 is not accurate.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // Or whatever your's is
int measurePin = 0; //Connect dust sensor to Arduino A0 pin
int ledPower = 2; //Connect led driver pins of dust sensor to Arduino D2
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
#define pwmPin A1
float Co2= 0;
int prevVal = LOW;

long th, tl, h, l, ppm;

void setup(){
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
pinMode(pwmPin, INPUT);
Serial.print("****************** keyestudio ******************\n");
lcd.init(); // initialize the lcd

// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Raw Value:");

lcd.backlight ();
lcd.setCursor(0,1);
lcd.print("C02:");
pinMode(ledPower,OUTPUT);
}
void loop(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (5.0 / 1024.0);
dustDensity = 0.17 * calcVoltage - 0.1;

lcd.backlight();
lcd.setCursor(10,0);
lcd.print(voMeasured);
lcd.setCursor(13,2);
lcd.print(dustDensity);
delay(1000);
Co2 = analogRead(measurePin);
long tt = millis();

int myVal = digitalRead(pwmPin);

if (myVal == HIGH) {
if (myVal != prevVal) {
h = tt;
tl = h - l;
prevVal = myVal;
}
} else {
if (myVal != prevVal) {
l = tt;
th = l - h;
prevVal = myVal;
ppm = 5000 * (th - 2) / (th + tl - 4);
Serial.println("PPM = " + String(ppm));
}
}
lcd.backlight();
lcd.setCursor(5,1);
lcd.print(Co2);

delay(1000);
}//************************************************************

Please take a moment to read this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum and then go back and format your code and use code tags. It helps people help you.

Does your CO2 sensor give the proper values when used alone?

By default MH-Z19 recalibrate itself after 24 hours of work

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.