In this article, we will learn about how we send temperature and humidity data from a DHT22 sensor and displays it on an LCD screen using an Arduino board. The LCD screen is initialized with its pins and dimensions, and the temperature is printed in Celsius or Fahrenheit depending on the value of a boolean variable. The humidity value is also displayed on the screen.this program displays a message from Quartz Components on the screen.
component required
- Arduino board (such as Arduino Uno)
- DHT22 temperature and humidity sensor
- 16x2 LCD screen
- Potentiometer (for adjusting the contrast of the LCD screen)
- Breadboard
- Jumper wires (male to male and male to female)
- USB cable (for connecting the Arduino board to a computer or power source)
DHT22 pinout
The DHT22 is a digital temperature and humidity sensor that communicates with a microcontroller using a single wire communication protocol called "One-wire interface". Here is the pinout of the DHT22 sensor:
Vcc: Connect this pin to a power supply with a voltage between 3 and 5 volts DC.
Data Out: This is the digital output pin of the sensor. Connect it to a microcontroller's input pin to read the temperature and humidity data.
Ground: Connect this pin to the ground of the power supply.
Note that the DHT22 sensor should be connected to a pull-up resistor (usually 10K ohms) between the Vcc and Data Out pins to ensure proper communication with the microcontroller.
16X2 LCD display pinout
Sure, here is the pinout of a standard 16x2 LCD display in bullet points:
Pin 1: VSS (Ground)
Pin 2: VDD (+5V Power)
Pin 3: V0 (Contrast Adjust)
Pin 4: RS (Register Select)
Pin 5: RW (Read/Write)
Pin 6: E (Enable)
Pin 7: D0 (Data 0) - Not used in 4-bit mode.
Pin 8: D1 (Data 1) - Not used in 4-bit mode.
Pin 9: D2 (Data 2) - Not used in 4-bit mode.
Pin 10: D3 (Data 3) - Not used in 4-bit mode.
Pin 11: D4 (Data 4)
Pin 12: D5 (Data 5)
Pin 13: D6 (Data 6)
Pin 14: D7 (Data 7)
Pin 15: A (Anode +) - Backlight Positive (+)
Pin 16: K (Cathode -) - Backlight Negative (-)
Note: this pins 7 through 10 (D0-D3) are not used in 4-bit mode. In 4-bit mode, only pins 4 through 7 (RS, RW, E, D4-D7) are used to communicate with the LCD. The V0 pin is used to adjust the contrast of the display, and we can be connected to a potentiometer to allow for adjustment of the contrast by us. The A (Anode +) and K (Cathode -) pins are used to control the backlight of the LCD.
CIRCUIT DIAGRAM for interfacing arduino with dht22
In this, we can see DHT22 connected with Pin 8 of Arduino Uno, VCC connected with 5V of Arduino Uno, and ground connected with Arduino Ground .
Arduino Code
Install the DHT sensor library in the Arduino IDE. You can do this by navigating to Sketch -> Include Library -> Manage Libraries, and then searching for "DHT sensor library" and installing it.
Write a program in the Arduino IDE to read the temperature and humidity values from the DHT22 sensor. Here is an example program that reads the temperature and humidity values from the DHT22 sensor and prints them to the serial monitor:
#include <DHT.h>
#define dataPin 8 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor is used
dht DHT; // Initialize DHT sensor
void setup() {
Serial.begin(9600); // Start serial communication
dht.begin(); // Start DHT sensor
}
void loop() {
float temperature = dht.readTemperature(); // Read temperature value from DHT sensor
float humidity = dht.readHumidity(); // Read humidity value from DHT sensor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000); // Wait for 2 seconds
}
Circuit Diagram for INTERFACING ARDUINO UNO WITH 16X2 LED DISPLAY
STEP 1:- Connect the VSS pin of the LCD display to the GND pin of the Arduino Uno board. To interface an Arduino Uno board with a 16x2 LCD display, you can
STEP 2:- Connect the VDD pin of the 16 x2 LCD display to the +5V pin of the Arduino board.
STEP 3:- Connect the V0 pin of the 16 x2 LCD display to a potentiometer that is connected to the GND and +5V pins of the Arduino board. This will help you to adjust the contrasts of the display.
STEP 4:- Connect the RS pin of the LCD display to any digital output pin of the Arduino Uno board, for example, pin 12.
STEP 5:- Connect the RW pin of the 16x2 LCD display to the GND pin of the Arduino board.
STEP 6:- Connect the E pin of the LCD display to any digital output pin of the Arduino Uno board, for example, pin 11.
STEP 7:- Connect the D4, D5, D6, and D7 pins of the 16 x2 LCD display to any digital output pins of the Arduino Uno board, for example, pins 5, 4, 3, and 2 respectively.
STEP 8:- Connect the A (Anode +) and K (Cathode -) pins of the LCD display to the +5V and GND pins of the Arduino Uno board respectively.
Arduino Code
Install the LiquidCrystal library in the Arduino IDE. You can do this by navigating to
Sketch -> Include Library -> LiquidCrystal.
Write a program in the Arduino IDE to the display some text on the LCD display. Here is an example program that displays "Hello, World!" on the first line of the LCD display:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4); // Initialize the LCD display
void setup()
{
lcd.begin(16, 2); // Set the size of the LCD display to 16x2
lcd.print("Hello, World!"); // Print "Hello, World!" in the first line of the LCD display }
void loop() { // Nothing to do here }
}
After Upload the program to the Arduino and you should see "Hello World!" displayed on the first line of the LCD display.
CIRCUIT DIAGRAM for interfacing with Arduino with DHT22 and 16 x2 LCD Display
In this circuit diagram you can see the connection are given below
Arduino Uno
|
DHT22 |
VCC |
5V
|
GROUND |
GND
|
PIN8 |
SIGNAL |
Arduino Uno
|
LCD |
VCC |
A,VDD,VO |
GROUND |
K,VSS,RW |
PIN2 |
D7 |
PIN3 |
D6 |
PIN4 |
D5 |
PIN5 |
D4 |
PIN11 |
E |
PIN12 |
RS |
Arduino Code
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <dht.h>
#define dataPin 8
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
dht DHT;
bool showcelciusorfarenheit = false;
void setup()
{
lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
}
void loop()
{
int readData = DHT.read22(dataPin);
float t = DHT.temperature;
float h = DHT.humidity;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("T:"); // Prints string "Temp." on the LCD
//Print temperature value in Celcius and Fahrenheit every alternate cycle
if(showcelciusorfarenheit)
{
lcd.print(t); // Prints the temperature value from the sensor
lcd.print(" ");
lcd.print((char)223);//shows degrees character
lcd.print("C");
showcelciusorfarenheit = false;
}
else
{
lcd.print((t * 9.0) / 5.0 + 32.0); // print the temperature in Fahrenheit
lcd.print(" ");
lcd.print((char)223);//shows degrees character
lcd.print("F");
showcelciusorfarenheit = true;
}
lcd.setCursor(0,1);
lcd.print("H:");
lcd.print(h);
lcd.print(" #");
lcd.print("QUARTZ COMPONENTS");
// Move the cloud to the right
for (int i = 0; i < 16; i++) {
lcd.scrollDisplayRight();
delay(100);
}
// Delay before the next animation
delay(2000 );
}
This code is an Arduino program written in C++ that interfaces with a DHT22 temperature and humidity sensor and a 16x2 LCD screen. The program reads the temperature and humidity values from the sensor and displays them on the LCD screen in alternating Celsius and Fahrenheit units. The LCD screen also displays a scrolling message.
Let's break down the code line by line:
#include <LiquidCrystal.h>
This line includes the LiquidCrystal library, which provides a simple interface for controlling LCD displays.
#include <dht.h>
This line includes the dht library, which provides a set of functions for reading the temperature and humidity values from a DHT22 sensor.
#define dataPin 8
This line defines the data pin for the DHT22 sensor.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
This line creates an object called "lcd" of the LiquidCrystal class, which is used to control the LCD display and The numbers in the parentheses help specify which digital pins of the Arduino are connected to the RS, enable, D4, D5, D6, and D7 pins of the LCD, respectively.
dht DHT;
This line creates an object called "DHT" of the dht class, which is used to read temperature and humidity values from the DHT22 sensor.
bool showcelciusorfarenheit = false;
This line defines a boolean variable called "showcelciusorfarenheit" and initializes it to false. This variable is used to alternate between displaying the temperature in Celsius and Fahrenheit units.
void setup()
This line starts the setup() function, which is called once when the Arduino is powered on or reset.
lcd.begin(16,2);
This line initializes the LCD display with 16 columns and 2 rows.
void loop()
This line starts the loop() function, which is called repeatedly after the setup() function.
int readData = DHT.read22(dataPin);
This line reads the temperature and humidity values from the DHT22 sensor and stores them in variables called "t" and "h".
float t = DHT.temperature;
This line gets the temperature value from the DHT22 sensor and stores it in a variable called "t".
float h = DHT.humidity;
This line gets the humidity value from the DHT22 sensor and stores it in a variable called "h".
lcd.setCursor(0,0);
This line sets the cursor position on the LCD display to the top left corner.
lcd.print("T:");
This line prints the string "T:" on the LCD display to indicate that the following value is the temperature.
if(showcelciusorfarenheit)
This line starts an if statement that checks whether the boolean variable "showcelciusorfarenheit" is true.
lcd.print(t);
If "showcelciusorfarenheit" is true, this line prints the temperature value in Celsius units on the LCD display.
lcd.print(" ");
This line prints a space on the LCD display.
lcd.print((char)223);
This line prints the degree symbol (°) on the LCD display.
lcd.print("C");
This line prints the "C" character on the LCD display to indicate Celsius units.