Hello all!
I have been working on this Environmental control system for some time now and thanks to this community is ALMOST completely operational.
The program is quite simple. Take reading from two different sensors, and if they are below or above certain level, switch on a relay.
The problem I am coming across now is that I will occasionally come to the Arduino not functioning as it should... as if the sketch ceases to loop and gets stuck performing its last function with both relays off, one on, or both on. When I reset the Arduino through uploading the same sketch, the Arduino takes a reading and "realizes" it is out of the allowed humidity/co2 level and functions as it should... for a while.
04/03/18 UPDATE:
I am utilizing a new humidity sensor (BME280) which allows me to use the SDA and SCK pins on the Arduino along with the CO2 sensor. I am no longer using duel I2C wires to communicate with the sensors individually, however the problem of the the sketch hanging still persists.
its almost as if the Arduino gets takes one last reading from the sensor(s), acts accordingly... but fails to loop and take another reading. Hence getting stuck performing its last action with the relay(s)
I have revised the code a bit (and used auto-format), by shortening the delay() intervals and changing the IF statements to include <= or >=. Example below.
// CO2 sensor ON-OFF statement
if (mySensor.ppm >= ppmLevel) {
digitalWrite(co2Pin, LOW);
digitalWrite(8, LOW);
}
else {
digitalWrite(co2Pin, HIGH);
digitalWrite(8, HIGH);
}
delay(1000);
delay(1000);
// Humidity ON-OFF statment
int humidity = mySensorB.readFloatHumidity() ;
if (humidity <= humLevel)
{
(digitalWrite(humidityPin, LOW));
(digitalWrite(7, LOW));
}
else {
(digitalWrite(humidityPin, HIGH));
(digitalWrite(7, HIGH));
}
I felt like using a longer interval (50s) would prevent the sketch hanging from occurring so quickly... considering the sketch has hung four times just this morning since the change, I am lead to believe shortening the delay time makes the problem worse.
I have noticed the sketch has a higher tenancy to fail when the 12v-5v buck converter is at 5.1v instead of 5.4v. Perhaps it is a power related problem?
sidenote: co2 connected to 5v, humidity sensor connected to 3.3v. Power source is 12v 1200ma. Total current draw is no more than 600ma
I am currently working on adding the watchdog timer, as I feel this will help me bypass this problem by resetting the Arduino at regular intervals.
Thank you for your input so far.
Attached is a schematic of the sketch and below you will find the revised code.
Link to co2 sensor:
https://sandboxelectronics.com/?product=mh-z16-ndir-co2-sensor-with-i2cuart-5v3-3v-interface-for-arduinoraspeberry-pi
Link to humidity sensor:
Revised code is as follows:
#include <Wire.h>
//in use 02/
#include <NDIR_I2C.h>
//in BME280 use
#include "SparkFunBME280.h"
BME280 mySensorB; //Uses I2C address 0x76 (jumper closed)
NDIR_I2C mySensor(0x4D); //Adaptor's I2C address (7-bit, default: 0x4D)
// Environemntal variables
int humLevel = 84.0; // highest observed valuse 84 = 100%
int ppmLevel = 700;
// Relay PIN
int co2Pin = 2;
int humidityPin = 3;
void setup()
{
Serial.begin(9600); // Open serial connection to report values to host
Serial.println("Starting up");
// set the digital pin as output:
pinMode(co2Pin, OUTPUT);
pinMode(humidityPin, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
delay(1000);
//CO2 Begin
if (mySensor.begin()) {
Serial.println("Wait 10 seconds for sensor initialization...");
delay(10000);
} else {
Serial.println("ERROR: Failed to connect to the sensor.");
while (1);
}
//BME280 USE
mySensorB.setI2CAddress(0x76); //Connect to a second sensor
if (mySensorB.beginI2C() == false) Serial.println("Sensor B connect failed");
Serial.println("Starting up");
// set the digital pin as output:
pinMode(co2Pin, OUTPUT);
pinMode(humidityPin, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
delay(1000);
}
void loop()
{
//CO2 Begin
if (mySensor.measure()) {
Serial.print("CO2 Concentration is ");
Serial.print(mySensor.ppm);
Serial.println("ppm");
} else {
Serial.println("Sensor communication error.");
}
delay(5000);
//BME280 Begin
Serial.print(" Humidity: ");
Serial.print(mySensorB.readFloatHumidity());
Serial.print(" Pressure: ");
Serial.print(mySensorB.readFloatPressure(), 0);
Serial.print(" Temp: ");
//Serial.print(mySensorB.readTempC(), 2);
Serial.print(mySensorB.readTempF(), 2);
Serial.println();
delay(5000);
// CO2 sensor ON-OFF statement
if (mySensor.ppm >= ppmLevel) {
digitalWrite(co2Pin, LOW);
digitalWrite(8, LOW);
}
else {
digitalWrite(co2Pin, HIGH);
digitalWrite(8, HIGH);
}
delay(1000);
delay(1000);
// Humidity ON-OFF statment
int humidity = mySensorB.readFloatHumidity() ;
if (humidity <= humLevel)
{
(digitalWrite(humidityPin, LOW));
(digitalWrite(7, LOW));
}
else {
(digitalWrite(humidityPin, HIGH));
(digitalWrite(7, HIGH));
}
delay(5000);
}


