Incorrect humidity readings 1602 LCD i2c DHT11

First off I hope I've put this in the correct place. I've troubleshooted the DHT11 and it seeems to be working fine, so it must be an issue with the LCD code, if its in the wrong place I'm sorry and would appreciate if a mod could move it.

I followed a guide Here to set up a temperature and humidity sensor.

The issue is that the display gives erroneous readings for humidity, for example if I increase the humidity (by gently breathing on the sensor) the humidity % goes to 0% - 9% and the LCD occasionally prints %% (two % symbols) when this error occurs.
I've checked to make sure its not the sensor itself by running the DHT test code and the correct humidity is always displayed via the serial monitor, even when breathing gently on the sensor to alter the readout. The temperature is usually different than the temperature displayed on the LCD too.
As such I assume that the problem is with the code for displaying things, especially as I'm getting things like an extra % symbol at times.

Looking at the comments on the guide there is an issue with the code (it should be "#include <DHT.h>" but instead it's "#include <dht.h>" I've tried changing this but I run into more issues with the code if I do, two lines down under "dht DHT" with an error message of; 'dht' does not name a type

Any ideas what might be wrong?

/* DHT-11 sensor with 12c 16x2 LCD with Arduino uno
   Temperature and humidity sensor displayed in LCD
   based on: http://www.ardumotive.com/how-to-use-dht-22-sensor-en.html and
   https://www.ardumotive.com/i2clcden.html for the i2c LCD library by Michalis Vasilakis
   Recompile by adhitadhitadhit
   Notes: use LCD i2c Library from link above, i'm not sure why but new Liquidcristal library from Francisco Malpartida  isn't working for me
   other thing, check your */

//Libraries
#include <dht.h> // sensor library using lib from https://www.ardumotive.com/how-to-use-dht-22-sensor-en.html
#include <LiquidCrystal_I2C.h> // LCD library using from  https://www.ardumotive.com/i2clcden.html for the i2c LCD library 
#include <Wire.h> 
dht DHT;

//Constants
#define DHT11_PIN 2     // DHT 11  (AM2302) - pin used for DHT22 
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 after finding it from serial monitor (see comment above) for a 16 chars and 2 line display

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

void setup()
{
    
    lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setBacklight(HIGH);
}

void loop()
{
    int chk = DHT.read11(DHT11_PIN);
    //Read data and store it to variables hum and temp
    hum = DHT.humidity;
    temp= DHT.temperature;
    //Print temp and humidity values to LCD
    lcd.setCursor(0,1);
    lcd.print("Humidity: ");
    lcd.print(hum);
    lcd.print("%");
    lcd.setCursor(0,0);
    lcd.print("Temp: "); 
    lcd.print(temp);
    lcd.print((char)223);
    lcd.println("C     ");
    
    delay(2000); //Delay 1 sec between temperature/humidity check

  Serial.print(F("Humidity: "));
  Serial.print(hum);
  Serial.print(F("%  Temperature: "));
  Serial.print(temp);
  Serial.print(F("°C "));
}

Edit: Ignore the last 5 lines of code (Serial.print) a friend and I were seeing if we could get it to display to the serial monitor using this sketch but instead used the DHT tester sketch that's in the DHT library.

Hi and welcome.

This code is quite basic, and for your learning curve that's OK.
All it does is read the sensor, and send these readings 2 ways.
The data in your serial monitor is printed as 1 line growing larger about every 2 seconds.
The data on your LCD is overwritten every 2 seconds.
One of your problems is that overwriting LCD contents does not necessarily mean erasing the old data.
So if there is an old value of 12%, which is overwritten by 8%, you'll end up with a display saying 8%%.

It is good practice to only write updated contents to your LCD instead of rewriting your entire screen.
You already learned how to jump to a certain location on your LCD, use that to go to where your values are displayed.
You could choose to first erase and then overwrite, or to make sure your overwriting always covers enough space to make sure you'll end up seeing what you'd want to see.

You mentioned a test program showing correct values over serial, you didn't mention whether the values over serial from your sketch are correct or the same as what your display shows.

You seem to be using a dht library (as well as a LCD library) from some alternative source.
I'd use the IDE library manager (under Sketch in the menu) to select the DHT library, as that is what will be most used and will be reference for most users here.
I can't tell if and what the differences between these two libraries would be, next to the fact that one is written in lower case and the other in upper case (you need to call it using the correct case, or create a definition to have the IDE do some conversion).

There's a better LCD library available too, but let's first solve your problem.

First off, my apologies, those 5 lines (serial.print) shouldn't have been in that sketch, I was seeing if I could get the serial monitor up with that sketch however it didn't work, I should have double checked my code before posting it as that caused some confusion.

"It is good practice to only write updated contents to your LCD instead of rewriting your entire screen."

I assume what you mean by this is that the code for the things constantly displayed on the LCD (Temp, Humidity and % as well as °C) should all be written in the setup, rather than the loop as these never change?

The libraries I'm using were the ones in the guide, that I followed and I only modified the code from the guide a little (changed it so my DHT 11 would work, switched temperature and humidity around so temperature was on the top row and changed "Celsius" to "°C" as Celsius was too long to display on the LCD and "Celsiu" just looked a bit silly) as for swapping the libraries over, I'm very new to using Arduino, how would I go about doing that? I'm not looking for you to write the code for me obviously, just point me in the right direction.

I ran DHT11 test from the dht library and when increasing the humidity by gently breathing on the sensor I ran into the same error in the serial monitor.

00:27:16.227 -> DHT11, Checksum error, 62.0, 16.0
00:27:18.250 -> DHT11, Checksum error, 62.0, 16.0
00:27:20.300 -> DHT11, Checksum error, 61.0, 17.0
I gently breathe on the sensor
00:27:22.319 -> DHT11, Checksum error, 0.0, 18.0
00:27:24.343 -> DHT11, Checksum error, 1.0, 18.0
00:27:26.404 -> DHT11, Checksum error, 2.0, 18.0
00:27:28.422 -> DHT11, Checksum error, 3.0, 19.0
00:27:30.450 -> DHT11, Checksum error, 3.0, 19.0
00:27:32.504 -> DHT11, Checksum error, 3.0, 19.0

However when running DHT tester from the "DHT sensor library" I dont get the error and instead the humidity just increases, as would be expected.

00:33:19.751 -> Humidity: 65.00% Temperature: 14.40°C 57.92°F Heat index: 13.59°C 56.47°F
I breathe on the sensor again
00:33:21.787 -> Humidity: 66.00% Temperature: 14.40°C 57.92°F Heat index: 13.62°C 56.51°F
00:33:23.856 -> Humidity: 67.00% Temperature: 15.10°C 59.18°F Heat index: 14.41°C 57.95°F
00:33:25.892 -> Humidity: 69.00% Temperature: 15.80°C 60.44°F Heat index: 15.24°C 59.43°F
00:33:27.961 -> Humidity: 68.00% Temperature: 16.50°C 61.70°F Heat index: 15.98°C 60.77°F
00:33:29.995 -> Humidity: 68.00% Temperature: 16.80°C 62.24°F Heat index: 16.31°C 61.36°F
00:33:32.037 -> Humidity: 68.00% Temperature: 17.10°C 62.78°F Heat index: 16.64°C 61.95°F
00:33:34.100 -> Humidity: 69.00% Temperature: 17.40°C 63.32°F Heat index: 17.00°C 62.60°F
00:33:36.169 -> Humidity: 70.00% Temperature: 17.40°C 63.32°F Heat index: 17.02°C 62.64°F

Humidity eventually peaks out at 71% and then goes back down to 65%

I would say that its a bad sensor (it came in a cheap kit) but I would assume it would show the same error across both libraries.

I've tried changing the library (#Include DHT.h) however when I try to verify the code I get "'dht' does not name a type" on the line dht DHT. If I omit that line altogether I get more errors, such as "expected primary-expression before '.' token" on the line in my loop reading "temp= DHT.temperature;"

I had a friend who does a lot of coding (C++ C# and Python) come over and take a look yesterday and he was as baffled as I was.

Any help would be much appreciated.

DavidDavidson:
"It is good practice to only write updated contents to your LCD instead of rewriting your entire screen."
I assume what you mean by this is that the code for the things constantly displayed on the LCD (Temp, Humidity and % as well as °C) should all be written in the setup, rather than the loop as these never change?

That is certainly part of it; there is neither need nor is it desirable to re-print everything as it causes annoying flickering.

The next point is that it makes no sense to re-print any data unless it changes from one reading to the next and unless there has been sufficient time for it to have actually been read. Your delay() (two seconds specified while the comment refers to "1 sec") fulfils the latter but not the former. You need to "remember" the values from one measurement to the next in order to compare them.

It is definitely inappropriate to use lcd.clear() in loop(). :astonished: (Fortunately, you did not. :grinning: )

I can't say much about the libraries as I have not experimented with the DHT11 or DHT22 devices, but the use of floats seems quite unnecessary and I would expect a proper library would simply give values as 8 or 12 or 16 bit integers denoting tenths or 100ths of the unit as does the DS18B20 library.

Your serial monitor output should be posted the same as "code" and for all the same reasons. :roll_eyes: