Programming help Humidifier project

I am attempting to write a program using the humidity/temp sensor. The objective is to have the program turn on(close relay) if less than 40% humidity on initial start up. If there is a sensor error at any time, leave relay open. Then I want to try and make a loop with the sensor reading to stay on if under 50% and turn off if over 50%. Then circle back to the under 40% again so the relay isn't constantly going on and off. I can't find anything similar to this anywhere to model off of. Can someone please help me. Here is what I have so far.


/*

int sensorPin = 2;
int relayPin = 9;

void setup()
{
Serial.begin(9600);
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop()
{
int sensorValue = analogRead(sensorPin);

if( sensorValue <40 ) // Change the value as per your requirement
{
digitalWrite(relayPin, HIGH);
Serial.print("relay on");
Serial.println(sensorValue);
delay(100);
}
loop
if ( sensorValue <50 )
{
digitalWrite(relayPin, HIGH);
Serial.print("relay on");
Serial.println(sensorValue);
delay(100);
}
if( sensorValue >50 )
{ digitalWrite(relayPin, LOW);
Serial.print("relay off");
Serial.println(sensorValue);
else
{
digitalWrite(relayPin, LOW);
Serial.print("relay off");
Serial.println(sensorValue);
}

We can help but need you to make an initial effort on the code and a schematic.

Take a google at hysteresis.

You give a vague description but it seems to only amount to

a. turn on the whatever when it goes below 40 so it goes up and then

b. turn it off when it goes over 50 so it goes down

so it stays between 40 and 50.

Over and over.

Also I don't see any means for testing for sensor error in your sketch. How would this present itself?

The "if" and "else" clauses of your last if statement are identical, this may not be what you meant.

a7

What I mean is if the sensor isn't reading then I need it to default to off.

Your "schematic" has at least one errors I hope your real wiring does not duplicate.

How do you detect a sensor that "isn't reading"? At this point the only interface to the sensor is to analogRead() the value - what reading would allow you to conclude that the sensor was off?

a7

Hi,
What humidity/temperature sensor are you using.
Can you post a link to specs/data please?

If it is a DHT11 or similar, you cannot read humidity directly off the output pin with analogRead.

Your connections to the relay are not correct.
You should have the Vcc pin of the relay module connected to 5V.
You should have the IN pin of the output pin9 of the UNO.

Thanks.. Tom... :smiley: :+1: :australia:

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