Help with Programming

Hi All

I am on my way to building a Humidifier, i have so far built thus - Arduino UNO R3 + DHT22 + LCD 12C, downloaded all libraries and created a sketch, it all works fine display of temp & humidity is good.

I Now wish to incorporate A relay to go HIGH when the system detects a pre set low Humidity value. I know how to connect a relay to arduino, but i am not sure what to add to an my existing Temp & Humidity sketch to make it happen.

Can someone guide me please.

Regards

Ray

my existing Temp & Humidity sketch

Show it to us.

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

but i am not sure what to add to an my existing Temp & Humidity sketch to make it happen.

An if statement.

if (humidity < 50) digitalWrite(relayPin, HIGH);
if (humidity > 70) digitalWrite(relayPin, LOW);

Note the gap between the on and off: that’s a hysteresis, and needed to prevent switching on and off your humidifier too often. Now it’ll switch on the moment the humidity drops below 50, and switches off again when it’s over 70.

For that matter you may want to add a minimum time after it’s switched off before it can be switched on again (use millis() based timing for that - see Blink Without Delay on how that works).

Here is the sketch i am using -

*/

//Libraries
#include <DHT.h>
#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

//Constants

#define DHTPIN 4     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//ALWAYS USE THIS WITH LCD I2C and Addres 0x27
#define I2C_ADDR  0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{

  Serial.begin(9600);
  dht.begin();
  lcd.begin(16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
}

void loop()
{
  
  delay(1000);
  //Read data and store it to variables hum and temp
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print(" ");
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Hum:  ");
  lcd.print(hum);
  lcd.print(" %");

  delay(1000); //Delay 1 sec.

}

Looks good to me.

Basically all you have to do now is add the lines I suggested to your loop; amend the variable names; set the correct pin number for the relay; and set on/off values for the humidity that make sense to your situation.

I have entered the recommendations as stated (i think) and i get an error message "humidity" not declared in this scope, what have i done wrong?

[code
//Libraries
#include <DHT.h>
#include <Wire.h> 
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

//Constants

#define DHTPIN 4     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
#define relay 11


//ALWAYS USE THIS WITH LCD I2C and Addres 0x27
#define I2C_ADDR  0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{

  Serial.begin(9600);
  dht.begin();
  lcd.begin(16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
}

void loop()
{

  
if (humidity < 40) digitalWrite(relayPin, HIGH);
if (humidity > 55) digitalWrite(relayPin, LOW);
  
  delay(1000);
  //Read data and store it to variables hum and temp
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temp);
  lcd.print(" ");
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Hum:  ");
  lcd.print(hum);
  lcd.print(" %");

  delay(1000); //Delay 1 sec.

}]

Hi,
Change;

if (humidity < 40) digitalWrite(relayPin, HIGH);
if (humidity > 55) digitalWrite(relayPin, LOW);

to;

if (hum < 40) digitalWrite(relayPin, HIGH);
if (hum > 55) digitalWrite(relayPin, LOW);

You also need to add

#define relayPin  X

Where X is the pin number you have connected your relay board.
Also you are using pin0 and pin1, they should be left alone as they are used for programming your controller.
Tom… :slight_smile:

Hi,
Why have you got this in your code?

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

The example from that library is;

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,16,2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");
}

void loop()
{
}
LiquidCrystal_I2C lcd(0x20,16,2);

Is the only parameters needed to get I2C running with the I2C bus.
All the extra parameters you have added is for the 4 bit parallel connection to an LCD.

Because you have the DHT connected to pin4, you cannot have the LCD connected to it either.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom :slight_smile:

Hi,
How come you code is very similar to this?

Tom... :o

Many thanks to you all, great learning curve for me, TomGeorge i got the 12c libraries off the internet, i carried out your recommendations and my sketch now works great.

DHT 22 - Monitoring Temperature and Humidity
LCD 12C - Displaying current Temperature and Humidity
UNO R3 - Programmed to set a relayPin high when Humidity drops below set point.

I am now going to attempt to put it onto a stand alone Atmega AVR to save my UNO for another day, which is the best tutorial to do this do you all think?

Regards

Ray

Hey! How did you get the Setbacklight to work I kept getting an error? What lib you using?

ArduLost

It's all in my 2nd post further up this page.
Ray