# Driving MOSFET with PWM signal from 18F4550

#### Vitesse

Joined Feb 10, 2013
5
Hi Guys,

Stuck on a little problem (or several problems).

I'm working on a project to PWM control a couple of PC fans from a PIC18F4550 and as there will be 4 fans wired in parallel I thought the best option would be to use a MOSFET rather than an NPN transistor due to the higher current requirements.

So, I've used the attached circuit however It seems that I dont get full control of the fan speed, it seems to be achieving full speed with only about a 50% PWM duty cycle, now I wasnt sure if this was just due to the fan itself so i've tried a few different ones with the same result.

Can someone please just have a glance over my circuit (attached) and see what they think and if there is something wrong, what I might be able to try to resolve the problem. The PWM frequency is 25KHz which should be well within the capabilities of the STP36NF I am using.

Thanks

#### Attachments

• 180.1 KB Views: 108

#### GopherT

Joined Nov 23, 2012
8,012
When your CCPR2L register (on-duty period) is greater than PR2 (PWM Period) register, then you will always be on (No PWM control - just on).

#### THE_RB

Joined Feb 11, 2008
5,438
Your circuit is fine.

The symptom is quite normal. PC fans have electronics built in, and don't need the full continuous 12v to get to full fan speed. So as you are finding the fans might be getting up to full speed long before your PWM gets to 100% PWM.

The first thing I would do is put a cap across the fan and a resistor in series with one fan wire. Maybe a 470uF cap and a 100 ohm resistor. It depends on fan size and current.

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
I think you should add push pull conf at mosfet gate to turn on/off mosfet fast b'coz discharging of gate capacitance take time...with using (directly) will get mosfet hot.

#### Vitesse

Joined Feb 10, 2013
5
When your CCPR2L register (on-duty period) is greater than PR2 (PWM Period) register, then you will always be on (No PWM control - just on).
Hi GopherT, this doesnt appear to be the problem, as I have a LED (via suitable series resistor) connected to the PIC output, and brightness varies when setdcpwm1 is set to values ranging from 0-1023.

Tried adding an electrolytic cap across the fan and this has had one good effect so far, reduced the noise the fan was producing and smoothed the speed out a little however still getting nothing resembling full control over it however I was hoping to avoid using a series resistor as this will be driving 4x120mm fans. Setting a value ranging from 270 - 520 gives Stall to 100% speed.

This is the code im using for the PWM outputs (Hitech C)
Rich (BB code):
OpenPWM1(0xFF);
OpenPWM2(0xFF);
OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & T2_POST_1_1);
20MHz Xtal OSC

no longer worrying about PWM frequency, just concentrating more on getting it to work then sort out the finer details later

So far regarding temperatures the mosfet is absolutely stone-cold with no heatsink, this is only being breadboarded at the moment. Would a push-pull circuit result in better switching?

Also a pecuiliarity, with an electrolytic cap across the fan, if i turn the 12V supply off (5v is separate) this is causing the PIC to reset, could this be that the cap on the 5V supply rail (100uF 16V) is too low a value? I also have a 100nf on each pic supply terminal bearing in mind this is powered by a bench PSU (12V and 5V) at present not a 7805/similar

Last edited:

#### Vitesse

Joined Feb 10, 2013
5
Thanks for the input and advice earlier guys

Another alternative I have been thinking about is there a way I can force the CCP module to go into 8-bit rather than 10-bit PWM even just as a temporary thing? This would certainly make debugging easier as sending a value in a single byte would reduce the chances of my poor coding also causing problems as at present I'm sending 2 bytes and performing maths to achieve what should be a fairly straightforward task and since this is just a fan speed control accuracy is not that important

Thanks

#### THE_RB

Joined Feb 11, 2008
5,438
You need that series resistor if you are going to put a large electro across the fan wires (see my post above).

Again, it's normal for the fan to need X volts to start turning. These fans have transistors and hall sensors etc inside, and have a minimum voltage before they start working.

Re the 8bit PWM, that is very easy! All you do is just load your 8bit PWM byte into CCPRL as normal, but don't change the 2 LSB bits in CCP1CON.

8bit PWM is probably plenty for a basic fan speed control (like you said).