Advertisement
LeventeDaradici

DHT11 + DS18B20 + i2C 1602 LCD display

Apr 24th, 2023 (edited)
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | Source Code | 0 0
  1. // https://www.youtube.com/@LeventeDaradici/videos
  2. #include <DHT.h>
  3. #include <LCD_I2C.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7. #define DHTPIN 7
  8. #define ONE_WIRE_BUS 8
  9. #define DHTTYPE DHT11
  10. LCD_I2C lcd(0x27, 16, 2);
  11. OneWire oneWire(ONE_WIRE_BUS);
  12. DallasTemperature sensors(&oneWire);
  13.  
  14. boolean AmGasitDHT11, AmGasitDS18B20 = false;
  15.  
  16. DHT dht(DHTPIN, DHTTYPE);
  17.  
  18. void setup() {
  19.   Serial.begin(9600);
  20.   lcd.begin();
  21.   lcd.backlight();
  22.   Serial.println("Detectare senzor");
  23.   lcd.setCursor(0, 0);
  24.   lcd.print("DETECTARE DHT11");
  25.   delay(500);
  26.   lcd.setCursor(0, 1);
  27.   lcd.print("DETECTARE DS18B20");
  28.   delay(500);
  29.   dht.begin();
  30.   pinMode(13, OUTPUT);
  31.   sensors.begin();
  32. }
  33.  
  34. void loop()
  35.     {
  36.       delay(1000);
  37.       digitalWrite(13,LOW);
  38.       float h = dht.readHumidity();
  39.       float t = dht.readTemperature();
  40.       float f = dht.readTemperature(true);
  41.  
  42.       if (isnan(h) || isnan(t) || isnan(f))
  43.          {
  44.             Serial.println("Nu am gasit senzor DHT11!");
  45.             AmGasitDHT11 = false;
  46.             lcd.setCursor(0, 0);
  47.             lcd.print("                 ");
  48.             lcd.setCursor(2, 0);
  49.             lcd.print("LIPSA DHT11!");        
  50.          } else
  51.               {
  52.                  lcd.setCursor(0, 0);
  53.                  lcd.print("                 ");
  54.                  lcd.setCursor(0, 0);
  55.                  lcd.print("DHT11");
  56.                  AmGasitDHT11 = true;  
  57.                  float hif = dht.computeHeatIndex(f, h);
  58.                  float hic = dht.computeHeatIndex(t, h, false);
  59.                  Serial.print("DHT11 Temperatura: ");
  60.                  Serial.print(t,1);
  61.                  Serial.println(F("°C "));
  62.                  Serial.print("DHT11 Umiditate: ");
  63.                  Serial.print(h,0);
  64.                  Serial.println("%");
  65.                  lcd.setCursor(6, 0);
  66.                  lcd.print(t,1);
  67.                  lcd.setCursor(10,0);
  68.                  lcd.print((char)223);
  69.                  lcd.setCursor(11,0);
  70.                  lcd.print("C");
  71.                  lcd.setCursor(13, 0);
  72.                  lcd.print(h,0);
  73.                  lcd.setCursor(15,0);
  74.                  lcd.print("%");
  75.               }
  76.       delay(1000);
  77.       digitalWrite(13,HIGH);
  78.       sensors.requestTemperatures();
  79.              float tempC = sensors.getTempCByIndex(0);
  80.              if(tempC != DEVICE_DISCONNECTED_C)
  81.                 {
  82.                    Serial.print("DS18B20 Temperatura ");
  83.                    Serial.println(tempC);
  84.                    lcd.setCursor(0, 1);
  85.                    lcd.print("                 ");
  86.                    lcd.setCursor(0,1);
  87.                    lcd.print("DS18B20");
  88.                    lcd.setCursor(10,1);
  89.                    lcd.print(tempC,2);
  90.                    lcd.setCursor(14,1);
  91.                    lcd.print((char)223);
  92.                    lcd.setCursor(15,1);
  93.                    lcd.print("C");
  94.                 } else
  95.                      {
  96.                        Serial.println("Nu am gasit senzor DS18B20");
  97.                        lcd.setCursor(0,1);
  98.                        lcd.print("                 ");
  99.                        lcd.setCursor(1,1);
  100.                        lcd.print("LIPSA DS18B20");
  101.                      }            
  102.  
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement