Temperature & Humidity Sensor With DHT11 Sensor and OLED Display

by gurnoork in Circuits > Arduino

66 Views, 1 Favorites, 0 Comments

Temperature & Humidity Sensor With DHT11 Sensor and OLED Display

VideoCapture_20250122-121529.jpg

This project showcases the steps to build a temperature and humidity monitoring system using a DHT sensor, Arduino Uno, and an OLED display. It displays temperature in Celsius and Fahrenheit as well as the humidity percentage. Additionally, an LED is connected to the circuit which turns on when the humidity is below or equal to a certain percentage and turns off when it is above or equal to another percentage number. Practically, this LED plays the role of an automated sprinkler system for irrigation, which turns on when the humidity is too low and turns off when the soil is well moist.

Supplies

Arduino Uno

Breadboard

DHT sensor

OLED (128x64)

LED

Resistor (220 Ω)

Wires

Assemble the Circuit

  1. Connect the DHT11 Sensor:
  2. Connect the VCC pin to the 5V pin on the Arduino.
  3. Connect the GND pin to the GND pin on the Arduino.
  4. Connect the middle pin to digital pin7 on the Arduino.
  5. Connect the OLED:
  6. Connect SDA to SDA on the Arduino..
  7. Connect SCL to SCL on the Arduino.
  8. Connect the respective VCC & GND pins to the 5V & GND pins on the Arduino.
  9. Connect the LED:
  10. Attach one leg of the LED to a 220KΩ resistor and connect the resistor to pin 9.
  11. Connect the other leg of the LED to GND.

Upload the Code

Install the following libraries in Arduino IDE:

  1. Adafruit_Sensor
  2. Adafruit_GFX
  3. Adafruit_SSD1306
  4. DHT


Write or paste the code into the Arduino IDE.


Code -

#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <SPI.h>
#include <Wire.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHT11_PIN 7
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);

DHT dht11(DHT11_PIN, DHT11);
int led = 9;
void setup() {
Serial.begin(9600);
dht11.begin();
if (!display.begin(OLED_ADDR, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
}

void loop() {
delay(500);
float humi = dht11.readHumidity();
float tempC = dht11.readTemperature();
float tempF = dht11.readTemperature(true);

if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
Serial.println("Failed to read from DHT11 sensor!");
}
else {
Serial.print("DHT11# Humidity: ");
Serial.print(humi);
Serial.print("%");

Serial.print(" | ");

Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");

display.clearDisplay();
display.setTextSize(1); // Text size multiplier (1 is the smallest)
display.setTextColor(SSD1306_WHITE); // White text
display.setCursor(0, 0); // Set cursor position (x, y)
display.print(F("TempC: "));
display.setTextSize(2);
display.print(tempC);
display.print("C");
display.setTextSize(1); // Text size multiplier (1 is the smallest)
display.setTextColor(SSD1306_WHITE); // White text
display.setCursor(0, 23); // Set cursor position (x, y)
display.print(F("TempF: "));
display.setTextSize(2);
display.print(tempF);
display.print("F");
display.setTextSize(1); // Text size multiplier (1 is the smallest)
display.setTextColor(SSD1306_WHITE); // White text
display.setCursor(0, 46); // Set cursor position (x, y)
display.print(F("Humi: "));
display.setTextSize(2);
display.print(humi);
display.print("%");
display.display();

if (humi <= 65){
digitalWrite(led, HIGH);
}
else{
if(humi >= 75){
digitalWrite(led, LOW);
}
}
}
}


Test the Device

Test the setup with different humidity conditions and adjust thresholds for LED activation if needed.