Arduino Project

Smart Fan3.0

Course Introduction

In this lesson, you’ll create a temperature-controlled fan system using a DHT11 sensor, a DC motor driver, an I2C LCD, and traffic-light LEDs with the Arduino UNO R4.

The fan speed changes automatically across four levels as the temperature rises, while the LEDs indicate the current level (green, yellow, red) and the LCD displays the temperature and fan status in real time.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima/Arduino UNO R4 WIFI

1

2

USB Cable

1

×

3

Breadboard

1

BUY

4

Wires

Several

5

TA6586 - Motor Driver Chip

1

×

6

Humiture Sensor Module

1

×

7

Type Miniature DC Motor

1

×

8

Power Supply

1

9

I2C LCD 1602

1

10

Traffic Light LED

1

Wiring

2.png__PID:d3a1b040-75a4-405f-9e45-c0bed2d141ed

Common Connections:

DHT11 Humiture Sensor Module
DATA:
Connect to 2 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

TA6586 - Motor Driver Chip
BI:
Connect to 10 on the Arduino.
FI: Connect to 9 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

DC Motor
Connect to B0, F0 on the TA6586 - Motor Driver Chip.

I2C LCD 1602
SDA:
Connect to A4 on the Arduino.
SCL: Connect to A5 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

Traffic light LED
R:
Connect to 7 on the Arduino.
Y: Connect to 6 on the Arduino.
G: Connect to 5 on the Arduino.
GND: Connect to GND on the Arduino.

Writing the Code

Note
You can copy this code into Arduino IDE.
To install the library, use the Arduino Library Manager and search for LiquidCrystal I2C and install it.
Don’t forget to select the board(Arduino UNO R4 WiFi) and the correct port before clicking the Upload button.


#include 
#include 
#include 
#include 

/*
Smart Fan Controller (UNO R4 + DHT11 + TA6586 + I2C LCD + Traffic Light LEDs)

Fan Levels (4 levels total):
    - OFF:     T < 28°C           -> Fan OFF, traffic light OFF
    - Level 1: 28°C <= T < 30°C   -> Fan slow,  GREEN ON
    - Level 2: 30°C <= T < 35°C   -> Fan medium, YELLOW ON
    - Level 3: T >= 35°C          -> Fan fast,  RED ON

LCD shows:
    Line 1: Temperature
    Line 2: Fan Level (OFF / Level 1 / Level 2 / Level 3)
*/

// ---------- Pin definitions ----------
#define DHT_PIN   2        // DHT11 data pin
#define DHT_TYPE  DHT11

// TA6586 inputs (typical H-bridge style control)
// We use one PWM pin to control speed and one direction pin fixed LOW for forward.
#define TA_IN1_PWM  9       // Must be PWM-capable pin
#define TA_IN2_DIR  10      // Direction pin (kept LOW for forward)

// ---------- Traffic light LED pins ----------
#define LED_GREEN  5
#define LED_YELLOW 6
#define LED_RED    7

// ---------- LCD (I2C) ----------
LiquidCrystal_I2C lcd(0x27, 16, 2); // Common address 0x27 (sometimes 0x3F)

// ---------- DHT ----------
DHT dht(DHT_PIN, DHT_TYPE);

// ---------- Fan speed tuning ----------
const uint8_t PWM_LEVEL_1 = 160;   // slow
const uint8_t PWM_LEVEL_2 = 200;   // medium
const uint8_t PWM_LEVEL_3 = 255;   // fast

// ---------- Timing ----------
const unsigned long READ_INTERVAL_MS = 2000; // DHT11 should not be read too fast
unsigned long lastReadMs = 0;

// ---------- Fan state ----------
enum FanLevel { FAN_OFF = 0, FAN_L1 = 1, FAN_L2 = 2, FAN_L3 = 3 };
FanLevel fanLevel = FAN_OFF;

// ---------- Helper: set fan speed ----------
void fanStop() {
// Coast stop
analogWrite(TA_IN1_PWM, 0);
digitalWrite(TA_IN2_DIR, LOW);
}

void fanForward(uint8_t pwm) {
// Forward with PWM on IN1
digitalWrite(TA_IN2_DIR, LOW);
analogWrite(TA_IN1_PWM, pwm);
}

// ---------- Helper: traffic light ----------
void trafficOff() {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
}

void trafficShow(FanLevel lv) {
// Only light up when Level 1~3; OFF -> all off
trafficOff();
if (lv == FAN_L1) digitalWrite(LED_GREEN, HIGH);
else if (lv == FAN_L2) digitalWrite(LED_YELLOW, HIGH);
else if (lv == FAN_L3) digitalWrite(LED_RED, HIGH);
}

// ---------- Helper: update LCD ----------
void lcdPrintCentered(uint8_t row, const String &text) {
String s = text;
if (s.length() > 16) s = s.substring(0, 16);

int pad = (16 - (int)s.length()) / 2;
String line = "";
for (int i = 0; i < pad; i++) line += " ";
line += s;
while (line.length() < 16) line += " ";

lcd.setCursor(0, row);
lcd.print(line);
}

String levelToText(FanLevel lv) {
switch (lv) {
    case FAN_OFF: return "Fan: OFF";
    case FAN_L1:  return "Fan: Level 1";
    case FAN_L2:  return "Fan: Level 2";
    case FAN_L3:  return "Fan: Level 3";
    default:      return "Fan: OFF";
}
}

// ---------- Setup ----------
void setup() {
pinMode(TA_IN1_PWM, OUTPUT);
pinMode(TA_IN2_DIR, OUTPUT);
fanStop();

pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
trafficOff();

Serial.begin(115200);

dht.begin();

Wire.begin();
lcd.init();
lcd.backlight();

lcdPrintCentered(0, "Smart Fan");
lcdPrintCentered(1, "Starting...");
delay(800);
}

// ---------- Main loop ----------
void loop() {
unsigned long now = millis();
if (now - lastReadMs < READ_INTERVAL_MS) return;
lastReadMs = now;

// Read temperature from DHT11
float t = dht.readTemperature(); // Celsius
if (isnan(t)) {
    // DHT read failed
    fanStop();
    fanLevel = FAN_OFF;
    trafficOff();

    lcdPrintCentered(0, "Temp: --.- C");
    lcdPrintCentered(1, "Fan: OFF");
    Serial.println("DHT11 read failed!");
    return;
}

// Decide fan level by temperature
FanLevel newLevel;
if (t < 28.0f) {
    newLevel = FAN_OFF;
} else if (t < 30.0f) {
    newLevel = FAN_L1;
} else if (t < 35.0f) {
    newLevel = FAN_L2;
} else {
    // Includes 35~40 and 40+ (Level 3)
    newLevel = FAN_L3;
}

// Apply fan control
fanLevel = newLevel;
switch (fanLevel) {
    case FAN_OFF: fanStop(); break;
    case FAN_L1:  fanForward(PWM_LEVEL_1); break;
    case FAN_L2:  fanForward(PWM_LEVEL_2); break;
    case FAN_L3:  fanForward(PWM_LEVEL_3); break;
}

// Apply traffic light
trafficShow(fanLevel);

// Update LCD
char buf[17];
snprintf(buf, sizeof(buf), "Temp: %4.1f C", t);
lcdPrintCentered(0, String(buf));
lcdPrintCentered(1, levelToText(fanLevel));

// Debug
Serial.print("Temp(C): ");
Serial.print(t, 1);
Serial.print(" | ");
Serial.println(levelToText(fanLevel));
}