Advertisement
LeventeDaradici

DHT11 temperature and humidity sensor with 1602 i2C LCD display

Jun 25th, 2021
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include "DHT.h"
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. #define DHTPIN 2
  6. #define DHTTYPE DHT11
  7.  
  8. DHT dht(DHTPIN, DHTTYPE);
  9. LiquidCrystal_I2C lcd(0x27, 16, 2);
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.   Serial.println(F("Senzor de temperatura si umiditate cu display 1602 cu interfata i2c"));
  14. lcd.init();
  15.  
  16.  
  17.     lcd.backlight();
  18.     lcd.clear();
  19.     lcd.setCursor(0,0);
  20.     lcd.print("Daradici Levente");
  21.   lcd.setCursor(2,1);
  22.     lcd.print("DHT11 Senzor");
  23.   delay(3000);
  24.   lcd.clear();
  25.   dht.begin();
  26. }
  27.  
  28. void loop() {
  29.   delay(3000);
  30.  
  31.   float h = dht.readHumidity();
  32.   // Read temperature as Celsius (the default)
  33.   float t = dht.readTemperature();
  34.   // Read temperature as Fahrenheit (isFahrenheit = true)
  35.   float f = dht.readTemperature(true);
  36.  
  37.   // Check if any reads failed and exit early (to try again).
  38.   if (isnan(h) || isnan(t) || isnan(f)) {
  39.     Serial.println(F("Failed to read from DHT sensor!"));
  40.     return;
  41.   }
  42.  
  43.   // Compute heat index in Fahrenheit (the default)
  44.   float hif = dht.computeHeatIndex(f, h);
  45.   // Compute heat index in Celsius (isFahreheit = false)
  46.   float hic = dht.computeHeatIndex(t, h, false);
  47.     lcd.clear();
  48.     lcd.setCursor(0,0);
  49.     lcd.print("Temp: ");
  50.   lcd.setCursor(9,0);
  51.     lcd.print(t);
  52.   lcd.setCursor(14,0);
  53.   lcd.print((char)223);
  54.   lcd.setCursor(15,0);
  55.   lcd.print("C");
  56.   lcd.setCursor(0,1);
  57.     lcd.print("Umid:");
  58.   lcd.setCursor(9,1);
  59.     lcd.print(h);
  60.   lcd.setCursor(15,1);
  61.     lcd.print("%");
  62.   Serial.print(F("Umiditate: "));
  63.   Serial.print(h);
  64.   Serial.print(F("%  Temperatura: "));
  65.   Serial.print(t);
  66.   Serial.println(F("°C "));
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement