Measure temperature/humidity using DHT22 + LCD i2c + Arduino
Hello there,
In this tutorial we’ll try to measure temperature and humidity using the DHT22, DHT22 and DHT11 are famous sensors used a lot for measuring temperature and humidity, they are pretty much the same, simple to interface and simple to use.
I also add to the project a LCD i2c screen to visualize the measures, check LCD i2c tutorial in case you don’t know how to use it.
And for the temperature I’m using both Celsius and Fahrenheit, you chose what suits you.
Differrences between DHT11 and DHT22:
DHT11 | DHT22 | |
Temperature range | 0-50 °C | -40 – 80 °C |
Error | ±2 °C | ±0.5 °C |
Humidity range | 20-80% | 0-100% |
Error | ±5% | ±2-5% |
Sampling Rate | 1Hz | 0.5Hz |
As you can see the 22 is more accurate and have high range but it reads one value every 2 seconds unlike the 11 that reads every second, but accuracy is more important.
In this tutorial I’m using the 4 pins version of the DHT22, unlike my DHT11 tutorial where I used the 3 pin version of the DHT11, both versions are the same because the other pin is not used, also they add a pull-up resistor, I didn’t use it because i didn’t see any difference, you can add it if you want but don’t forget that pull-up resistor dissipate more power in case you want to use it in a project using batteries or low consumption circuit…
Wirings:
Wiring 1: wiring directly to Arduino to visualize the measures on the Arduino IDE monitor
N.B: In case you want to add a pull-up resistor you can leave this wiring as is it then add a 10k resistor with 5v from arduino and pin 2 of the DHT22
The pin 3 is not connected
Wiring 2: Adding the LCD i2c screen

Libraries:
- DHT library: works for both DHT11 and 22 needs only a little change in the code you can see it in codes, download here (Just download the zip file, then in arduino IDE add it and it will install quickly)
- LCD i2c library: Newliquidcrystal Download here
Codes
Download here all used codes, then chose what suits your project (contains, printing in C and F in serial monitor, printing in C on LCD and printing in F on LCD).
Code 1: Here it’s a direct use with Arduino, the values are shown on the serial monitor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//This code is to use with DHT22 module to measure temperature and humidiy and print the result on Arduino monitor //It reads the value given by the sensor and print them on monitor, for the temperature it shows both in Celsius and Farnheit //Refer to https://surtrtech.com/ for more information #include <dht.h> //DHT library works for both 11 and 22 dht DHT; //Declaring the DHT #define DHT22_PIN 7 //DHT data pin here I used D7 you can change it void setup(){ Serial.begin(9600); //Serial baude rate } void loop() { int chk = DHT.read22(DHT22_PIN); //Reading data from the sensor float t= DHT.temperature*(9/5) + 32;//Converting the Celsius to Farenheit Serial.print("Temperature = "); Serial.print(DHT.temperature);//Showing the temperature in Celsius Serial.print("°C "); Serial.print(t);//Showing the temperature in Farenheit Serial.println(" °F"); Serial.print("Humidity = "); Serial.println(DHT.humidity);//Showing the humidity delay(2000);//Repeating every 2s make sure the reading is done every 2s or more as the sampling rate of the DHT22 is 0.5Hz } |
Code 2: Using the LCD i²c, values are shown in °C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//This code is to use with DHT22 Temperature/humidity sensor with LCD i2c screen //We measure the values of the temperature in Celsius and humidity then print them on the screen every 2 seconds //Refer to https://surtrtech.com/ for more information #include <dht.h> //DHT and LCD libraries #include <LiquidCrystal_I2C.h> #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 #define DHT22_PIN 7 //Declaring where the DHT signal pin is wired LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); dht DHT; //Declaring the DHT as a dht type to use it later void setup(){ lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); lcd.home (); } void loop() { lcd.clear(); lcd.setCursor(0,0); int chk = DHT.read22(DHT22_PIN); //Reading data from the module lcd.print("Temp: "); lcd.print(DHT.temperature); //Showing temperature value in Celsius lcd.print(" C"); lcd.setCursor(0,1); lcd.print("Humidity: "); lcd.println(DHT.humidity); //Showing humidity percentage lcd.print(" %"); delay(2000); //Refreshing every 2s } |
Code 3: Using the LCD i²c, values are shown in °F
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
//This code is to use with DHT22 Temperature/humidity sensor with LCD i2c screen //We measure the values of the temperature in Farenheit and humidity then print them on the screen every 2 seconds //Refer to https://surtrtech.com/ for more information #include <dht.h> //DHT and LCD libraries #include <LiquidCrystal_I2C.h> #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); dht DHT; //Declaring the DHT as a dht type to use it later #define DHT22_PIN 7 //Declaring where the DHT signal pin is wired void setup(){ lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); lcd.home (); } void loop() { lcd.clear(); lcd.setCursor(0,0); int chk = DHT.read22(DHT22_PIN); //Reading data from the module float t= DHT.temperature*(9/5) + 32;//Converting Celsius to Farenheit lcd.print("Temp: "); lcd.print(t); //Showing temperature value in Farenheit lcd.print(" F"); lcd.setCursor(0,1); lcd.print("Humidity: "); lcd.println(DHT.humidity); //Showing humidity percentage lcd.print(" %"); delay(2000); //Refreshing every 2s } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
It tell me no such file or directory on the line where it says dht
Did you install the DHT library?