Arduino - Temperatur- und Luftfeuchtigkeitssensor - OLED-Display

In diesem Tutorial lernen wir:

Arduino DHT11/DHT22 Temperatur- und Feuchtigkeitssensor mit OLED-Display

Erforderliche Hardware

1×Arduino Uno R3
1×USB 2.0 Kabel Typ A/B (für USB-A PC)
1×USB 2.0 Kabel Typ C/B (für USB-C PC)
1×SSD1306 I2C OLED-Display 128x64
1×SSD1306 I2C OLED-Display 128x32
1×DHT11 Temperatur- und Feuchtigkeitssensor
1×Verbindungskabel

You can use DHT22 sensor instead of DHT11 sensor.

1×(Empfohlen) Schraubklemmenblock-Shield für Arduino Uno
1×(Empfohlen) Breadboard-Shield für Arduino Uno
1×(Empfohlen) Gehäuse für Arduino Uno
1×(Empfohlen) Prototyping-Grundplatte & Breadboard-Kit für Arduino Uno

Oder Sie können die folgenden Kits kaufen:

1×DIYables STEM V3 Starter-Kit (Arduino enthalten)
1×DIYables Sensor-Kit (30 Sensoren/Displays)
1×DIYables Sensor-Kit (18 Sensoren/Displays)
Offenlegung: Einige der in diesem Abschnitt bereitgestellten Links sind Amazon-Affiliate-Links. Wir können eine Provision für Käufe erhalten, die über diese Links getätigt werden, ohne zusätzliche Kosten für Sie. Wir schätzen Ihre Unterstützung.

Über das OLED-Display, DHT11- und DHT22-Temperatur- und Feuchtigkeitssensor

Wenn Sie nichts über OLED-Displays sowie DHT11- und DHT22-Temperatur- und Feuchtigkeitssensoren wissen (Pinbelegung, Funktionsweise, Programmierung ...), lernen Sie in den folgenden Tutorials mehr darüber:

Verdrahtungsdiagramm

Arduino - DHT11-Modul LCD-Verkabelung

Arduino DHT11-Sensor OLED Verdrahtungsdiagramm

Dieses Bild wurde mit Fritzing erstellt. Klicken Sie, um das Bild zu vergrößern.

Arduino - DHT22-Modul LCD-Verkabelung

Arduino DHT22-Sensor OLED Verdrahtungsdiagramm

Dieses Bild wurde mit Fritzing erstellt. Klicken Sie, um das Bild zu vergrößern.

Arduino-Code - DHT11-Sensor - OLED

/* * Dieser Arduino Code wurde von newbiely.de entwickelt * Dieser Arduino Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino/arduino-temperature-humidity-sensor-oled-display */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define DHTPIN 2 // pin connected to DHT11 sensor #define DHTTYPE DHT11 Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHTPIN, DHTTYPE); String temperature; String humidity; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true) ; } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(3); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht.begin(); // initialize DHT11 the temperature and humidity sensor temperature.reserve(10); // to avoid fragmenting memory when using String humidity.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht.readHumidity(); // read humidity float tempC = dht.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(tempC)) { temperature = "Failed"; humidity = "Failed"; } else { temperature = String(tempC, 1); // one decimal places temperature += char(247); // degree character temperature += "C"; humidity = String(humi, 1); // one decimal places humidity += "%"; } Serial.print(tempC); // print to Serial Monitor Serial.print("°C | " ); // print to Serial Monitor Serial.print(humi); // print to Serial Monitor Serial.println("%"); // print to Serial Monitor oledDisplayCenter(temperature, humidity); // display temperature and humidity on OLED } void oledDisplayCenter(String temperature, String humidity) { int16_t x1; int16_t y1; uint16_t width_T; uint16_t height_T; uint16_t width_H; uint16_t height_H; oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T); oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_H, &height_H); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT / 2 - height_T - 5); oled.println(temperature); // text to display oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT / 2 + 5); oled.println(humidity); // text to display oled.display(); }

Schnelle Schritte

  • Öffne die Arduino IDE auf deinem PC.
  • Gehe zum Bibliotheken-Symbol in der linken Seitenleiste der Arduino IDE.
  • Suche „SSD1306“, dann finde die SSD1306-Bibliothek von Adafruit.
  • Klicke auf die Schaltfläche Installieren, um die Bibliothek zu installieren.
Arduino OLED-Bibliothek
  • Sie werden aufgefordert, einige weitere Bibliotheksabhängigkeiten zu installieren.
  • Klicken Sie auf den Button Install All, um alle Bibliotheksabhängigkeiten zu installieren.
Arduino Adafruit GFX Sensorbibliothek
  • Suchen Sie nach “DHT”, und finden Sie dann die DHT-Sensor-Bibliothek von Adafruit
  • Klicken Sie auf die Schaltfläche Install, um die Bibliothek zu installieren.
Arduino DHT-Sensor-Bibliothek
  • Sie werden aufgefordert, einige weitere Bibliotheksabhängigkeiten zu installieren.
  • Klicken Sie auf die Schaltfläche Alle installieren, um alle Bibliotheksabhängigkeiten zu installieren.
Arduino Adafruit einheitliche Sensorbibliothek
  • Kopieren Sie den obigen Code und öffnen Sie ihn mit der Arduino-IDE
  • Klicken Sie in der Arduino-IDE auf die Hochladen-Schaltfläche, um den Code auf Arduino hochzuladen
  • Tauchen Sie den Sensor in heißes und kaltes Wasser ein, oder halten Sie den Sensor in der Hand
  • Sehen Sie das Ergebnis auf dem OLED-Display und im Serial Monitor

※ Notiz:

Der About-Code zentriert den Text automatisch horizontal und vertikal auf dem OLED-Display.

Arduino-Code - DHT22-Sensor - OLED

/* * Dieser Arduino Code wurde von newbiely.de entwickelt * Dieser Arduino Code wird der Öffentlichkeit ohne jegliche Einschränkung zur Verfügung gestellt. * Für vollständige Anleitungen und Schaltpläne besuchen Sie bitte: * https://newbiely.de/tutorials/arduino/arduino-temperature-humidity-sensor-oled-display */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define DHTPIN 2 // pin connected to DHT22 sensor #define DHTTYPE DHT22 Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHTPIN, DHTTYPE); String temperature; String humidity; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true) ; } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(3); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht.begin(); // initialize DHT22 the temperature and humidity sensor temperature.reserve(10); // to avoid fragmenting memory when using String humidity.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht.readHumidity(); // read humidity float tempC = dht.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(tempC)) { temperature = "Failed"; humidity = "Failed"; } else { temperature = String(tempC, 1); // one decimal places temperature += char(247); // degree character temperature += "C"; humidity = String(humi, 1); // one decimal places humidity += "%"; Serial.print(tempC); // print to Serial Monitor Serial.print("°C | " ); // print to Serial Monitor Serial.print(humi); // print to Serial Monitor Serial.println("%"); // print to Serial Monitor } oledDisplayCenter(temperature, humidity); // display temperature and humidity on OLED } void oledDisplayCenter(String temperature, String humidity) { int16_t x1; int16_t y1; uint16_t width_T; uint16_t height_T; uint16_t width_H; uint16_t height_H; oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T); oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_H, &height_H); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT / 2 - height_T - 5); oled.println(temperature); // text to display oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT / 2 + 5); oled.println(humidity); // text to display oled.display(); }

※ Notiz:

Der Code für DHT11 und DHT22 ist identisch, abgesehen von einer Codezeile. Die Bibliothek für DHT11 und DHT22 ist dieselbe.

Video Tutorial

Wir erwägen die Erstellung von Video-Tutorials. Wenn Sie Video-Tutorials für wichtig halten, abonnieren Sie bitte unseren YouTube-Kanal , um uns zu motivieren, die Videos zu erstellen.

※ UNSERE NACHRICHTEN

  • Sie können gerne den Link zu diesem Tutorial teilen. Bitte verwenden Sie jedoch unsere Inhalte nicht auf anderen Websites. Wir haben viel Mühe und Zeit in die Erstellung der Inhalte investiert, bitte respektieren Sie unsere Arbeit!