deckle studioIm Editor öffnen

Arduino Uno: Raumklima mit DHT22 und LCD

DHT22 misst, I²C-LCD zeigt an.

  • Arduino Uno
  • HD44780 LCD
  • DHT22
Im Simulator öffnenOhne Konto: Simulieren drücken, und es läuft.

So funktioniert es

Der DHT22 an D2 misst Temperatur und Luftfeuchte (Eindraht-Protokoll, DHT-Bibliothek von Adafruit), das 16×2-Text-LCD mit I²C-Adapter (PCF8574, 0x27) zeigt sie mit zwei selbst definierten Zeichen (Thermometer, Tropfen) an. Die Bibliothek wartet nach dem Einschalten 1 s, bis das LCD initialisiert wird – vorher zeigt es die typischen Blöcke in Zeile 1. Temperatur und Feuchte im Inspektor ändern, auch während die Simulation läuft.

Verdrahtung

Jede Zeile ist eine Verbindung (Netz) mit den Pins, die sie verbindet.

NetzPins
GNDA1 GNDDS1 GNDU2 GNDV1 −
5VA1 5VDS1 VCCU2 VCCV1 +
DHTA1 D2U2 DATA
SCLA1 A5/SCLDS1 SCL
SDAA1 A4/SDADS1 SDA

Bauteile

Firmware

sketch.ino · 27 Zeilen

// Room climate display: the DHT22 on D2 measures, the 16×2 LCD with I²C adapter (0x27, A4/A5) displays
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(2, DHT22);
byte thermo[8] = { 0b00100, 0b01010, 0b01010, 0b01110, 0b01110, 0b11111, 0b11111, 0b01110 };   // custom character (CGRAM)
byte drop[8]   = { 0b00100, 0b00100, 0b01010, 0b01010, 0b10001, 0b10001, 0b01110, 0b00000 };

void setup() {
  Serial.begin(115200);
  lcd.init();                 // waits 1 s after power-on (library)
  lcd.backlight();
  lcd.createChar(0, thermo);
  lcd.createChar(1, drop);
  dht.begin();
}

void loop() {
  float t = dht.readTemperature(), h = dht.readHumidity();
  if (isnan(t) || isnan(h)) { lcd.clear(); lcd.print(F("Sensor error")); delay(2000); return; }
  lcd.setCursor(0, 0); lcd.write(0); lcd.print(F(" Temp: ")); lcd.print(t, 1); lcd.print((char)223); lcd.print(F("C  "));
  lcd.setCursor(0, 1); lcd.write(1); lcd.print(F(" Humidity: ")); lcd.print(h, 0); lcd.print(F(" %  "));
  Serial.print(t, 1); Serial.print(F(" C  ")); Serial.print(h, 1); Serial.println(F(" %"));
  delay(2000);                // the DHT22 measures at most every 2 s
}
Im Simulator öffnen

Weitere Beispiele: Arduino