Arduino Nano - Temperatur- und Luftfeuchtigkeitssensor - OLED

Dieses Tutorial erklärt Ihnen, wie Sie die Temperatur und Luftfeuchtigkeit von einem DHT11/DHT22-Sensor auslesen und sie auf einem OLED-Display anzeigen.

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

Erforderliche Hardware

1×Official Arduino Nano
1×Alternativ: DIYables ATMEGA328P Nano Development Board
1×USB-A-zu-Mini-B-Kabel
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) Schraubklemmen-Erweiterungsboard für Arduino Nano
1×(Empfohlen) Breakout-Erweiterungsboard für Arduino Nano
1×(Empfohlen) Stromverteiler für Arduino Nano

Oder Sie können die folgenden Kits kaufen:

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 OLED-Display, DHT11- und DHT22-Temperatur- und Feuchtigkeitssensor

Wenn Sie mit dem OLED-Display, dem DHT11- und DHT22-Temperatur- und Feuchtigkeitssensor (Pinbelegung, Funktionalität, Programmierung …) nicht vertraut sind, können Ihnen die folgenden Tutorials helfen:

Verdrahtungsdiagramm

Arduino Nano - DHT11-Modul LCD-Verkabelung

Arduino Nano DHT11 Sensor OLED Verdrahtungsdiagramm

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

Arduino Nano - DHT22-Modul LCD-Verkabelung

Arduino Nano DHT22-Sensor OLED Verdrahtungsdiagramm

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

Siehe Der beste Weg, den Arduino Nano und andere Komponenten mit Strom zu versorgen.

Arduino Nano Code - DHT11 Sensor - OLED

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano 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-nano/arduino-nano-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // The Arduino Nano pin connected to DHT11 sensor #define DHT_TYPE DHT11 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); 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

  • Klicken Sie auf das Bibliotheken-Symbol in der linken Leiste der Arduino-IDE.
  • Suchen Sie nach „SSD1306“ und finden Sie dann die SSD1306-Bibliothek von Adafruit.
  • Drücken Sie auf die Installieren-Schaltfläche, um die Bibliothek zu installieren.
Arduino Nano OLED-Bibliothek
  • Sie werden aufgefordert, weitere Bibliotheksabhängigkeiten zu installieren.
  • Drücken Sie die Schaltfläche Alle installieren, um die Installation aller Bibliotheksabhängigkeiten abzuschließen.
Arduino Nano Adafruit GFX Sensor-Bibliothek
  • Suchen Sie nach „DHT“ und finden Sie die Adafruit DHT-Sensor-Bibliothek.
  • Klicken Sie auf die Schaltfläche Installieren, um die Bibliothek zu installieren.
Arduino Nano 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 Nano Adafruit Unified Sensor-Bibliothek
  • Kopieren Sie den Code oben und öffnen Sie ihn in der Arduino IDE.
  • Klicken Sie in der Arduino IDE auf die Hochladen-Schaltfläche, um den Code an den Arduino Nano zu senden.
  • Tauchen Sie den Sensor in heißes und kaltes Wasser oder halten Sie ihn in der Hand.
  • Überprüfen Sie das Ergebnis auf dem OLED-Display und dem seriellen Monitor.

※ Notiz:

Der betreffende Code zentriert den Text automatisch sowohl horizontal als auch vertikal auf einem OLED-Display.

Arduino Nano-Code – DHT22-Sensor – OLED

/* * Dieser Arduino Nano Code wurde von newbiely.de entwickelt * Dieser Arduino Nano 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-nano/arduino-nano-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // The Arduino Nano pin connected to DHT22 sensor #define DHT_TYPE DHT22 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); 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 derselbe, bis auf eine Zeile. Die für beide verwendete Bibliothek ist ebenfalls 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!