Control a DC fan with voice commands - variable speed control with PWM.
- ESP32 board
- DC fan (5V or 12V)
- MOSFET or motor driver (e.g., L293D, L298N, IRF520)
- External power supply matching fan voltage
- Flyback diode (1N4007 or similar)
ESP32 GPIO25 → MOSFET Gate
MOSFET Source → GND (common ground with ESP32)
MOSFET Drain → Fan Negative (-)
Fan Positive (+) → External Power Supply (+)
Power Supply (-) → GND (common ground)
Flyback Diode → Across fan terminals (cathode to +)
ESP32 GPIO25 → IN1 (PWM input)
ESP32 GND → GND
OUT1/OUT2 → Fan
12V/5V → External power supply
- Variable speed control 0-100%
- PWM-based motor control (25kHz)
- Speed adjustment (increase/decrease)
- Physical button for preset cycling
- Smooth speed transitions
- "Alexa, turn on the fan"
- "Alexa, turn off the fan"
- "Alexa, set fan speed to 75"
- "Alexa, set fan to maximum"
- "Alexa, increase fan speed"
- "Alexa, decrease fan speed"
cd examples/fan
idf.py build flash monitorEdit main/fan_example.c - update WiFi and SinricPro credentials.
For different GPIO or PWM settings, modify:
#define FAN_GPIO (25)
#define FAN_FREQUENCY (25000) /* 25 kHz */
#define BUTTON_GPIO (0)Press BOOT button to cycle through:
- OFF (0%)
- LOW (33%)
- MEDIUM (66%)
- HIGH (100%)
- MOSFET Selection: Use logic-level MOSFETs (IRF520, IRLZ44N) for 3.3V GPIO
- Flyback Diode: REQUIRED to protect against inductive kickback
- Common Ground: ESP32 and fan power supply must share common ground
- PWM Frequency: 25kHz is above audible range (prevents buzzing)
- Minimum Speed: Code sets minimum 20% duty for fan startup
- Never power high-current devices directly from ESP32 pins
- Always use appropriate motor driver or MOSFET
- Add flyback diode for inductive loads
- Use proper heat sinking for high-power applications