Four wire fan

Objective: control the rotational speed of a four wire DC fan

You will need: ESP-32 (any model) with USB lead, four-wire 12v DC fan and 12V supply, breadboard and patch cables. You could use 24V or 48V fans providing you have an appropriate power supply.

See here for more information on the types of fans available.


The DC brushless fans that are used in PCs, mid- to high-end routers and network equipment, servers, etc. use four wires. Two are for the power and ground, a third for the tacho signal that transmits pulses that can be used to calculate the rotational speed of the fan and a fourth wire for speed control using a PWM signal. These fans usually run on 12V, so a separate supply is required for this project, but the control can be achieved using a 3.3V PWM signal on an ESP-32. This website has information about the various cable colours used for 3- and 4-wire fans and their connections.


The ESP-32 has up to 16 independent PWM channels (depending upon the version) numbered 0 to 15. These can be assigned to any GPIO pin that is capable of supporting PWM. You should refer to the documentation that is specific to the version of ESP-32 that you have.


You set up the PWM parameters with a call to


Next, you assign the PWM channel to a GPIO pin:


To set the duty cycle for the PWM output use:


Exclamation mark The function names that start with ledc originate from the intention for the PWM functions to drive LEDs (short for LED Control) but of course PWM can be used to drive all sorts of devices; motors, sounders, etc.

The sketch sets up the PWM channel and assigns a GPIO pin. The loop() function starts with a duty cycle of zero and increments it to 255, waits for a few seconds and then decrements it to zero again.


  1. It's important to connect the ground (GND) pin of the ESP-32 to the negative terminal (0V) on the fan supply. If the two are not connected then PWM has no reference and the fan may not work properly, or at all.
  2. With no PWM connection to the fan, or an incorrect GPIO assignment or other error, the fan is likely to run at full speed. Presumably this is a fail-safe to protect the thing it's cooling rather than sit idle waiting for a PWM signal that may never arrive.


The Fritzing diagram is below. The green wire is the tacho output that can be ignored in this situation. The blue wire is the PWM input to the fan. The yellow wire is the positive supply to the fan - do not connect this to the ESP-32. The black wire is the 0V supply to the fan and must also be connected to the ESP-32's GND pin.

Fritzing diagram