Room Temperature Controlled Fan System using DHT22, OLED Display , Arduino nano & Single channel relay

 Circuit Diagram:





🌡️ Room Temperature Controlled Fan System

Smart Automation with DHT22 & OLED Display

An intelligent, automated cooling solution designed to maintain comfort by monitoring environmental conditions in real-time. This project uses a DHT22 precision sensor to track temperature and an OLED display for a professional user interface.

✨ Key Features

  • Precision Monitoring: High-accuracy tracking using the DHT22 digital sensor.
  • Smart Automation: Fan control triggers automatically at 30°C.
  • Visual Dashboard: Real-time status on a 128x64 I2C OLED screen.
  • Industrial Logic: Relay-isolated control for safe fan operation.

📟 Component Pinout

Component Pin Name Arduino Pin Description
DHT22 Sensor Data Pin 2 Signal Input
5V Relay IN Pin 3 Logic Trigger
I2C OLED SDA / SCL A4 / A5 I2C Data/Clock
Power VCC / GND 5V / GND Main Power

🚀 How It Works

The system samples the ambient temperature every 2 seconds. It follows a strict threshold logic:

  • Condition A: If Temperature > 30.0°C → Fan turns ON
  • Condition B: If Temperature ≤ 30.0°C → Fan turns OFF

📚 Required Libraries

DHT sensor library by Adafruit
Adafruit SSD1306
Adafruit GFX Library
Adafruit Unified Sensor
DIAGRAM.JSON - FAN SYSTEM CONFIG
{
  "version": 1,
  "author": "Computer work",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 62.4, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-dht22",
      "id": "dht1",
      "top": -86.1,
      "left": 52.2,
      "attrs": { "temperature": "28.5" }
    },
    { "type": "wokwi-relay-module", "id": "relay1", "top": 221, "left": 28.8, "attrs": {} },
    {
      "type": "board-ssd1306",
      "id": "oled1",
      "top": 51.14,
      "left": 211.43,
      "attrs": { "i2cAddress": "0x3c" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 198,
      "left": 272.6,
      "attrs": { "color": "limegreen" }
    },
    {
      "type": "wokwi-text",
      "id": "text1",
      "top": -115.2,
      "left": 38.4,
      "attrs": { "text": "Temp Sensor " }
    },
    {
      "type": "wokwi-text",
      "id": "text2",
      "top": 9.6,
      "left": 211.2,
      "attrs": { "text": "OLED Display" }
    },
    {
      "type": "wokwi-text",
      "id": "text3",
      "top": 182.4,
      "left": 240,
      "attrs": { "text": "Indication of Fan " }
    },
    {
      "type": "wokwi-text",
      "id": "text4",
      "top": 288,
      "left": 38.4,
      "attrs": { "text": "Relay for Switching" }
    }
  ],
  "connections": [
    [ "relay1:NO", "led1:A", "red", [ "h78", "v17.4", "h38.4" ] ],
    [ "led1:C", "nano:GND.1", "black", [ "v9.6", "h-9.2", "v-19.2", "h-48", "v-48", "h-48" ] ],
    [ "oled1:GND", "nano:GND.2", "black", [ "v-9.6", "h-124.8" ] ],
    [ "nano:3.3V", "relay1:COM", "red", [ "v86.4", "h144", "v38.4" ] ],
    [ "oled1:VCC", "nano:5V", "red", [ "v-19.2", "h-67.05", "v115.2", "h-67.2" ] ],
    [ "oled1:SDA", "nano:A4", "green", [ "v-9.6", "h48.07", "v115.2", "h-240", "v-38.4" ] ],
    [ "oled1:SCL", "nano:A5", "green", [ "v-19.2", "h67.5", "v134.4", "h-240" ] ],
    [ "dht1:VCC", "nano:5V", "red", [ "v19.2", "h-86.4", "v105.6", "h144" ] ],
    [ "dht1:GND", "nano:GND.2", "black", [ "v19.2", "h28.8" ] ],
    [ "dht1:NC", "nano:2", "green", [ "v28.8", "h28.9" ] ],
    [ "dht1:SDA", "nano:2", "green", [ "v28.8", "h38.5" ] ],
    [ "relay1:IN", "nano:3", "green", [ "h-67.2", "v-221", "h96", "v19.2", "h48" ] ],
    [ "relay1:VCC", "nano:5V", "red", [ "h-19.2", "v-86.4", "h115.2" ] ],
    [ "relay1:GND", "nano:GND.1", "black", [ "h-38.4", "v-67.6", "h153.6" ] ]
  ],
  "dependencies": {}
}
ARDUINO NANO CODE (HYSTERESIS LOGIC)
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Pin Definitions
#define DHTPIN 2
#define DHTTYPE DHT22
#define RELAY_PIN 3

// OLED Definitions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Start with fan off
  
  dht.begin();
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    for(;;); // Don't proceed if OLED fails
  }
  
  display.clearDisplay();
  display.setTextColor(WHITE);
}

void loop() {
  float t = dht.readTemperature();

  // Check if reading failed
  if (isnan(t)) {
    return; // Skip this loop iteration if the sensor fails
  }

  // Set up the display for the temperature reading
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(t);
  display.print(" C");

  // --- HYSTERESIS LOGIC ---
  const float turnOnTemp = 30.0;
  const float turnOffTemp = 28.0; 
  
  display.setCursor(0, 30);
  display.setTextSize(2);
  
  // 1. If it gets too hot, turn the fan ON
  if (t >= turnOnTemp) {
    digitalWrite(RELAY_PIN, HIGH); 
    display.print("FAN: ON");
  } 
  // 2. If it has cooled down enough, turn the fan OFF
  else if (t <= turnOffTemp) {
    digitalWrite(RELAY_PIN, LOW);  
    display.print("FAN: OFF");
  } 
  // 3. If the temperature is between 28.0 and 30.0
  else {
    if (digitalRead(RELAY_PIN) == HIGH) {
      display.print("FAN: ON");
    } else {
      display.print("FAN: OFF");
    }
  }

  display.display();
  delay(2000); // DHT22 needs ~2 seconds between readings
}

Comments