DHT11 & LCD JHD 162A


I am a newcomer to this area. I copied the test code DHT11, and in it wrote the code for displaying on the LCD. Now I work humidity sensor problems, because my feeling shows insufficient moisture. Is there any additional line that shows me the 2 decimal moisture and temperature? I attach the log.

#define dht_dpin 14 //no ; here. Set equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on digital line 14, aka analog 0
//Other digital lines can be used, e.g. D2

#include <LiquidCrystal.h>

// LiquidCrystal display based on JHD659 FN/JG
// GND // 1
// +5v // 2
int RS = 12; // 4
//int RW = GND; // 5
int ENABLE = 11;// 6
int d4 = 5; // 11
int d5 = 4; // 12
int d6 = 3; // 13
int d7 = 2; // 14

const int numRows = 2;
const int Humidity = 16;

LiquidCrystal lcd(RS, ENABLE, d4, d5, d6, d7);
int backLight = 15; // pin 15 will control the backlight

byte bGlobalErr;//for passing error code back from complex functions.
byte dht_dat[4];//Array to hold the bytes sent from sensor.

void setup(){
InitDHT();//Does what’s necessary to prepare for reading DHT
delay(300);//Let system settle
Serial.println(“Humidity and temperature\n\n”);
delay(700);//Wait rest of 1000ms recommended delay before
//accessing sensor
//end “setup()”

pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace ‘HIGH’ with ‘LOW’ to turn it off.
lcd.begin(16, 2); // rows, columns. use 8,1 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 10

void loop(){
ReadDHT();//This is the “heart” of the program.
//Fills global array dht_dpin, and bGlobalErr, which
//will hold zero if ReadDHT went okay.
//Must call InitDHT once (in “setup()” is usual) before
//call ReadDHT.
switch (bGlobalErr){
case 0:
lcd.print(“Hum = “);
lcd.print(dht_dat[0], DEC);
lcd.print(dht_dat[1], DEC);
lcd.print("% ");
lcd.print(“Temp = “);
lcd.print(dht_dat[2], DEC);
lcd.print(dht_dat[3], DEC);
lcd.println("C ");
case 1:
Serial.println(“Error 1: DHT start condition 1 not met.”);
case 2:
Serial.println(“Error 2: DHT start condition 2 not met.”);
case 3:
Serial.println(“Error 3: DHT checksum error.”);
Serial.println(“Error: Unrecognized code encountered.”);
}//end “switch”
delay(1000);//Don’t try to access too frequently… in theory
//should be once per two seconds, fastest,
//but seems to work after 0.8 second.
}// end loop()

/Below here: Only “black box” elements which can just be plugged
unchanged into programs. Provide InitDHT() and ReadDHT(), and a function
one of them uses.

void InitDHT(){
}//end InitDHT

void ReadDHT(){
/Uses global variables dht_dat[0-4], and bGlobalErr to pass
“answer” back. bGlobalErr=0 if read went okay.
Depends on global dht_dpin for where to look for sensor.
byte dht_in;
byte i;
// Send “start read and report” command to sensor…
// First: pull-down I/O pin for 23000us
/*aosong.com datasheet for DHT22 says pin should be low at least
500us. I infer it can be low longer without any]
penalty apart from making “read sensor” process take
longer. */
//Next line: Brings line high again,
// second step in giving “start read…” command
delayMicroseconds(40);//DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking line
//low. That low should last 80us. Acknowledges “start read
//and report” command.

//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.


bGlobalErr=1;//dht start condition 1 not met
}//end “if…”


bGlobalErr=2;//dht start condition 2 not met
}//end “if…”

/After 80us low, the line should be taken high for 80us by the
sensor. The low following that high is the start of the first
bit of the forty to come. The routine “read_dht_dat()”
expects to be called with the system already into this low.
//now ready for data reception… pick up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat = read_dht_dat();
//Next: restore pin to output duties
//Next: Make data line high again, as output from Arduino
//Next see if data received consistent with checksum received
byte dht_check_sum =

  • dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];*
    /*Condition in following “if” says "if fifth byte from sensor
    _ not the same as the sum of the first four…"*/_
    if(dht_dat[4]!= dht_check_sum)
  • {bGlobalErr=3;}//DHT checksum error*
    };//end ReadDHT()
    byte read_dht_dat(){
    //Collect 8 bits from datastream, return them interpreted
    //as a byte. I.e. if 0000.0101 is sent, return decimal 5.
    //Code expects the system to have recently entered the
    //dataline low condition at the start of every data bit’s
    //transmission BEFORE this function is called.
  • byte i = 0;*
  • byte result=0;*
  • for(i=0; i< 8; i++){*
  • //We enter this during the first start bit (low for 50uS) of the byte*
  • //Next: wait until pin goes high*
  • while(digitalRead(dht_dpin)==LOW);*
  • //signalling end of start of bit’s transmission.*
  • //Dataline will now stay high for 27 or 70 uS, depending on*
  • //whether a 0 or a 1 is being sent, respectively.*
  • delayMicroseconds(30);//AFTER pin is high, wait further period, to be*
  • //into the part of the timing diagram where a 0 or a 1 denotes*
  • //the datum being send. The “further period” was 30uS in the software*
  • //that this has been created from. I believe that a higher number*
  • //(45?) might be more appropriate.*
  • //Next: Wait while pin still high*
  • if (digitalRead(dht_dpin)==HIGH)*
  • result |=(1<<(7-i));// “add” (not just addition) the 1*
  • //to the growing byte*
  • //Next wait until pin goes low again, which signals the START*
  • //of the NEXT bit’s transmission.*
  • while (digitalRead(dht_dpin)==HIGH);*
  • }//end of “for…”*
  • return result;*
    }//end of “read_dht_dat()”[/quote]
    best regards

Would you please modify your post, and change the word quote in the to code? In [ code ][ /code ] tags, things like [ i ] are not interpreted as formatting tags. In [ quote ][ /quote ] blocks, they are. It is hard to follow your code when the [ i ] stuff is missing.

As an example, it is hard to tell what this:

for (i=0; i<5; i++)
   dht_dat = read_dht_dat();

is supposed to be doing. It looks like [ i ] might be missing.

Is there any additional line that shows me the 2 decimal moisture and temperature?

It appears as though ReadDHT() is setting 4 global values - the integer portion of the temperature, the decimal portion as an integer (multiplied by 100), the integer portion of the humidity, and the decimal portion of the humidity (multiplied by 100).

You are printing these now, to the LCD, with decimal points in appropriate places, so it is not clear what your question is. There is a problem with the way you are printing them. 29.03 degrees will be returned as 29 and 3, which you print as 29.3.

There's further information on using a DHT11 at....