This circuit integrates an Arduino UNO microcontroller with a 0.96" OLED display and a DHT11 temperature and humidity sensor. The Arduino UNO serves as the central processing unit, controlling the OLED display via I2C communication and reading environmental data from the DHT11 sensor. The OLED display shows the temperature and humidity readings obtained from the DHT11 sensor. The circuit is powered through the Arduino UNO, which provides the necessary voltage levels to the connected components.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
// OLED display width and height, in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // No reset pin, so -1
// Initialize SSD1306 display with the I2C address 0x3C (common for these displays)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT sensor configuration
#define DHTPIN 5 // Pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // DHT 11 sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Initialize the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Check for the OLED display at address 0x3C
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the display buffer
display.clearDisplay();
display.setTextSize(1); // Set text size to 1
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 0);
display.print("Initializing...");
display.display();
// Initialize the DHT sensor
dht.begin();
delay(2000); // Wait for the DHT sensor to stabilize
}
void loop() {
// Read humidity and temperature values
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any readings failed and exit early (to try again later)
if (isnan(humidity) || isnan(temperature)) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Failed to read from DHT sensor!");
display.display();
delay(2000); // Wait 2 seconds before retrying
return;
}
// Display temperature and humidity values on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.print(" C");
display.setCursor(0, 10);
display.print("Humidity: ");
display.print(humidity);
display.print(" %");
display.display();
// Wait before updating again
delay(2000);
}
This code initializes the OLED display and the DHT11 sensor, reads temperature and humidity data from the DHT11, and displays the readings on the OLED screen. If the sensor fails to provide valid readings, an error message is displayed. The display is updated every 2 seconds with the latest readings.