⭐⭐⭐ Challenge ⏱️ 30 minutes

Smart fan

You build a thermostat. Does it get warmer than 25 degrees? Then the relay clicks on. This way your Arduino can turn on a small fan on a battery, for example. On the screen you see the temperature and whether the fan is on.

First you log in for a moment. Then your project is ready right away.

🧰 What do you need?

🗺️ Wiring diagram

Follow the wires from the Arduino to each part. You can choose the wire colors yourself.

From To
Arduino UNO · D2 Temperature sensor DHT11 · DAT
Arduino UNO · 5V Temperature sensor DHT11 · VCC
Arduino UNO · GND Temperature sensor DHT11 · GND
Arduino UNO · D7 Relay 5V · S
Arduino UNO · 5V Relay 5V · +
Arduino UNO · GND Relay 5V · -
Arduino UNO · A4 OLED screen 0.91" · SDA
Arduino UNO · A5 OLED screen 0.91" · SCL
Arduino UNO · 5V OLED screen 0.91" · VCC
Arduino UNO · GND OLED screen 0.91" · GND

🛠️ This is how you build it

  1. Connect the DHT11: DAT to pin 2, VCC to 5V, and GND to GND.
  2. Connect the relay: S to pin 7, + to 5V, and − to GND.
  3. Connect the OLED screen: SDA to A4, SCL to A5, VCC to 5V, and GND to GND.
  4. Do you really want to switch something? Then only use a battery or low voltage on COM and NO, never the wall outlet.

💻 The code

// Smart fan (thermostat)
// Is it getting too warm? Then the relay turns on, for example for a fan.
// On the screen you see the temperature and whether the fan is on.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>

const int DHT_PIN = 2;        // DAT of the DHT11 on pin 2
const int RELAY_PIN = 7;      // S of the relay on pin 7
const float ON_ABOVE = 25.0;  // Above this temperature the fan turns on
const float OFF_BELOW = 24.0; // Below this temperature it turns off again

DHT dht(DHT_PIN, DHT11);
Adafruit_SSD1306 display(128, 32, &Wire, -1);  // OLED: SDA = A4, SCL = A5

bool fanOn = false;

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);   // Start with the relay off
  dht.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextColor(SSD1306_WHITE);
}

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

  if (!isnan(temperature)) {
    // Two limits, so the relay does not keep clicking on and off
    if (temperature > ON_ABOVE) fanOn = true;
    if (temperature < OFF_BELOW) fanOn = false;
    digitalWrite(RELAY_PIN, fanOn ? HIGH : LOW);
  }

  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0, 0);
  if (isnan(temperature)) {
    display.print(F("DHT11 ?"));
  } else {
    display.print(temperature, 1);
    display.print((char)247);
    display.print(F("C"));
  }
  display.setTextSize(1);
  display.setCursor(0, 22);
  display.print(fanOn ? F("Fan: ON") : F("Fan: off"));
  display.display();

  Serial.print(F("Temperature: "));
  Serial.print(temperature);
  Serial.println(fanOn ? F(" C  -> ON") : F(" C  -> off"));

  delay(2000);
}

🧠 What does the code do?

  • The DHT11 measures the temperature. Above 25 degrees we turn the relay on.
  • Only below 24 degrees does the relay turn off again. With those two limits, it does not keep clicking on and off.
  • The OLED screen shows the temperature and "Fan: ON" or "off".

🎯 Try this…

  • Add two buttons to set the limit temperature higher and lower
  • Turn on an LED while the fan is on
  • Let the fan also turn on when the humidity goes above 70%

🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/thermostat and click MAKE THIS. Then the code and the wiring diagram are ready right away.

First you log in for a moment. Then your project is ready right away.
⬇️ Download this page as a PDF 📚 Download the complete tutorial (PDF)

Codey Online is a product of OTRONIC

Build your Arduino or ESP32 project with AI

Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.

Start for free