Author's Picture
Author: Joel Gray Published: 27 August 2020 Read Time: ~4 minutes

Temperature and Humidity Sensor – Arduino

How to Show Temperature and Humidity on a 2×16 LCD

Here we are going to use the DHT22 Temperature and Humidity Sensor and an I2C 2×16 LCD screen to display the current Temperature and Humidity.

Always try to be creative and do a little but more than what the tutorial shows you. Play with the code and maybe add another few components, this will help you more than just copying the code.

Links to what you’ll need:

1x Breadboard

1x Arduino Uno

1x DHT22 Sensor

9x Jumper Wires

1x I2C LCD (2×16)

1x USB-A Cable

How do I start?

The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.

We connect the SDA and SCL pins of the LCD screen to the SDA and SCL pins on the Arduino respectively. These are the two pins nearest the Reset Button, above AREF.

Simply connect the VCC pin of the LCD to the positive rail on the breadboard and the GND pin to the ground rail of the breadboard.

Do the same for the VCC and GND pins of the DHT22 sensor. Then connect the middle pin of the DHT22 sensor to Pin 7.

Finally connect the positive rail of the breadboard to the 5V pin on the Arduino, and connect the ground rail to the GND pin.

A quick summary of the circuit can be seen below:

5V Pin > Positive Rail on Breadboard

GND Pin > Ground Rail on Breadboard

DHT22 VCC Pin > +5V

DHT22 GND Pin > GND

DHT22 Middle Pin > Pin 7

LCD VCC Pin . +5V

LCD GND Pin > GND

SDA LCD > SDA Pin

SCL LCD > SCL Pin


Let’s run through what the code is doing

The following code is used to implement the project by uploading it to the Arduino Microcontroller.

We use #include <Library.h> to include the required libraries in the project. Then we use #define to define specific Pin 7 as the DHT22 sensor and to set DHTTYPE as DHT22. We then pass these two variables into the DHT library function.

Then we create a variable called t and h which are going to hold our temperature and humidity values. These are type ‘float’ as a float allows us to use decimal numbers and we can give more accurate readings.

Inside the Setup function we use the lcd.begin() and dht.begin() library specific functions to allow these devices to operate properly.

Then inside the main loop function we are assigning the dht.readHumidity() and dht.readTemperature() functions to assign the current values of Temperature and Humidity to our variables. After this we are simply using lcd.print() library function to show the values along with some text on the LCD screen.

Check out the information given on Arduino documentation to learn more.


Now open the Arduino IDE and copy the code below

If you don’t know how to do this, check out our Arduino Sketch set up tutorials and youtube videos.

  
// This program uses the DHT22 sensor and displays the data on an I2C LCD

// DHT libraries to allow the sensor to operate
#include <DHT.h>
#include <DHT_U.h>
//Wire library for communicating with I2C devices
#include <Wire.h>
//I2C LCD Library
#include <LiquidCrystal_I2C.h> 

#define DHTPIN 7                   // This is the pin which the data line on the DHT22 is connected to
#define DHTTYPE DHT22              // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);          // Give the variables to the DHT library function

// Setting temperature an humidity to float values due to their decimal points
float t;
float h;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // This is the address of the LCD

void setup(){
  // Starting the LCD and the DHT sensor
  lcd.begin(16,2);
  dht.begin();
  
}

void loop() {
  delay(2000);
  // Functions which read the Humidity and temperature and save them as 'h' and 't'
  h = dht.readHumidity();
  t = dht.readTemperature();

  // Two very simple sections of code to print the readings from the sensor in a neat manner,
  // on the serial monitor. Println is used to take a new line after printing the information.
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print("*C");
  lcd.print(" ");

  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print("% ");
  lcd.print(" ");

  delay(2000);
}
  
 
/*
  created 03/2020
  by Jamie Buick 

  graycode.ie
*/