0

I am building a special chamber, where I'm dealing with humidity, temperature and co2 contamination, but I'm having difficulties with software. If I separate it by parts, everything works as it should, but when I combine everything together, the CO2 measuring stops and the CO2 is being added to the chamber continuosly.

Here are my requests: -The temperature has to be between 36 and 37°C, -It has to be known the exact humidity, -The amount of CO2 (carbon dioxide) has to be around 5% (50.000 ppm), around +/- 0.2% is allowed, -Everything should be shown on 1602 LCD display.

To measure temperature, I have a LM335, for measuring humidity a HIH-4004-001 humidity sensor and for measuring CO2 contamination I have a MH-Z16 sensor. If there is lack of CO2 in the chamber, I'm adding it with a electromagnetic gas valve, which is driven by a relay. To heat up the chamber, I'm also using a relay, to turn on the heater.

Here's the code. Does anyone has any kind of clue, what could possibly go wrong here?

#include <SoftwareSerial.h>           //lib for serial communication for co2
#include <NDIR_SoftwareSerial.h>      //lib for co2

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  //(RS, EN, D4, D5, D6, D7)

//LM335 sensor TEMP
int outputPin = 3;

//heater
const int rele = 2;  //relay output for HEATER
int flag = 0;       //flag for hysteresis at 37°C

//humidity
int HIH4000_Pin = A2; //analog pin 2, humidity sensor

//co2
NDIR_SoftwareSerial mySensor(11, 12); //green= 11; yellow= 12.
float percentage = 0.00; //co2 in percentage

//co2 gas valve
const int ventil = 3;  //relay output for CO2 GAS VALVE

void setup() {    
  lcd.begin(16, 2);
  pinMode(rele, OUTPUT);      //relay for HEATER
  pinMode(ventil, OUTPUT);    //relay for CO2 GAS VALVE
  Serial.begin(9600);

  digitalWrite(rele, HIGH);   //heater off
  digitalWrite(ventil, HIGH);   //co2 gas valve off

  if (mySensor.begin()) {
    Serial.println("Wait 10 seconds for sensor initialization...");

    lcd.setCursor(0, 0);
    lcd.print("Wait_10_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_9_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_8_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_7_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_6_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_5_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_4_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_3_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_2_seconds");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
    lcd.print("Wait_1_second");
    lcd.setCursor(2, 1);
    lcd.print("CALIBRATION");
    delay(1000);
    lcd.clear();
  }
  else {
    Serial.println("ERROR: Failed to connect to the sensor.");
    lcd.setCursor(0, 0);
    lcd.print("Press RST button");
    while (1);
  }

  lcd.setCursor(0, 0);
  lcd.print("T= ");
  lcd.setCursor(7, 0);
  lcd.print((char)223);
  lcd.setCursor(8, 0);
  lcd.print("C");

  lcd.setCursor(10, 0);
  lcd.print("H=");
  lcd.setCursor(15, 0);
  lcd.print("%");

  lcd.setCursor(0, 1);
  lcd.print("CO2=");
  //lcd.setCursor(8,1);
  //lcd.print("%");
}

void loop() {    
  delay(100);

  //beggining of measuring and displaying CO2
  if (mySensor.measure()) {
    Serial.print("CO2 Concentration is ");
    Serial.print(mySensor.ppm);
    Serial.println("ppm");
    lcd.setCursor(4, 1);
    lcd.print("        ");
    lcd.setCursor(4, 1);
    lcd.print(mySensor.ppm);
    percentage = mySensor.ppm;
  } else {
    Serial.println("Sensor communication error.");
  }
  //end of measuring and displaying CO2

  //beggining of CO2 gas valve
  if (percentage < 1000) {
    digitalWrite(ventil, LOW);
    delay(1000);
    digitalWrite(ventil, HIGH);
    /*lcd.setCursor(2,0);
      lcd.print(celsius);  */
  }
  else {
    digitalWrite(ventil, HIGH);
  }
  //end of CO2 gas valve

  //start measuring temperature
  int rawvoltage = analogRead(outputPin);
  float millivolts = (rawvoltage / 1024.0) * 5000;
  float celsius = (millivolts / 10) - 276.20;
  lcd.setCursor(2, 0);
  lcd.print(celsius);
  //stop measuring temperature

  //start humidity sensor
  float relativeHumidity  = analogRead(HIH4000_Pin);
  float av = 0.0048875 * relativeHumidity;
  float vlaga = (av - 0.86) / 0.03;
  int humi = vlaga;
  if (humi < 100) {
    lcd.setCursor(12, 0);
    lcd.print(" ");
    lcd.setCursor(13, 0);
    lcd.print(humi);
  }
  else {
    lcd.setCursor(12, 0);
    lcd.print(humi);
  }
  //end humidity sensor

  //start of relay for heater
  lcd.setCursor(12, 1);
  if (celsius < 37 && flag == 0) {    //upper treshold of hysteresis 37
    digitalWrite(rele, LOW);
    //lcd.print("WARM");
  }

  if (celsius < 36 && flag == 1) {    //lower treshold of hysteresis 36
    digitalWrite(rele, LOW);
    //lcd.print("WARM");
  }

  if (celsius > 37) {               // treshold of first heating up
    digitalWrite(rele, HIGH);
    //lcd.print("COOL");
    flag = 1;
  }
  //end of relay for heater

  delay(650);
}
  • Remember to cross the wires for the serial, so RX goes to TX and TX goes to RX. – Magic-Mouse Oct 4 '17 at 10:53
  • That's already okay. – Jakey Oct 4 '17 at 11:12
  • I noticed, that the valve will correct the value of the co2, but only 1 or 2 times, then it will crash; But if I look through serial, I can see, that I loose connection between sensor and Arduino: I start getting this message "Sensor communication error." – Jakey Oct 5 '17 at 13:59
0

If the CO2 sensor stops working that means you should be getting a message out of the Serial console, you didn't mention it so I presume you aren't which means it is still reading a value.

If you are still reading a value and the CO2 is still being added this means the 'percentage' CO2 has to be less than 1000.

So, either the CO2 sensor is faulty (which because it works on its own it looks like it isn't) or there is a "wiring fault" and you aren't actually reading the CO2 sensor when you think you are.

Is it possible that you are misreading the result from the serial connection? Is it possible that the library is misreading the data?

Try a simple program that reads the value from the CO2 sensor, prints it to the serial console and then opens the value for a second.

(Oh and in the setup function have a look at using a loop :) )

  • Thank You, it helped a bit and I came to a new conclusion: As long as I have my arduino connected to my PC with a cable, it works perfectly. When I unplug the USB cable, it starts to mess around by not measuring anymore. Is there any way for me to totally remove serial communication and work totally independent without the PC? – Jakey Oct 4 '17 at 10:30
  • That behaviour sounds like a bad earth connection. But to answer your question a phone charger will normally do the job, just chop the USB connector off and plug the wires into the Arduino's GND and VIN pins. Make sure you get the wires the right way round though with a multi metre or an LED – Code Gorilla Oct 4 '17 at 12:16
  • I'm using a 230V -> 5VDC ac-dc converter, which is grounded. Everything is inside a plastic case. – Jakey Oct 4 '17 at 12:26
0

Okay, I figured it out, what was wrong. It's completely unlogical and strange, but it solved me a problem. First off, I tried to disconnect the valve relay from arduino, while arduino was still giving logical 0 and 1 on the output. And the strange thing was, it worked. So I took a step further and back connected the relay, but disconnected the burden (valve). It worked fine aswell. So I figured it out, that here's some sort of a problem, that the valve SHOULDN'T be opened with relay, so I just added a 2N2222 transistor and a 12k resistor to the base, and the whole thing is working now perfectly. I just connected the output of the arduino (which was on relay for valve before) and put it on base resistor of 2N2222.

But thanks everyone for help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.