Temperature and Humidity Sensor Arduino Code with LCD
Abstract
This article provides a detailed guide on how to use an Arduino board along with a temperature and humidity sensor to display real-time data on an LCD screen. The article is structured as follows:
Introduction
An Arduino board is a popular microcontroller platform that allows you to create various electronic projects. One common application is to measure temperature and humidity using a sensor such as the DHT11 or DHT22, and display the readings on an LCD screen. This article will guide you through the process of setting up the hardware and writing the necessary code.
Sensor Setup
To begin, you will need an Arduino board and a temperature and humidity sensor. Connect the sensor to the Arduino by following the pinout diagram provided by the manufacturer. Typically, three pins are used: VCC (power), GND (ground), and DATA (data signal). Ensure that the sensor is properly connected and secured.
LCD Setup
Next, connect an LCD screen to the Arduino. You will need to use the liquid crystal library for this. Wire the LCD according to its pinout diagram, making sure to connect the correct pins to the Arduino. Additionally, adjust the contrast of the LCD using the built-in potentiometer, if necessary.
Arduino Code
Now it’s time to write the Arduino code. Begin by including the required libraries, such as the DHT library for the sensor and the liquid crystal library for the LCD. Initialize the sensor and LCD objects, specifying the appropriate pins. In the main loop, read the temperature and humidity values from the sensor and update the LCD display accordingly.
#include
#include
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
delay(2000);
}
Conclusion
In conclusion, by following the steps outlined in this article, you can easily set up an Arduino board with a temperature and humidity sensor to display real-time data on an LCD screen. The combination of hardware wiring and the provided Arduino code will allow you to accurately measure and monitor the surrounding temperature and humidity levels.