portfolio

Displaying Data to the OLED

Course Content
Session 1: Introduction to ESP32
Objective: Get familiar with the ESP32 microcontroller and set up the development environment.
0/5
Session 2: Interfacing with Sensors and Displays
Learn how to connect a DHT22 sensor and an OLED display to the ESP32.
0/4
Session 4: Programming the OLED Display
Objective: Write code to display information on the OLED display.
0/4
Session 5: Integrating Sensor Data with OLED Display
Objective: Combine sensor readings with OLED display functionality.
0/1
Session 6: Setup WIFI Capability
0/1
Session 7: Adding a Webserver
0/1
Session 8: Wrap-up
0/1
ESP32 Basics
About Lesson

Required Libraries:

  1. Adafruit_SSD1306 – For OLED display
  2. Adafruit_GFX – Required by Adafruit_SSD1306
  3. 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:

  1. 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 to 0x3C (common for many OLED displays).
  2. DHT22 Initialization: The DHT22 sensor is initialized using the DHT library. The data pin is connected to GPIO4 of the ESP32.
  3. 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.