PWM with ESP32 and a transistor

I would like to use my ESP32 to controll a PWM PC Fan. To do that I need a 5V PWM signal. But the ESP32 only has 3,3V on its output. So I was thinking using a transistor could help me out.

But I am not sure if I just can use one transistor or do I need a second transistor or an optocoupler? (Because of the voltage difference and the voltage drop of 0,7V on the transistor?)
This would be how I imagine that it could look like:
image

The secound question that rised was how do I calcualte the resistors when I do not know the current of the PWM signal?

I also rode that often a mosfet is used, does it offer a big advantage over a transistor in this use case?

And how do you make it right like a professional, to do it in the future the right way too?

And would I have an advantage using a circuit like this which can turn faster off? The diode d1 is for example a BAT85.
Transistor als Schalter mit Diode

Circuit A1 should do what is needed.

Change 12v to 5v; for a PWM fan, the diode will not be required; resistor R30 can be increased in value too, 470R to 1k will probably work for a transistor like 2N2222.

Note it does invert the PWM signal but that will not matter in this case.


A logic level MOSFET should work too, circuit C1.

1 Like

Another option.

image

Do you know that for a fact (having tested it with 3.3V and found it did not work) or is it just an assumption based on something you've read?

I would begin by trying a direct connection!

Already tried and nothing did happen, while searching for the problem I found alot of topics on it saying that you need 5V.

Do you mean the fan did not spin at all, or that the fan spun at full speed and you were unable to reduce the speed with the PWM signal?

In my experience, with this type of fan, if the PWM pin is not connected, the fan spins at maximum speed.

I think that looks like what I need. But why do I not need the second resistor? And how do I calculate the resistor the right way or can you just "see" that? (I got stuck with calculating as it required to know the current of the PWM signal for the resistor)

Can you please explain why inverting does not matter? (Maybe because I can invert it I my code or another reason?)

When I use a PNP transistor does that give me the normal PWM signal?

On your second post you show a circuit with 2 resistors. Can you explain to me how the resistors are calculated for the PWM signal.

I would like to understand it myself so I can use that knowlage on other circuits as well.

Thank your for your answer ^^ I hope you can answer my questions.

I could not control it. It just spinned at maximum speed.
I used different test projectes to test it e.g. ESP32 Web Server with Slider: Control LED Brightness (PWM) | Random Nerd Tutorials
This was with and led and not with a fan but I also used some other project to test it but they used a level shifter for the signal and they did not work for me.

With the fan connected to 12V, but the PWM pin disconnected, what voltage do you measure on the PWM pin?

Sorry I can not measure that at the moment I am at work currently.

Do you mean to measure the voltage between the PWM Pin and GND with the multimeter?

Yes.

I am concerned that it might measure 12V and could damage the Arduino.

When I tested that I only had the around 3,2 volts if i remember right. (I just tested to set the output to a constant output with no PWM to test if there is an signal. But on that test I also used and 5V Powersupply with a 12V Step-up converter for the Fans)
Don´t know if that changes something (They still had the same ground)

I would recommend trying @LarryD 's suggestion from post #3.

It will be a matter of experimenting. We do not know what the circuit inside the fan is, and they are probably not all the same. Different fan manufacturers probably use different circuits, for example.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

Hi All, apologies, I was looking at this closed thread from last year:

PWM with ESP32 and a transistor

My particular interest was @PaulRB 's comment about measuring V on the PWM pin and concerns about +12v

Reason being, I've just cooked 3 ESP32 devices and I think this may be the reason. From the thread, I'm using a similar circuit to what @LarryD described as C1 - i.e. with a MOSFET (N channel).

Whilst this isn't Arduino specific (sorry), I'm not sure what I'm doing wrong to get +12v on the PWM pin from the fan (Arctic 80mm PWM).

Appreciate any thoughts and guidance - thanks very much in advance,

Anthony..

I have re-opened the topic and added your new post onto it

1 Like

Awesome - thanks very much @UKHeliBob

My concern there was that if the fan circuit had a internal pull-up to 12V on the PWM pin, and that was directly connected to an ESP32 pin, damage could occur.

But if you are using the MOSFET circuit you described, that would protect the ESP32 from damage, if correctly connected.

So I think something else may have cooked your ESP32.

Post your schematic. Hand drawn is fine. Also maybe some close-up, bright, clear photos showing all the connections.

Thanks - will sort that out in the next couple of hours hopefully...