In this project, we will guide you through the process of showing the temperature on an LCD screen using an Arduino board, a DHT11 temperature and humidity sensor, and an I2C LCD module. The DHT11 sensor is a simple, low-cost sensor that can measure temperature and humidity, while the I2C module makes it easier to connect the LCD screen to the Arduino using just two data wires.
Components Required:
- Arduino Board (Arduino Uno, Nano, etc.)
- DHT11 Temperature and Humidity Sensor
- I2C LCD Display (16×2 LCD)
- Jumper Wires
- Breadboard (optional, for easy connections)
- Arduino IDE installed on your computer
Step-by-Step Guide
1. Wiring the Components
Before we begin the coding part, let’s first wire the components correctly. Here’s how to connect everything:
- DHT11 Sensor to Arduino:
- VCC pin of DHT11 to 5V on the Arduino.
- GND pin of DHT11 to GND on the Arduino.
- Data pin of DHT11 to Pin 12 on the Arduino.
- I2C LCD to Arduino:
- VCC pin of I2C LCD to 5V on the Arduino.
- GND pin of I2C LCD to GND on the Arduino.
- SDA pin of I2C LCD to A4 pin on the Arduino (for Arduino Uno).
- SCL pin of I2C LCD to A5 pin on the Arduino (for Arduino Uno).
Your connections should look like this:
DHT11
- VCC -> 5V
- GND -> GND
- DATA -> Pin 12
I2C LCD
- VCC -> 5V
- GND -> GND
- SDA -> A4 (for Arduino Uno)
- SCL -> A5 (for Arduino Uno)
2. Install Necessary Libraries
Before we can start coding, you need to install two libraries:
- DHT sensor library (to interface with the DHT11 sensor)
- LiquidCrystal_I2C library (to interface with the I2C LCD)
To install these libraries in Arduino IDE:
- Go to Sketch > Include Library > Manage Libraries.
- Search for DHT sensor library and install it.
- Search for LiquidCrystal_I2C and install it.
3. Arduino Code:
Once you have the necessary libraries installed, it’s time to write the code.
Here’s an example of the code you can use to read the temperature from the DHT11 sensor and display it on the I2C LCD:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Initialize DHT11 sensor`
#define DHTPIN 12 // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // Define the sensor type (DHT11)
// Create an instance of the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize the LCD with I2C address 0x27 (this can vary)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address, columns, rows
void setup() {
// Start the serial communication for debugging
Serial.begin(9600);
// Initialize the LCD
lcd.init(); // Use 'init()' instead of 'begin()'
lcd.backlight(); //Turns the LCD backlight ON,
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between readings
delay(2000);
// Reading temperature as Celsius
float temperature = dht.readTemperature();
// Check if the reading is successful
if (isnan(temperature)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor error");
return;
}
// Display temperature on the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Clear the LCD screen
lcd.clear();
// Display the temperature on the LCD
lcd.setCursor(0, 0); // First row
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
}
How the Code Works:
- Library Inclusions: We start by including the necessary libraries:
Wire.h
(for I2C communication),LiquidCrystal_I2C.h
(for controlling the I2C LCD), andDHT.h
(for reading the DHT11 sensor). - DHT11 Initialization: The DHT11 sensor is initialized with the pin number where it is connected (in this case, pin 2). The sensor type is defined as
DHT11
. - LCD Initialization: We initialize the LCD with the I2C address
0x27
(this is the most common address, but check your specific LCD if it’s different). The LCD is set to 16 columns and 2 rows. - Reading the Temperature: In the
loop()
function, we read the temperature using thedht.readTemperature()
function. If the reading is successful, the temperature is displayed on the serial monitor and the LCD. If there’s an error with the sensor, an error message is displayed on both the serial monitor and the LCD. - Displaying Data on the LCD: The LCD is cleared and the temperature value is printed on the first line of the LCD screen.
4. Testing the Setup
After uploading the code to your Arduino, the temperature should be displayed on the LCD screen, refreshing every two seconds. You can monitor the serial output to ensure that the temperature readings are correct.
Troubleshooting
- LCD not showing anything: Check if the LCD address
0x27
is correct. You can use an I2C scanner code to find the correct address of your LCD if needed. - DHT11 sensor failure: Make sure that your connections are secure and that the sensor is functioning correctly. If the reading is showing “NaN or Sensor error”, it typically means there’s an issue with the sensor or wiring.
Conclusion
This simple project shows you how to display temperature data on an LCD screen using an Arduino, DHT11 sensor, and I2C module. It’s a great starting point for creating weather stations, temperature monitoring systems, or just learning how to interact with sensors and displays in Arduino projects.