#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h" // Example using DHT22 sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Define the OLED display address (typically 0x3C for 128x64)
#define OLED_ADDR 0x3C
// Define the reset pin (or -1 if not used)
#define OLED_RESET -1
// Define the pins for ESP32 (SDA and SCL)
#define SDA_PIN 21
#define SCL_PIN 18
// Create an instance of the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT sensor setup
const int DHT_PIN = 15; // Pin where your DHT sensor is connected
DHTesp dht;
void setup() {
Serial.begin(115200);
// Initialize I2C communication and set pins
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize the OLED display with the I2C address
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Initialize the DHT sensor
dht.setup(DHT_PIN, DHTesp::DHT22); // Change to DHT11 if you're using DHT11
// Clear the display buffer.
display.clearDisplay();
// Set text color to white
display.setTextColor(SSD1306_WHITE);
// Set text size and cursor position
display.setTextSize(1);
display.setCursor(10, 20);
// Display initial message
display.println("Temperature:");
// Display the content on the screen
display.display();
// Delay to keep the message on the screen
delay(2000);
}
void loop() {
// Read temperature from DHT sensor
float temperature = dht.getTemperature();
// Clear the previous temperature value
display.fillRect(0, 30, SCREEN_WIDTH, 10, SSD1306_BLACK); // Clear previous temperature
// Update temperature value
display.setCursor(10, 30);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
// Display the updated content on the screen
display.display();
// Delay before updating again
delay(2000);
}