Author Topic: Fan controller with PWM using STM32 chip  (Read 3603 times)

0 Members and 2 Guests are viewing this topic.

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Fan controller with PWM using STM32 chip
« on: May 06, 2021, 07:42:54 pm »
Hello.

I have already a circuit to make a fan controller, controlled by temperature. What I have is a thermistor sensing the temperature of a device and then, the stm32l031k6t6 I have, will read the temperature and will produce a PWM that will control the fan speed according to a few temperature values.

The supply I have is 12V, so I used an AMS1117 to step-down the voltage to 3.3V to feed the microcontroller, then I use a mosfet to drive the microcontroller output voltage back to 12V to feed the fan.

The circuit I have is the following:


I'm looking for improvement suggestions. These are the components I have, so please, try to stick to the components I have there. I'm looking to improve anything like resistor values, diodes, capacitors, etc.
I might have to change the pins that I used to connect the thermistor and the mosfet, but I'll change it to the correct pins later if I can't use the ones I'm using now!

Thank you for any help/suggestion
 

Offline gamalot

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: au
  • Correct my English
    • Youtube
Re: Fan controller with PWM using STM32 chip
« Reply #1 on: May 06, 2021, 07:53:12 pm »
The NRST pin should be connected to a 100nF capacitor instead of a 10K pull-down resistor.

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #2 on: May 06, 2021, 07:59:45 pm »
The NRST pin should be connected to a 100nF capacitor instead of a 10K pull-down resistor.

Allow me to ask why. Can I see that in the datasheet? I would like to read about the reasoning.
 

Offline gamalot

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: au
  • Correct my English
    • Youtube
Re: Fan controller with PWM using STM32 chip
« Reply #3 on: May 06, 2021, 08:10:32 pm »
The NRST pin should be connected to a 100nF capacitor instead of a 10K pull-down resistor.

Allow me to ask why. Can I see that in the datasheet? I would like to read about the reasoning.

Please read page 85 of the data sheet (DS10668).

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #4 on: May 06, 2021, 08:13:41 pm »
The NRST pin should be connected to a 100nF capacitor instead of a 10K pull-down resistor.

Allow me to ask why. Can I see that in the datasheet? I would like to read about the reasoning.

Please read page 85 of the data sheet (DS10668).

I see. I started looking the datasheet as soon as I read your reply and found it too in that same page.

 

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #5 on: May 06, 2021, 08:20:41 pm »
I'm still going to change a few things in the circuit, like connectors, push buttons, and anything else needed.
 

Offline rglory

  • Contributor
  • Posts: 15
  • Country: us
Re: Fan controller with PWM using STM32 chip
« Reply #6 on: May 08, 2021, 01:26:48 am »
I have one too  ;)
https://gitlab.com/rglory/tinyfan

Though it uses AVR and either digital DS18B20  or NTC 100K.
 
The following users thanked this post: psysc0rpi0n

Offline HB9EVI

  • Frequent Contributor
  • **
  • Posts: 586
  • Country: ch
Re: Fan controller with PWM using STM32 chip
« Reply #7 on: May 08, 2021, 11:39:52 am »
you cannot pwm a 2 or 3-pin fan, that's not going to work as you might expect; you need a 4-pin pwm capable fan
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 4137
  • Country: fi
Re: Fan controller with PWM using STM32 chip
« Reply #8 on: May 08, 2021, 01:14:08 pm »
you cannot pwm a 2 or 3-pin fan, that's not going to work as you might expect; you need a 4-pin pwm capable fan

Of course you can, that is being done all the time. Typical BLDC chassis fan is designed to be PWM'd on the low or high side.

Tacho output will be nonfunctional though.
 

Offline rglory

  • Contributor
  • Posts: 15
  • Country: us
Re: Fan controller with PWM using STM32 chip
« Reply #9 on: May 08, 2021, 02:44:21 pm »
you cannot pwm a 2 or 3-pin fan, that's not going to work as you might expect; you need a 4-pin pwm capable fan
Looks like you do not have a 3d printer...
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #10 on: May 08, 2021, 04:44:55 pm »
You can perfectly do so with a 3-pin fan.
The only issue will be the tachometer output not working correctly because you're turning the fan off.
You can also add diode and capacitor as filtering, so it works in voltage mode.
Usually they will start working at around 4V.
If the voltage is stable, the tachometer should be ok.
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #11 on: May 09, 2021, 03:36:43 pm »
I think I had a few things wrongly connected in the image above.

I changed the layout a little bit and I still have a question. This is how it looks like right now:


The question is if I'm going to connect VDDA to VDD. I'm not sure about this. In the image above, I have connected them together but I don't know if they are supposed to be connected that way.
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #12 on: May 09, 2021, 04:24:53 pm »
If the analog supply needs to be as clean as posible, better to add a small choke and/or low value resistor in series to filter the supply noises.
And put a ~10-22uF tantalum cap + ~1nF low esr ceramic cap.
If you don't mind some noise, just put a 47...100ohm resistor between vdd and vdda and the CAP close as possible to the vdda pin.
Check vdda current demand to ensure the voltage won't drop more than a few mV in the resistor.
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 

Offline gamalot

  • Frequent Contributor
  • **
  • Posts: 636
  • Country: au
  • Correct my English
    • Youtube
Re: Fan controller with PWM using STM32 chip
« Reply #13 on: May 09, 2021, 04:44:55 pm »
I think I had a few things wrongly connected in the image above.

I changed the layout a little bit and I still have a question. This is how it looks like right now:


The question is if I'm going to connect VDDA to VDD. I'm not sure about this. In the image above, I have connected them together but I don't know if they are supposed to be connected that way.

VDD and VDDA are shorted.

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #14 on: May 09, 2021, 05:43:26 pm »
Some examples:



« Last Edit: May 09, 2021, 05:44:57 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #15 on: May 10, 2021, 06:49:50 pm »
If the analog supply needs to be as clean as posible, better to add a small choke and/or low value resistor in series to filter the supply noises.
And put a ~10-22uF tantalum cap + ~1nF low esr ceramic cap.
If you don't mind some noise, just put a 47...100ohm resistor between vdd and vdda and the CAP close as possible to the vdda pin.
Check vdda current demand to ensure the voltage won't drop more than a few mV in the resistor.

Can you tell me exactly where to place the components you mention?
I'm not familiar with the term choke and about the small value resistor, where would it be placed?

The ~10-22uF tantalum cap + ~1nF low esr ceramic cap should go where?

About the noise and the 47...10ohm resistor between VDD and VDDA and the CAP close as possible to the VDDA pin. If it brings noise, why you suggest to put it there? What would be the goal?
------------------------------------------------------------------------------------------------------------------



I think I had a few things wrongly connected in the image above.

I changed the layout a little bit and I still have a question. This is how it looks like right now:


The question is if I'm going to connect VDDA to VDD. I'm not sure about this. In the image above, I have connected them together but I don't know if they are supposed to be connected that way.

VDD and VDDA are shorted.

I know, I just said it above. And I also said I was not sure it was correct... :\
------------------------------------------------------------------------------------------------------------------



Some examples:





Could you please elaborate a bit further? Be more specific, please?I mean, the example you give is with a different IC, not the same pin out, an I got lost in the specific goal of your examples.
« Last Edit: May 10, 2021, 07:07:03 pm by psysc0rpi0n »
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #16 on: May 10, 2021, 07:38:31 pm »
What you don't understand? Yes, it's a different circuit/part, but the working principle is the same.
In any circuit you must put a decoupling capacitor close as possible to the power pin. This is no exception.

You don't connect VDD and VDDA directly, as VDD will have digital noises and other high frequency stuff that can affect the analog circuits.
Instead, you put a small resistor or choke in series that barely drops any voltage. So you do: VDD-> Choke/Resistor ->VDDA

You must know the current for VDDA. Checking the datasheet it seems it's not higher than 1mA when operation the ADC at max speed.
Using a 10ohm resistor, the Vdrop will be 10mV, which is fine.
Add some capacitors and you made a low band pass filter. All the high frequency noise in VDD will be eliminated.
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 
The following users thanked this post: psysc0rpi0n

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #17 on: May 10, 2021, 08:15:04 pm »
What you don't understand? Yes, it's a different circuit/part, but the working principle is the same.
In any circuit you must put a decoupling capacitor close as possible to the power pin. This is no exception.

Like this ?


You don't connect VDD and VDDA directly, as VDD will have digital noises and other high frequency stuff that can affect the analog circuits.
Instead, you put a small resistor or choke in series that barely drops any voltage. So you do: VDD-> Choke/Resistor ->VDDA

You must know the current for VDDA. Checking the datasheet it seems it's not higher than 1mA when operation the ADC at max speed.
Using a 10ohm resistor, the Vdrop will be 10mV, which is fine.
Add some capacitors and you made a low band pass filter. All the high frequency noise in VDD will be eliminated.

This way?


The circuit is now like this:
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #18 on: May 10, 2021, 08:29:40 pm »
For example, although 10uF would be better. Be careful to what you connect PWR_FLAG to, you don't want to inject any noises there.
In the pcb, put the VDDA capacitors near the pin. Ideally few mm away at most.

Also, it's better to add a small capacitor when reading voltage coming from a resistor.
This is because when the ADC samples the pin, the sample-and-hold circuit charges an internal capacitor and then disconnects the pin.
That time is usually adjustable, called sample or adquisition time. The higher the input inpedance, the higher the time you need to wait.
The internal capacitor is usually in the 5-10pF range. Seems low, right? But if the sample time is too low, the resistor might not let it charge completely, and your reading will be wrong.
Ex, if TH1 is 5K, to completely charge 10pF you need about 250nS. If it's 10K, it will be 500nS.
Doesn't seem a lot, but you need to know that when designing the analog frontend.
Depending in the application you might have issues. A small capacitor, ex. 100pF close to the ADC pin help to compensate this.

What's the PWM frequency? Driving mosfets that way is usually ok for low frequencies, but in the best scenario, not more than few KHz.
Depending on the gate capacitance, you might end making a RC integrator.
What happens next is that instead fully turning the fet on and off, the gate remains charged at a intermediate voltage.
That is very dangerous because the mosfet can work in the ohmic region (work as a resistor) and overheat very quickly. Also the load won't get perfectly controlled.

You can see the effect yourself, run the PWM at 50% and 500Hz, then start increasing the frequency in ex. 500Hz steps (Don't put load in the transistor) and keep watching the signal before and after R4.
As you rise the PWM frequency, it will start looking like this:

As you see the voltage is not reaching zero, also the rising and falling times are terrible, so the transistor will start to heat up if there was load.


If you keep increasing the frequency it will get worse and worse:

Now the transistor is probably turned on all the time!
Your load is out of control, and if the transistor is in the ohmic region and the flowing current high enough, it will overheat and fail in no time.

« Last Edit: May 10, 2021, 09:12:43 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #19 on: May 10, 2021, 08:45:28 pm »
For example, although 10uF would be better. Be careful to what you connect PWR_FLAG to, you don't want to inject any noises there.
In the pcb, put the VDDA capacitors near the pin. Ideally few mm away at most.

Also, it's better to add a small capacitor when reading voltage coming from a resistor.
This is because when the ADC samples the pin, the sample-and-hold circuit charges an internal capacitor and then disconnects the pin.
That time is usually adjustable, called sample or adquisition time. The higher the input inpedance, the higher the time you need to wait.
The internal capacitor is usually in the 5-10pF range. Seems low, right? But if the sample time is too low, the resistor might not let it charge completely, and your reading will be wrong.
Ex, if TH1 is 5K, to completely charge 10pF you need about 250nS. If it's 10K, it will be 500nS.
Doesn't seem a lot, but you need to know that when designing the analog frontend.
Depending in the application you might have issues. A small capacitor, ex. 100pF close to the ADC pin help to compensate this.

You mean 10u for the cap between 12V and GND?
This 12V are already coming from a RockPro64 board. So I assume they are already kinda "clean", so to speak.

The POWER_FLAGs were added jus to pass the Electrical Test. Those points are all coming from this RockPro64 board.

The capacitor near the ADC pin would be to the ground, right?
Like this:
« Last Edit: May 10, 2021, 08:50:24 pm by psysc0rpi0n »
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #20 on: May 10, 2021, 09:04:08 pm »
No, 10uF for C5. 1uF will be probably good enough, I'm just a little fanatic with filtering  ;D

Yeah, that's exactly what I mean in the ADC pin. Check it yourself, make 100 readings with and without it, and see if there are differences, little noises...
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #21 on: May 10, 2021, 09:31:18 pm »
No, 10uF for C5. 1uF will be probably good enough, I'm just a little fanatic with filtering  ;D

Yeah, that's exactly what I mean in the ADC pin. Check it yourself, make 100 readings with and without it, and see if there are differences, little noises...

Being paranoid with filtering, it's not bad, I guess. Unless the paranoid starts getting too expensive for the application.
Anyway, other than the corrections I did, suggested by you and others, would this be good to go?

I'm sure I'll make some more adjustments as time goes by. One starts remembering that is missing this here and there, mostly about connectors, test points, etc.
Also, most of the capacitors and resistors here are already beyond what the datasheet suggests. That's why even simple circuits like this, tend to get tricky and a bit complex when you have no experience (like me).

I still have to write down which capacitors needs to be tantalum, which needs to be placed close as possible to leads, etc. And some other minor details before sending this to the mill factory to make the PCB. I think this is not worth but I want to make this, for learning purposes!
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #22 on: May 10, 2021, 10:08:16 pm »
You have duplicated the VDDA filtering.
In the upper side you have the 10R resistor.
In the lower you have a 10uH coil?

I don't understand power_flag.
It's connected to VDDA at one side, but shorted to VSS/GND at the other.

Another tip: put a 3.3v zener in the stm32 pin that drives the mosfet (before R4).
So if the mosfet blows up and gets shorted, the zener will clamp the voltage, protecting not only the stm32, but everything connected to VDD.
Otherwise the stm32 pin clamping diode will put 12V into VDD!
If 12V gets into, with 220ohm R4 the zener will take 0.13W and R4 0.35W.
You can also put a very low power R4 (1/10w or less), so it acts as a fuse if such condition happens.

You usually put a NPN transistor in the middle to help with isolation and protection.
« Last Edit: May 10, 2021, 10:31:42 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 

Offline psysc0rpi0n

  • Frequent Contributor
  • **
  • Posts: 287
  • Country: pt
Re: Fan controller with PWM using STM32 chip
« Reply #23 on: May 11, 2021, 07:35:10 am »
You have duplicated the VDDA filtering.
In the upper side you have the 10R resistor.
In the lower you have a 10uH coil?

As you may have noticed, I'm far from understanding all the circuitry. I mean, I understand after I know about it. I added the inductor there by suggestion of a friend of mine. I think he told me it would help to compensate for voltage drops happening in PCB tracks. It was not for filtering purposes. I think this was what he told me.

I don't understand power_flag.
It's connected to VDDA at one side, but shorted to VSS/GND at the other.

I added the power flags only to get rid of the warnings from Electrical check. Otherwise, how would I get rid of them? I'll show later a picture with the warning fllags.

Another tip: put a 3.3v zener in the stm32 pin that drives the mosfet (before R4).
So if the mosfet blows up and gets shorted, the zener will clamp the voltage, protecting not only the stm32, but everything connected to VDD.
Otherwise the stm32 pin clamping diode will put 12V into VDD!
If 12V gets into, with 220ohm R4 the zener will take 0.13W and R4 0.35W.
You can also put a very low power R4 (1/10w or less), so it acts as a fuse if such condition happens.

You usually put a NPN transistor in the middle to help with isolation and protection.

I can do part of  that. I'm not sure I have 1/10W resistors here but when I'm about to assemble the whole circuit, I'll have to decide each component ratings.

Can you be more specific about the transistor? The exact place where to put it? In the middle of what?
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 1096
  • Country: es
Re: Fan controller with PWM using STM32 chip
« Reply #24 on: May 11, 2021, 11:02:19 am »


Use a lower value for R1, so the mosfet switches faster. 220 or 100ohms should be ok.
10K is ok for simple on/off applications, but too slow for pwm.
This circuit have more strenght, so the mosfet will switch faster.
Of course that is the simplest circuit. There are much better ones. For example push-pull circuits that use a npn+pnp, they switch even faster. Search push-pull mosfet driver.

Now if everything shorts out, the 10K resistor will limit the current a lot, max 1.2mA.
« Last Edit: May 11, 2021, 11:06:22 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ
Stm32 Soldering FW      Forum      Github      Donate      I need calibration reports!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf