Back to All Blog Posts
Tutorial β€’EltroNerd Engineering

A smart ceiling fan with speed control using ESP32

A smart ceiling fan with speed control using ESP32 is a solid student IoT project. It combines PWM (Pulse Width Modulation) for motor speed control with options like Wi-Fi, Bluetooth, or IR remote for user interaction.

Smart homes are not just about lights β€” even ceiling fans can be made intelligent.
In this project, we will build a Smart Ceiling Fan using the ESP32 microcontroller, where you can control the fan’s speed wirelessly from your phone.

The ESP32 will generate PWM signals to adjust the motor driver, which in turn controls the speed of the ceiling fan.


πŸ”Ή Key Idea: Use the ESP32’s PWM output to regulate fan speed, while Wi-Fi or Bluetooth provides remote control.


Step 1: Hardware Setup

  • ESP32 Development Board
  • Relay or TRIAC-based motor driver (for ceiling fan AC motor control)
  • Optional: MOSFET + DC Fan (for prototype)
  • Power supply (5V for ESP32)

Wiring:

  • Motor driver input β†’ ESP32 PWM pin (GPIO 23)
  • Driver power β†’ 5V or AC (depending on module type)
  • ESP32 β†’ USB/adapter power

Step 2: How It Works

  1. The ESP32 generates a PWM signal where duty cycle controls motor speed.
  2. The PWM signal is fed to a motor driver (or TRIAC dimmer for AC fans).
  3. Commands to change the speed are sent over Wi-Fi (Web App) or Bluetooth (Serial commands).
  4. The fan speed changes in real-time when you adjust values.

⚠️ Note: For safety, use a DC fan + MOSFET during testing. For AC ceiling fans, you must use a proper TRIAC driver circuit.

Step 3: Arduino Code Example (PWM Speed Control)

c
#include <WiFi.h>
#include <BluetoothSerial.h>

#define FAN_PIN 23   // PWM output pin

// Wi-Fi credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

WiFiServer server(80);
BluetoothSerial SerialBT;

int fanSpeed = 0; // 0–255 (PWM duty cycle)

void setup() {
  Serial.begin(115200);

  // Setup PWM channel
  ledcSetup(0, 5000, 8);   // Channel 0, 5kHz, 8-bit resolution
  ledcAttachPin(FAN_PIN, 0);

  // Wi-Fi setup
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWi-Fi connected!");
  Serial.println(WiFi.localIP());
  server.begin();

  // Bluetooth setup
  SerialBT.begin("ESP32_Fan");
  Serial.println("Bluetooth ready!");
}

void loop() {
  // --- Wi-Fi Control ---
  WiFiClient client = server.available();
  if (client) {
    String request = client.readStringUntil('\r');
    client.flush();

    if (request.indexOf("/speed/") != -1) {
      int value = request.substring(request.indexOf("/speed/") + 7).toInt();
      fanSpeed = constrain(value, 0, 255);
      ledcWrite(0, fanSpeed);
      Serial.printf("Wi-Fi Fan Speed: %d\n", fanSpeed);
    }

    // Web response
    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.println("<h1>ESP32 Fan Control</h1>");
    client.println("<form action='/speed/' method='GET'>");
    client.println("Set Speed (0–255): <input name='value' type='number'>");
    client.println("<input type='submit' value='Set'>");
    client.println("</form>");
    client.println();
    client.stop();
  }

  // --- Bluetooth Control ---
  if (SerialBT.available()) {
    int value = SerialBT.parseInt();
    if (value >= 0 && value <= 255) {
      fanSpeed = value;
      ledcWrite(0, fanSpeed);
      Serial.printf("Bluetooth Fan Speed: %d\n", fanSpeed);
    }
  }
}

##Step 4: Testing


Wi-Fi Mode: Connect your phone to the same Wi-Fi β†’ open ESP32 IP β†’ enter fan speed (0–255).

Bluetooth Mode: Pair with ESP32_Fan β†’ send numbers 0 (OFF) to 255 (Full Speed).

The fan will smoothly adjust speed in real time.

Why This Project is Useful


Works as a smart home upgrade without changing existing fans.

Students learn about PWM, motor control, Wi-Fi, Bluetooth in one project.

Can be expanded with temperature sensors for auto speed adjustment.

Hybrid control: offline + online.

Try this project in your browser

Compile code and simulate hardware output with EltroNerd Cloud IDE.

Launch Cloud IDE