Sorting by


ESP8266 with PWM to control a computer fan


Use of an ESP8266 to PWM control a 12Vdc, 2-wire computer fan, with a MOSFET (first with Tasmota, then a change to using ESPHome)


I wanted a simple speed control of a computer fan (or multiple fans) for temp control of a data cabinet. Previously, I had fans just switching on and off, and they were pretty noisy and gave little control of temp.

I had a ACW-1A board from PMB Electronics, which has a carrier board for a D1 mini, and includes a MOSFET amongst its I/O components. 

My plan was to be lazy and use Tasmota, as it is an easy install with support for lots of I/O control, including PWM (rather than code something with Arduino etc)

The board includes (among other things) the Mosfet at GPIO16 and a relay at GPIO15

Tasmota Setup

With Tasmota set up and the I/O assigned, the commands can be used as below. Assume the relay is assigned to “Relay 1” and mosfet to “PWM 2”

Tasmota does confuse things a bit with lots of systems added over the years, and lighting control is quite tied up with PWM. I’m sure there are options to decouple these entirely, but I found that you only need two commands to do the control (for PWM)

Set the PWM channel 2 to 0, which is “Basic PWM” (no lighting colour/dimmer commands). Then on your configuration dropown, select PWM2 for GPIO16.

pwm2 0

Then set the PWM frequency from 40 to 4,000Hz (note you can set an ESP32 from 40 to 50,000Hz)

pwmFrequency 4000

Tasmota was ok, but the fan was really noisy. The PWM frequency wasn’t really high enough. Some research showed that the developers of Tasmota have left the max value at 4,000Hz on the ESP8266 so there aren’t interruptions to other critical Tasmota functions such as WiFi etc.

My options to improve the speed were

  • Recompile Tasmota with a higher max frequency for PWM (not hard, but may have caused other issues)
  • Use an ESP32 (I would have had to spend another couple of $… and didn’t have one on hand)
  • Write some code (doable, but that isn’t the lazy way out and doesn’t give you the features such as MQTT/HA interfacing simply)
  • Use ESPHome or another project for the ESP8266. I hadn’t really used ESPHome, but it was a good time to do so and something I’d been meaning to look at for a while. This was the option I went with.

ESPHome Setup

This is the ESPHome YAML I used for the controller (MOSFET on GPIO16). It gives a web interface so you can see what is going on if you like, Home assistant support (with an option to ‘control the fan from 0-500 steps’) and sets PWM at 25,000Hz which was much better in terms of noise.

  name: netcupfan2

  board: esp01_1m

# Enable logging

# Enable Home Assistant API

  password: "123456789123456789123456789"

  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Netcupfan2 Fallback Hotspot"
    password: "sample_password"


  - platform: gpio
    name: "Relay_Main"
    pin: GPIO15
# Fan control in HA
  - platform: speed
    output: pwm_output
    name: "Net Cupboard Fan 2 Speed"
    speed_count: 500
 # PWM Fan Output
  - platform: esp8266_pwm
    pin: GPIO16
    frequency: 25000 Hz
    id: pwm_output
EspEasy implementation as a 3rd alternative
A useful board with a MOSFET

Leave a Reply

Your email address will not be published.

Post comment