[SOLVED] Help me with my project

for having a dc power supply for my relay

Your fan control should work. But I would use 2 relays...
First relay for direct mains or mains via caps.
The second relay to chose which cap.
That would prevent that you have 2 or 3 option 'on' at the same time.

okay sir I really appreciate your help it's hard working on a project for me since i don't know what's wrong and there is really no one to help me. I'll work on your tips and try to give you an update have a good day! :smile:

Good luck!

You might add Serial.print("here I am");
Inside your if...
And Serial.print("now I am here!"); in your else...
And "end of setup" at end of setup.
That way you know where your program is going...

btw sir follow up question I tried running my relay powered by my arduino what does it mean to have the led in my arduino to turn on and off along with my relay?

Pin 13 is connected to the builtin led.
So your observation confirms that the pin is set high and low by your code...

oh okay I thought it was also affected by my problem it seems that its working fine thanks

Good day to you Sir! Thank you for being so kind and sharing your knowledge with me. I'm truly grateful for your help. The problem with the relay is that it indeed needed another dc source to power it, now it's not experiencing any issues anymore. I am really thankful for your help sir since this is my thesis project I can't thank you enough. Enjoy your day sir and God Bless!!!

Thanks for your feedback.
Could you mark your thread as solved?
That will save the time of other helpers here...

Good day! Can I ask a question sir, 1 relay works fine for my project but after adding 2 there seems to be some complications happening. the parameters of my project is that the 1st relay is between 2ppm and 15 ppm for my 2nd relay is between >15ppm and 30 ppm. My problem is that the first reading is between 0.5ppm then it climbs to 2ppm after going back to 0 ppm the arduino keeps disconnecting to the ide there seems to be a spike or something idk the correct term. is there a solution for this one?

Could you post your current code and current schematic?
And the specifications if the power supplies?
Without that it is impossible to answer.

image

#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 = 35;       // Sensor Resistance in fresh air from previous code
int relayPin1 = 13;  // First relay control pin
int relayPin2 = 12;  // Second relay control pin
int hysteresis = 2;  // Hysteresis value
int relay1State = HIGH;  // Initial state (OFF)
int relay2State = HIGH;  // Initial state (OFF)

SoftwareSerial mySerial(2, 3);  // RX, TX pins on Arduino Uno

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  digitalWrite(relayPin1, relay1State);
  digitalWrite(relayPin2, relay2State);
  pinMode(CO2Sensor, INPUT);
  pinMode(AmmoniaSensor, INPUT);
}

void loop() {
  float sensor_volt;
  float RS_gas;
  float ratio;

  int sensorValueAmmonia = analogRead(AmmoniaSensor);
  int sensorValueCO2 = analogRead(CO2Sensor);

  sensor_volt = sensorValueAmmonia * (5.0 / 1023.0);
  RS_gas = ((5.0 * 10.0) / sensor_volt) - 10;
  ratio = RS_gas / R0;

  double ppm_log = (log10(ratio) - b) / m;
  double ammonia_ppm = pow(10, ppm_log);

  gas = sensorValueCO2;
  co2lvl = gas - 64;
  co2lvl = map(co2lvl, 0, 1024, 400, 5000);

  Serial.print("Ammonia: ");
  Serial.println(ammonia_ppm, 2);
  delay(500);

  Serial.print("CO2: ");
  Serial.println(co2lvl);
  delay(500);

  mySerial.print(ammonia_ppm, 2);
  mySerial.print(",");
  mySerial.println(co2lvl);

  // Control the relays with hysteresis
  if (ammonia_ppm >= hysteresis && ammonia_ppm <= 15) {
    if (relay1State != LOW) {
      digitalWrite(relayPin1, LOW);  // Turn on the first relay
      relay1State = LOW;
      relay2State = HIGH;
      delay(500);  // Delay for stability
    }
  } else {
    if (relay1State != HIGH) {
      digitalWrite(relayPin1, HIGH);  // Turn off the first relay
      relay1State = HIGH;
      relay2State = HIGH;
      delay(500);  // Delay for stability
    }
  }

  if (ammonia_ppm >= (15 + hysteresis)) {
    if (relay2State != LOW) {
      digitalWrite(relayPin2, LOW);  // Turn on the second relay
      relay1State = HIGH;
      relay2State = LOW;
      delay(500);  // Delay for stability
    }
  } else {
    if (relay2State != HIGH) {
      digitalWrite(relayPin2, HIGH);  // Turn off the second relay
      relay1State = HIGH;
      relay2State = HIGH;
      delay(500);  // Delay for stability
    }
  }
}

for the power source
input = 100 - 240v 50-60hz 0.5A
output = 5v 2A
the dc 5v charger you recommended

Hi, @newbieeee23241

Glad to hear you have got it working.
Sorry, but sad to realise that at that level of education.

A power supply problem was not recognised with simple troubleshooting methods.

Tom... :grinning: :+1: :coffee: :australia:

What current do the sensors take?
Are they fed from 5V of arduino?
Your schematic is not complete...

yes sir they are from the arduino
the sensors are not included in that schematic sorry

Mqt137 should not be fed from arduino 5V pin...

Hi,

An updated and accurate schematic would be good about now so we all know what you are talking about.

Please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Tom... :grinning: :+1: :coffee: :australia:

1
idk if this is clear for you sir but if you have questions please ask right away sir
i forgot to add the capacitors in each relay, each relay has 1 capacitor wired to the NO.
2

To anyone who encounters the same problem as me. Use Solid state relay when dealing with ac instead of mechanical relay mine now works smoothly. Thank you everyone that helped my project have a nice day.