Overview:
The temperature and humidity sensor is a very popular Arduino project that uses the DHT11 sensor to read the ambient temperature and humidity.
This tutorial explains how to read or control modules using Arduino libraries which will be very important in any project, for it not only makes the code minimalistic, it also saves precious time.
Hardware Used:
You can buy all this Hardware at Createlabz.
Software Used:
Application Discussion:
The temperature and humidity sensor is a popular Arduino project because of its practicality and of its use of inexpensive modules. Keep in mind that this project is scalable and could also be used with a DHT22 for a much more accurate reading.
The DHT 11
The DHT11 is a 4-pin sensor used to measure temperature and ambient humidity. This sensor can measure tempareture that ranges from 0°C - 50°C (± 2°C accuracy), and can measure ambient humidity that ranges from 20% RH - 90% RH (± 5% accuracy).
The DHT11 sensor included in the Arduino Upgraded Starter Kit comes with an ADC (Analog-to-Digital Converter) so the number of pins used will be lessend to three.
The 1602 LCD Module
The 1602 LCD module is a 16-pin device used for display purposes. It is labeled 1602 because 16 characters can be displayed in a row, and this particular module has 2 rows. In total, it can display 32 characters at once.
The 1602 LCD Module included in the Arduino Upgraded Starter Kit already has a soldered I2C Module. This is especially helpful for those with no soldering tools and those who are saving space for the pins since using an I2C module lessens the number of pins used from sixteen pins (for parallel interface) to only four (for I2C)
The potentiometer (small blue screw knob) mounted on the I2C module is used to control the contrast of the display. Turning it full counter clockwise will result in an empty display so make sure to test this out when troubleshooting later on!
Hardware Setup:
This is the breadboard configuration of the system. All parts are included in the Arduino Upgraded Starter Kit.
Software Setup:
These modules would require long codes to make it function accordingly. However, there is also the option of using libraries to make these modules work with far lesser code.
Before the arduino library tutorial, make sure you download these libraries into your computer:
- DHT by ElectroSchematics
- LiquidCrystal by fmalpartida
How to include Arduino libraries
In order to use the libraries downloaded above, it should be included by going to Sketch -> Include Library -> Add .ZIP Library...
Next, is to locate the folder where you downloaded the library on and Double-click the library ZIP file.
Aftwerwards, you can verify if you had included the library by going back to Sketch -> Include Library then scroll down until you find the Contributed libraries block and saw your included user-library.
Open the Arduino IDE and upload the code on the Arduino Uno Board.
Code:
//For the LCD I2C
#include <Wire.h>
#include <LCD.h.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //I2C pins declaration
//For DHT11
#include <DHT.h>
#define DHTPIN A0 //where DHT middle pin is connected to
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float humi;
float temp;
void setup() {
//Initializing LCD
lcd.begin(16,2); //Defining 16 columns and 2 rows of lcd display
lcd.backlight(); //To Power ON the back light
lcd.setCursor(0,0); //Defining positon to write from first row, first column.
lcd.clear();
//start sensor reading
dht.begin();
}
void loop() {
showTempHumi();
delay(2000);
}
void showTempHumi(){
humi = dht.readHumidity();
temp = dht.readTemperature();
lcd.clear();
lcd.print("Temp: ");
lcd.print(temp,2);
lcd.print(char(223));
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humi: ");
lcd.print(humi,2);
lcd.print("%");
}
Code Breakdown:
- setup()
- loop()
This is also a default Arduino function. Any commands set here will be looped indefinitely. The showTempHumi() function is called here and it's designed to allow the sensor to read the temperature and humidity every two seconds.
- showTempHumi()
lcd.clear()
in a loop so the updated temperature and humidity would show and will not simply append after the last character on the previous set.
Notice how the functions from the libraries are called
The dht
is signalling the code that the user is going to call a function from this library. That part of the function is user-defined and could be named anyway the user wanted as highlighted below.
The readHumidty()
is a function that is included inside the library. Library authors would usually
*Note: You can change DHT11 to DHT22 if you have the DHT22 sensor instead
Output:
This is the video of the project. Humid air was blown on top of the sensor to show that the readings do change.
Conclusion:
Now that the basics of setting up codes to accept libraries is done, future application of this knowledge will now be easier.
There will be more projects that will utilize this knowledge in the future, so stay tuned for more basic Arduino tutorials!
References:
https://store.createlabz.com/blogs/createlabz-tutorials/humidity-and-temperature-sensing-using-dht11-and-20-4-lcd-display-on-arduino-uno-1?_pos=1&_sid=5bfdb52c8&_ss=r
https://github.com/fmalpartida/New-LiquidCrystal
https://www.electroschematics.com/arduino-dht22-am2302-tutorial-library/