Problem defining set point for humidity controller

The goal of my project is to make a humidity control which is based on the outdoor air temperature. As the outdoor air temperature drops, the humidity set point should change based on pre-determined recommendations. Then, the Arduino compares the actual humidity to the set point and opens a water valve to the humidifier if the actual humidity is below set point.

Just a heads up alert before we delve into this. The temperature triggers in the sketch are for bench testing only and will be replaced with the correct temperature triggers in the final project.

When I measure the outdoor temperature it seems to return the correct set point (see serial monitor). But when I apply the test to see whether the actual is less than the set point, it would appear that the set point value is always zero.

Between Lines 106 and 135, the Arduino always prints the correct set point value to the serial monitor.
Line 151 always sends a value of zero to the Serial monitor.
Line 160 always sends a value of zero to the LCD.

I’ve pondered this for several days now and can’t seem to put my finger on the problem.
Here is my code:

A DS18B20 Temperature sensor is connected to 5VDC, ground and (Data) to the Arduino’s Pin D2
and measures the outdoor temperature.  This value gets converted to a range of 25 to 50 and 
is used as a set point for the humidity. It is compared to the humidity read from the AM2302
(DHT22) and is used to trigger the solenoid to supply water to the Humidifier.

An AM2302 Humidity/Temperature sensor is connected to the Arduino’s Pin D4 and measures 
Temperature and Humidity. The program reads the value of this sensor every 5 seconds. 

Pin D7 of the Arduino is connected to a General Purpose Relay SPDT (5VDC) via a 2N2222
switching transistor to handle the 140 mA coil current. If pin D7 goes high, the relay
triggers to the on state and completes the circuit between the N.O. contacts of the relay.
If the relay contacts are energized (closed), they will complete the circuit and open the 
solenoid valve supplying water to the Humidifier on the furnace.

The program is written with a test to make sure that the DHT sensor is connected and working.
If the sensor cannot be read, the Water Solenoid LED will flash for 10 times, ½ second on,
1 second off to let you know there is a problem. In addition a “Fault: DHT22” message will 
display on the LCD.
If the Humidifier relay is energized, it will latch for two minutes, before sampling the return
air’s ambient humidity again. Once the return air humidity reaches the set point, the Humidistat’s 
relay will de-energize and the program will again start to sample the ambient humidity and test
it against the set point every 5 seconds. 
The LCD displays the Relative Humidity set point as well as the actual humidity.  
This indicates whether or not the DHT-22 is working properly.  The outdoor temperature is displayed
on the second line of the LCD to verify that the DS18B20 is working properly. The only LED is 
connected to Pin D13 on the Arduino and lights when the water solenoid relay is energized supplying
water to the humidifier.


#include <DHT.h> 
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>

// initialize the library with the numbers of the interface pins
// LCD pin       RS   E  D4  D5   D6   D7  
LiquidCrystal lcd(7,  8, 9,  10,  11,  12);

// DS18B20 Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
#define DHTPIN 4 // AM2302 is plugged into pin 4 
#define DHTTYPE DHT22 // DHT 22 (AM2302) 
#define solenoid 3 // Pin to Activate water solenoid 
#define flow 13 // Pin to show solenoid valve is open (blue)

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
int tempF = 0;
int setPoint;

void setup() { 
  pinMode(solenoid, OUTPUT); 
  Serial.begin(9600); //Begin serial communication
  Serial.println("Arduino Humidistat // LCD Set Point Version"); //Print a message 

void loop() { 
// Wait 5 seconds between measurements. 
// Reading temperature or humidity takes about 250 
// Sensor readings may also be up to 2 seconds 'old' 
// (its a very slow sensor) 

int h = dht.readHumidity(); 
// Read temperature as Celsius 
int t = dht.readTemperature(); 
int tt = dht.convertCtoF(t); 
// Check if any reads failed and exit early (to try again). 
if (isnan(h) || isnan(t)) { 
Serial.println("Failed to read from DHT sensor!"); 
  lcd.print("Fault: DHT22");
// If the sensor fails to read successfully execute for loop 
// and flash the solenoid LED for 10 times
// a total of 15 seconds 
for(int x=0; x < 10; x++){ 
// Read DS18B20 and convert to humidity values between 25 and 50% 
// setPoint = the set point for the target humidity level 
  // Send the command to get temperatures
  int tempOut; //create a variable to hold the temperature value
  tempOut = (sensors.getTempFByIndex(0));
  // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire

  if (tempOut <= 40) {
    int setPoint=25;
    Serial.print ("Humidity Set Point is: ");
    Serial.println (setPoint);
    else if (tempOut > 40 && tempOut <=50) {  
      int setPoint=30;
      Serial.print ("Humidity Set Point is: ");
      Serial.println (setPoint);
    else if (tempOut > 50 && tempOut <=60) {  
      int setPoint=35;
      Serial.print ("Humidity Set Point is: ");
      Serial.println (setPoint);
    else if (tempOut > 60 && tempOut <=70) {  
      int setPoint=40;
      Serial.print ("Humidity Set Point is: ");
      Serial.println (setPoint);
    else if (tempOut > 70 && tempOut <=80) {  
      int setPoint=45;
      Serial.print ("Humidity Set Point is: ");
      Serial.println (setPoint);
    else  {  
      int setPoint=50;
      Serial.print ("Humidity Set Point is: ");
      Serial.println (setPoint);
// Test to see if the humidity is less than the set point 
// if so turn on solenoid 
// If not turn off solenoid 

if(h < setPoint) { 
digitalWrite(solenoid, HIGH); 
digitalWrite(flow, HIGH); 
Serial.println ("Solenoid valve abre");
else { 
digitalWrite(solenoid, LOW); 
Serial.println ("Solenoid valve cerado");
Serial.println (setPoint); 
Serial.print("Humidity: "); 
Serial.print(" Temperature: "); 
Serial.println(" degF "); 
  lcd.print("SP:  ");
  lcd.print(" RH: ");
  lcd.print("OutTemp: ");
  lcd.print(" degF");

I have also attached pictures of the serial monitor and LCD. If necessary I can also post a schematic.

You have these in various places:

int setPoint;
    int setPoint=25;
      int setPoint=30;
      int setPoint=35;
      int setPoint=40;
      int setPoint=45;
      int setPoint=50;

Each one declares a different variable named setPoint. This is a bad thing to do, even for the pros.

You probably intended the first

int setPoint;

All of the rest should lose the “int” part that causes declaration. They should look like these:


Your Serial.println(setPoint) statements refer to variables that disappear. Then you do
if(h < setPoint) {
which refers to the first setPoint variable to which you never gave a value.
You need to study about “scope”.

Good Luck!

Thanks so much! The answer was right there under my nose all the time. :)