About Lesson
Required Libraries:
Adafruit_SSD1306
– For OLED displayAdafruit_GFX
– Required byAdafruit_SSD1306
DHT
– For DHT22 sensor
Make sure you install these libraries via the Arduino Library Manager..
C++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // We don't use reset pin on most OLEDs
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT22 settings
#define DHTPIN 4 // GPIO pin for DHT22
#define DHTTYPE DHT22 // DHT22 sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Initialize DHT22
dht.begin();
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Clear display buffer
display.clearDisplay();
// Display static text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("DHT22 Sensor Data");
display.display(); // Update the display
delay(2000); // Wait for 2 seconds to show the text
}
void loop() {
// Read temperature and humidity from DHT22
float humidity = dht.readHumidity();
// Chose either Celsius or Fahrenheit and uncomment the line
// float temperature = dht.readTemperature(); // Celsius
// float temperature = dht.readTemperature(true); // Fahrenheit
// Check if readings are valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Clear the display before showing new data
display.clearDisplay();
// Display temperature
display.setCursor(0, 10);
display.setTextSize(2);
display.println("Temp:");
display.setTextSize(2);
display.setCursor(0, 30);
display.print(temperature);
display.println(" F");
// Display humidity
display.setCursor(0, 50);
display.setTextSize(2);
display.print("Hum:");
display.setCursor(0, 70);
display.print(humidity);
display.println(" %");
// Update the display with new values
display.display();
// Print the same data to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Wait a few seconds between measurements
delay(2000);
}
Explanation:
- OLED Initialization: The OLED display is initialized using the
Adafruit_SSD1306
library. The display resolution is set to 128×64 pixels, and the I2C address is set to0x3C
(common for many OLED displays). - DHT22 Initialization: The DHT22 sensor is initialized using the
DHT
library. The data pin is connected to GPIO4 of the ESP32. - Main Loop:
- The temperature and humidity are read from the DHT22 sensor.
- The OLED display is cleared, and the new temperature and humidity values are printed on the screen.
- The same values are printed to the Serial Monitor for debugging or logging.
This code will update the temperature and humidity readings on the OLED display every 2 seconds. Make sure to adjust the pin numbers based on your setup.