Interfacing DHT11 with Arduino
Library used: DHT11 by Dhruba Saha
Example Code 1 (Serial Monitor):
#include <DHT11.h>
DHT11 dht11(7); // Connect DHT11 to pin 7 of Arduino UNO
int temperature=0;
int humidity=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println();
int result = dht11.readTemperatureHumidity(temperature, humidity);
if (result == 0) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(result));
}
delay(2000);
}
DHT11 dht11(7); // Connect DHT11 to pin 7 of Arduino UNO
int temperature=0;
int humidity=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println();
int result = dht11.readTemperatureHumidity(temperature, humidity);
if (result == 0) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(result));
}
delay(2000);
}
Working Video (LCD):
Example Code 2 (LCD):
#include <DHT11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
DHT11 dht11(7); // Connect DHT11 to pin 7 of Arduino UNO
LiquidCrystal_I2C lcd(0x27,16,2);
int temperature=0;
int humidity=0;
void setup()
{
lcd.init();
lcd.backlight();
}
void loop()
{
lcd.clear();
int result = dht11.readTemperatureHumidity(temperature, humidity);
if (result == 0) {
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(1000);
} else {
// Print error message based on the error code.
lcd.println(DHT11::getErrorString(result));
delay(2000);
}
}
No comments:
Post a Comment