Temp and hum on 2line lcd

Ive got a problem, Im having an dht11, and a 2 channel relay working. But no i want to use an 2 line lcd screen for the temp and humidity readings. But how do i change my code for working.

the code i use is:

#include <dht.h>

#define dht_dpin 2
#define CH1 8   
#define CH2 9 

dht DHT;

void setup(){
   pinMode(CH1, OUTPUT);
   pinMode(CH2, OUTPUT); 

 Serial.begin(9600);
  delay(300);//Let system settle
  Serial.println("Humidity and temperature\n\n");
  delay(700);
  
}

void loop(){
   DHT.read11(dht_dpin);
    Serial.print("Lucht vochtigheid = ");
    Serial.print(DHT.humidity);
    Serial.print("% ");
    Serial.print("Graden = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");
  delay(2000);
  
if (DHT.temperature < 30)
    digitalWrite(CH1, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH1, LOW);        // Otherwise, turn the relay off.
    delay(2000); 
if (DHT.temperature > 30)
    digitalWrite(CH1, LOW);        // If so, turn on the relay.
else
    digitalWrite(CH1, HIGH);        // Otherwise, turn the relay off.

if (DHT.humidity > 70)
    digitalWrite(CH2, LOW);        // If so, turn on the relay.
else
    digitalWrite(CH2, HIGH);        // Otherwise, turn the relay off.
    delay(2000); 
if (DHT.humidity < 70)
    digitalWrite(CH2, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH2, LOW);        // Otherwise, turn the relay off.

}

and ive got an 4 pinned lcd screen

Vcc
GND
SDA
SCL

You have to get the display working by itself first. Take a look at some of the previous posts with LCD and I2C in the title for some ideas.

Don

Hi !

and ive got an 4 pinned lcd screen

Please post more information about your display ! :wink: Is it LCD 16x2 with I2C?

if (DHT.temperature < 30)
    digitalWrite(CH1, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH1, LOW);        // Otherwise, turn the relay off.
    delay(2000); 
if (DHT.temperature > 30)
    digitalWrite(CH1, LOW);        // If so, turn on the relay. 
else
    digitalWrite(CH1, HIGH);        // Otherwise, turn the relay off.  

if (DHT.humidity > 70)
    digitalWrite(CH2, LOW);        // If so, turn on the relay. 
else
    digitalWrite(CH2, HIGH);        // Otherwise, turn the relay off. 
    delay(2000); 
if (DHT.humidity < 70)
    digitalWrite(CH2, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH2, LOW);        // Otherwise, turn the relay off.

}

This will be the same :smiley:

if (DHT.temperature < 30)
    digitalWrite(CH1, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH1, LOW);        // Otherwise, turn the relay off.
    delay(2000); 

if (DHT.humidity > 70)
    digitalWrite(CH2, LOW);        // If so, turn the relay off. 
else
    digitalWrite(CH2, HIGH);        // Otherwise, turn on the relay. 
    delay(2000);

p.s. DHT11 is not good sensors. I have an expirience with this!

yes it is an lcd with I2C but ive got the thing working with with only the sensor, but got problems with having the readings on the display, and using this code at the same time.

So how do i get the serial reading on the lcd instead of my pc/laptop

What are recommendations for other temp, and hum sensors, ?

First of all you practice with

Serial.print("Hello World");
Serial.print(123);
...

then you try

lcd.print("Hello World");
lcd.print(123);
...

Note that Serial can use a println() to go to the next line. With an lcd you use lcd.setCursor(col, row) to get to the right position.

Just experiment.

David.

What are recommendations for other temp, and hum sensors, ?

DHT22 works better. But I recomend to use SHT20.

So how do i get the serial reading on the lcd instead of my pc/laptop

Use google: http://www.instructables.com/id/I2C-LCD-Controller-Part-II/

EDIT: Okey, I wrote this code for you. Must work !

#include <dht.h>  //http://playground.arduino.cc/Main/DHTLib
#include <Wire.h> //this library arduino IDE has itself
#include <LiquidCrystal_I2C.h> //http://playground.arduino.cc/Code/LCDi2c 

#define dht_sigPin A0 // signal pin connected to A0

dht DHT;

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address  for a 16 chars and 2 line display (0x27)
 
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif

uint8_t zero[8]  = {0x2,0x5,0x2}; //degrees symbol

void setup(){

  lcd.init();                      // initialize the lcd 
  lcd.backlight();             // backlight ON 
  
  lcd.createChar(0, zero); // create degrees symbol
  lcd.home();

}

void loop() {

  DHT.read11(dht_sigPin);  // read DHT11 temperature and humidity sensor values
  lcd.setCursor(0,0); // set cursor (char, line);
  lcd.print("T="); // print T = 
  lcd.print(DHT.temperature, 0); // print temperature value
  lcd.printByte(0);  // print degrees symbol
  lcd.print("C"); //print C
  lcd.setCursor(0,1); // set cursor (char, line);
  lcd.print("H="); //print H=
  lcd.print(DHT.humidity, 0); // print humidity value
  lcd.print("%"); // print %
  delay(5000); // set time for measurements (5000 = 5 sec)
  lcd.clear();

}

i will be back soon when my arduino software is running like it should be, its now just giving errors

KamikazejunkyNL: i will be back soon when my arduino software is running like it should be, its now just giving errors

How it's look? You must install libraries !

heninsh:
This will be the same :smiley:

No it is not the same, re-read the original code, you will see it has three states not two, eg:

< 30 on
=30 on/off every 2s

30 off

heninsh:
p.s. DHT11 is not good sensors. I have an expirience with this!

These sensors works fine, they are just not very accurate, but where accuracy is not needed e.g. for a simple control system they are perfectly adequate. They are also very cheap and readily available as simple to use breakout boards, and hence ideal for experiments with an Arduino.

@KamikazejunkyNL Get it working with the sensor you have. You can investigate other sensor options later if you really find you need the accuracy.

No it is not the same, re-read the original code, you will see it has three states not two, eg:

< 30 on
=30 on/off every 2s
> 30 off

Yes, you are right. I saw that before, but is this option realy needed? =30 on/off every 2s

These sensors works fine, they are just not very accurate, but where accuracy is not needed e.g. for a simple control system they are perfectly adequate. They are also very cheap and readily available as simple to use breakout boards, and hence ideal for experiments with an Arduino.

Yes, I agree. For arduino is good. And price is very low.

My experiment with two DTH11 and DHT22 at the same place and same time !I tried sensors with diffirent controllers UNO, Mega, Pro mini, Nano. And all three sensors with one controller. The same results. Who show right values? :smiley:

Results:

DHT11 /1 DHT11 /2 DHT 22

T H T H T H

28 C 34% 23 C 19% 26 C 43%

heninsh:
Who show right values? :smiley:

You need an accurate meter to find out. The results look suspiciously like the sensors have been abused. :slight_smile:

The point is we don’t know whether KamikazejunkyNL happens to have an accurate DHT11 or not, or whether absolute accuracy is needed, so money and time could easily get wasted.

KamikazejunkyNL simply wants to know how to integrate the DHT11 LCD code into the sketch. So any other changes can wait until later.

bodmer: . . . KamikazejunkyNL simply wants to know how to integrate the DHT11 LCD code into the sketch. So any other changes can wait until later.

And we still don't know if he can anything at all to display on the LCD even without the sensors.

Don

KamikazejunkyNL: yes it is an lcd with I2C but ive got the thing working with with only the sensor, but got problems with having the readings on the display, and using this code at the same time.

My reading of this is that you have a sketch that puts sensor reading on the LCD and you have the sketch in post #1, but you have not been able to add the LCD code to the posted sketch.

Is this interpretation correct?

We shouldn't have to guess.

Don

my lcd is working fine, i was busy with the program 2 days ago, but my libraries are gone, so didnt have any time anymore to solve that problem, i had a picture with the lcd working. i had a code with the dht11 and a relay working, but now is my problem i want the readings on the lcd and i dont know how..

Im sorry for the late response but had 2 long working days.

so i hope someone could help me.

im trying to get my software working tomorrow.

OK, great.

Post the working LCD sketch and it will be easy for someone to integrate the relevant bits into the relay driver sketch.

/*-----( Import needed libraries )-----*/
#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
dht11 DHT11;

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
  lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines, turn on backlight
  lcd.backlight();
  // Print a message to the LCD.
  //lcd.setCursor(0, 1);
  lcd.print("Temp en lucht");

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{


  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }
  lcd.setCursor(0, 1);

  lcd.print("C=");
  lcd.print((float)DHT11.temperature, 0);
  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

  lcd.print(" F=");
  lcd.print(Fahrenheit(DHT11.temperature), 0);
  Serial.print("Temperature (oF): ");
  Serial.println(Fahrenheit(DHT11.temperature), 2);
  
  lcd.print("  H=");
  lcd.print((float)DHT11.humidity, 0);
  lcd.print("%");
  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);  

  Serial.print("Temperature (K): ");
  Serial.println(Kelvin(DHT11.temperature), 2);

  Serial.print("Dew Point (oC): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

  Serial.print("Dew PointFast (oC): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

  delay(2000);
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

this is the code i got from the internet to have my lcd working. i lost all my libraries in a weird way but its working again. now how to put the 2 channel relay to work with it.

You are using two different DHT libraries!

I have added the relay driver code to the LCD example. I have not tried it as I am not sure which libraries you have been using!

/*-----( Import needed libraries )-----*/
#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
dht11 DHT11;

#define CH1 8   //<<<<
#define CH2 9   //<<<<

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2

void setup()   /*----( SETUP: RUNS ONCE )----*/
{

   pinMode(CH1, OUTPUT); //<<<<
   pinMode(CH2, OUTPUT); //<<<<

  Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
  lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines, turn on backlight
  lcd.backlight();
  // Print a message to the LCD.
  //lcd.setCursor(0, 1);
  lcd.print("Temp en lucht");

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{


  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }
  lcd.setCursor(0, 1);

  lcd.print("C=");
  lcd.print((float)DHT11.temperature, 0);
  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

  lcd.print(" F=");
  lcd.print(Fahrenheit(DHT11.temperature), 0);
  Serial.print("Temperature (oF): ");
  Serial.println(Fahrenheit(DHT11.temperature), 2);
 
  lcd.print("  H=");
  lcd.print((float)DHT11.humidity, 0);
  lcd.print("%");
  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2); 

  Serial.print("Temperature (K): ");
  Serial.println(Kelvin(DHT11.temperature), 2);

  Serial.print("Dew Point (oC): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

  Serial.print("Dew PointFast (oC): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

// Added these lines 
if (DHT11.temperature < 30)
    digitalWrite(CH1, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH1, LOW);        // Otherwise, turn the relay off.
    delay(2000);
if (DHT11.temperature > 30)
    digitalWrite(CH1, LOW);        // If so, turn on the relay.
else
    digitalWrite(CH1, HIGH);        // Otherwise, turn the relay off.

if (DHT11.humidity > 70)
    digitalWrite(CH2, LOW);        // If so, turn on the relay.
else
    digitalWrite(CH2, HIGH);        // Otherwise, turn the relay off.
    delay(2000);
if (DHT11.humidity < 70)
    digitalWrite(CH2, HIGH);        // If so, turn on the relay.
else
    digitalWrite(CH2, LOW);        // Otherwise, turn the relay off.

}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}

thank you, im going to test it tomorrow night,

for the people who are interessted, ill try to explain what im going to build at home,

im going to make a self controlling herbs and plants plantation from old hifi furniture.

going to make with the this code, an air flow/temp control

and an self watering system with an soil humidity measure sensor.

so if the temp is to high i have 2 12v switched on, and if de soil is to dry, im going to try making an self spraying system.

there is one picture which youve got the idea, i started with it today. :smiley: i hope if there are more questions from me, u could help me

bodmer: You are using two different DHT libraries!

i did have problems, with my libraries i had to instal them all again, im now switched from windows to linux on an other pc for arduino so all my codes which i have are having to be changed