Hello I am working on a project in which I have an ac fan and a mq137 sensor then the 3 relays wherein there are capacitors connected which will change the speed of the fan depending on the value of the sensor. My issue is that at first I used one relay to check if the hardware is working if it will turn on/off everytime the sensor detects something, it worked fine at first but then I connected my wiring to the NO of the relay then after a few minutes even though there is no value detected on my sensor the fan is still spinning making the NO to become NC. Now another issue arose after letting my relay rest what happens is that everytime it detects something the arduino keeps resetting and in the ide it keeps disconnecting and connecting even my pc monitor suddenly turned on and off. attached is my circuit diagram and code I hope that you can help me since I don't have anyone to guide me on this one.
#include <SoftwareSerial.h>
int AmmoniaSensor = A0;
int CO2Sensor = A1;
int gas, co2lvl;
float m = -0.263; // Slope
float b = 0.42; // Y-Intercept
float R0 = 20; // Sensor Resistance in fresh air from previous code
int relayPin1 = 13; // First relay control pin
int relayPin2 = 12; // Second relay control pin
SoftwareSerial mySerial(2, 3); // RX, TX pins on Arduino Uno
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
delay(500);
digitalWrite(relayPin1, HIGH); // Initial state (OFF)
digitalWrite(relayPin2, HIGH); // Initial state (OFF)
pinMode(CO2Sensor, INPUT);
pinMode(AmmoniaSensor, INPUT);
}
void loop() {
float sensor_volt; // Define variable for sensor voltage
float RS_gas; // Define variable for sensor resistance
float ratio; // Define variable for ratio
float sensorValueAmmonia = analogRead(AmmoniaSensor); // Read ammonia sensor
float sensorValueCO2 = analogRead(CO2Sensor); // Read CO2 sensor
sensor_volt = sensorValueAmmonia * (5.0 / 1023.0); // Convert analog values to voltage
RS_gas = ((5.0 * 10.0) / sensor_volt) - 10; // Get value of RS in a gas
ratio = RS_gas / R0; // Get ratio RS_gas/RS_air
double ppm_log = (log10(ratio) - b) / m; // Get ammonia ppm value in linear scale according to the ratio value
double ammonia_ppm = pow(10, ppm_log); // Convert ppm value to log scale
gas = sensorValueCO2;
co2lvl = gas - 64;
co2lvl = map(co2lvl, 0, 1024, 400, 5000);
// Send ammonia value to the serial monitor
Serial.print("Ammonia: ");
Serial.println(ammonia_ppm, 2); // Print the ammonia ppm value with 2 decimal places
delay(500); // Delay for 0.5 seconds
// Send CO2 value to the serial monitor
Serial.print("CO2: ");
Serial.println(co2lvl);
delay(500);
// Send ammonia value to NodeMCU
mySerial.print(ammonia_ppm, 2); // Print the ammonia ppm value with 2 decimal places
mySerial.print(","); // Separate values with a comma
delay(500); // Delay for 0.5 seconds
// Send CO2 value to NodeMCU
mySerial.println(co2lvl);
// Control the relay
if (ammonia_ppm >= 5 && ammonia_ppm < 15) {
digitalWrite(relayPin1, LOW); // Turn on the first relay
} else {
digitalWrite(relayPin1, HIGH); // Turn off the first relay
}
delay(1000); // Delay for a second before the next reading
}
CIRCUIT DIAGRAM:
I appreciate your time for reading this one! Have a good day!


