PWM fan control

jpacman

Member
hey guys,

Im working on this project for school where i'm using the "DS18B20" to record the temp and set the percentage of the fan speed(i'm using a 2 wire fan.) i have everything set up except for the PWM for the fan control. I was looking around and don't really understand how PWM works and what everything means. this is my code

Code:
symbol temp = b1
	

main:
	;Read temp from DS18B20
	readtemp C.1,temp
	;Calc Values
	let temp = temp - 128
	;Start fan process
	if temp >= 35 AND temp =< 39 then ThirtyFive
		if temp =< 35 then goto main
	if temp >= 40 AND temp =< 44 then Fourty
		if temp =< 40 then goto main
	if temp >= 45 AND temp =< 49 then FourtyFive
		if temp =< 45 then goto main
	if temp >= 50 AND temp =< 54 then Fifity
		if temp =< 50 then goto main
	if temp >= 55 AND temp =< 59 then FifityFive
		if temp =< 55 then goto main
	if temp >= 60 AND temp =< 64 then Sixty
		if temp =< 60 then goto main
	if temp >= 65 then SixtyFive
		if temp =< 65 then goto main
	goto main
	
ThirtyFive:	
	high b.0
	wait 1
	low b.0
	goto main
	
Fourty: 
	high b.1
	wait 1
	low b.1
	goto main
	
FourtyFive:
	high b.2
	wait 1
	low b.2
	goto main
	
Fifity:
	high b.3
	wait 1
	low b.3
	goto main
	
FifityFive:
	high b.4
	wait 1
	low b.4
	goto main
	
Sixty:
	high b.5
	wait 1
	low b.5
	goto main
	
SixtyFive:
	high c.4
	wait 1
	low c.4
	goto main
This code here is to determine the temp of "DS18B20" is recording. can anyone help me with the PWM side of things ? I will be using pin C.1 for the "DS18B20" and pin B.2 for my fan.
 

lbenson

Senior Member
1) which picaxe, which fan?

2) pwm is usually used to control the speed of a fan by altering the "duty cycle" of the signal (the percent of the time the signal is high--less time high means less current delivered to the fan, more time high means more current, and higher speed). Have you looked at the PWM Wizard (picaxe wizards, pwmout in PE5 and PE6)? Start with 1khz and maximum duty cycle and decrease until the fan stops. If the fan is noisy or jerky, increase to 2khz or higher.

3) What is the statement "let temp = temp - 128" supposed to do? Readtemp returns a value which will be the Centigrade temperature if not below zero. Subtracting 128 will result in an arithmetic "underflow", and will not give you a value which you will find useful.
 

jpacman

Member
1. I'm using the picaxe 14M2 and the fan I'm using is a 30mm 5V/200mA (2 wire) fan found here

2. that's was the answer i was looking for thank you for explaining how it works.

3. Sorry i read the "DS18B20" data sheet wrong.
 

lbenson

Senior Member
Ah, 200mA. That's more than a picaxe can directly drive, so you'll need something like a mosfet to drive it.

Recent threads have suggested that the IRL540 would be suitable for your needs (and for a lot more current). Check out Manual 3, page 8, "Standard Circuits 4 - The Power MOSFET Interfacing Circuit" for how to wire it up. Don't omit the protection diode across the motor terminals.
 

jpacman

Member
okay I'v looked at the manual 3 page 8 and I'm a little unsure on how to set up the circuit. Capture.PNG the pink line is the output line for the PWMOUT to the fan. the red and black lines at the top are positive.
 

lbenson

Senior Member
0V to middle pin of Temp, 5V to + pin, 4K7 resistor between + and data.

Pink line to gate of mosfet, fan "-" of motor to mosfet drain, mosfet source to board 0V, diode across motor terminals "+"
& "-", 5V to fan "+".

Draw your interpretation and repost. What is it about manual that you don't understand?
 
Last edited:

jpacman

Member
Capture.PNG
this is as much as I got before I got confused, what does the M in the circle mean on page 8 of manual 3
 
Last edited:

lbenson

Senior Member
Your blue line should go to 0V instead of to B.2. You figured out that "M" in the manual referred to "motor"--fan motor in your case.

You still need the 4.7K resistor between Temp "+" and data.

You don't show the .1uF capacitor which the picaxe needs to surpress transients.

Note that the picaxe will have trouble driving the IFR530 with pwm without overheating. You need a "logic level" mosfet like the IRL540. Also, when switching a motor, you may experience dips in voltage which would cause the picaxe to reset. I'd recommend replacing the 4.7uF capacitor with, perhaps, 100uF.
 

jpacman

Member
Capture.PNG

Is this right now ?

I intend on using the IRL540 its just that the program I'm using doesnt have that component so i just had to use the IFR530 to represent where the IRL540 will go.
 
Last edited:

Hemi345

Senior Member
Closer. Put the 10K pull down on the gate pin of the 'IRL540' to ground.

Also as lbenson noted, put a 100uF cap and 0.1uF cap close to the power pins on the PICAXE to keep it happy.

Is that an IR sensor at the top? If so, get rid of the blue wire on it. Should the OUT pin on it be routed to a pin on the PICAXE? What's the little two pin thing to the right of it?
 

lbenson

Senior Member
I think the lower part is ok. What is the upper part supposed to be doing? What is the unlabeled 3-pin device? A 100 ohm resistor would be insufficient to protect your led (if that is the component between OUT and GRD).
 

jpacman

Member
Capture.PNG

fixed the 10K and yes it is, the blue wire is there for testing purposes and the output pin is the top most pin. no the IR is not ment to be going to the picaxe as its ment for a different circuit. the 2 pin thing on the right of the IR is a LED to show that the IR is receiving data.
 

AllyCat

Senior Member
Hi,

The type of fan used in computers are brushless types and have an onbord chip to control the fan.
Have you noted Bill's comment? The type of "PWM" you're planning to use is really intended for simple commutator dc motors and a brushless type might not work. You may need to smooth (low-pass filter) the PWM to dc, but even then the control ic may be unhappy at lower voltages.

IMHO a cheap bipolar transistor (e.g. BC337) could handle the power, but you really should do a few "bench" measurements on the motor before designing and/or building any real hardware.

Cheers, Alan.
 

jpacman

Member
yes today i intend on testing the fan to see what the lowest voltage is before it cuts out so i can make a judgment. and im not really sure on how i should used or how PWM really works so i was hoping someone here could help.
 
Last edited:

jpacman

Member
this is my current code. dont pay any attention to me using different pins to each program its just a way for me to test in the simulation

Code:
;symbol
symbol temp = b1
;main program
main:
	;Read temp from DS18B20
	readtemp C.1,temp
	;Start fan process
	if temp < 45 then goto main	
	if temp >= 45 AND temp =< 49 then FourtyFive
		if temp =< 45 then goto main
	if temp >= 50 AND temp =< 54 then Fifity
		if temp =< 50 then goto main
	if temp >= 55 AND temp =< 59 then FiftyFive
		if temp =< 45 then goto main
	if temp >= 60 AND temp =< 64 then Sixty
		if temp =< 60 then goto main
	if temp >= 65 AND temp =< 69 then SixtyFive
		if temp =< 55 then goto main
	if temp >= 70 AND temp =< 74 then Seventy
		if temp =< 70 then goto main
	if temp >= 75 then SeventyFive
		if temp =< 75 then goto main
	goto main
;45 Deg	
FourtyFive:	
	high B.0
	wait 1
	low b.0
	
	goto main
;50 Deg	
Fifity: 
	high b.1
	wait 1
	low b.1
	goto main
;55 Deg	
FiftyFive:
	high b.2
	wait 1
	low b.2
	goto main
;60 Deg	
Sixty:
	high b.3
	wait 1
	low b.3
	goto main
;65 Deg	
SixtyFive:
	high b.4
	wait 1
	low b.4
	goto main
;70 Deg	
Seventy:
	high b.5
	wait 1
	low b.5
	goto main
;75 Deg	
SeventyFive:
	high c.4
	wait 1
	low c.4
	goto main
 

lbenson

Senior Member
Your indented if statements ("if temp =< 45 then goto main") are redundant--if true, the code will already have jumped to one of the degree labels.

Because your temperature ranges are in 5 degree groups, you could simplify your calculations:

if temp < 45 then goto main
b4 = temp - 45 / 5 + 1 max 7 ' results in values 1-7 for temps above 44

Then you could calculate a pwm duty cycle, with 7 steps between lowest which turns the fan and max (full power, no pwm). As someone pointed out in this or another similar thread, you might find that the fan speed did not increase linearly with duty cycle, so you might need to determine values experimentally and then use the lookup command to get the pwm duty cycle. (If straight PWM works for your fan at all.)
 

lbenson

Senior Member
Glad you got the fan running. That's a good sign. Note that if you just use a jumper between 5V and OUT, you have only a 100 ohm resistor for the led. That would mean 5 volts divided by 100 ohms equals 50 milliamps through the led. That could burn out many leds. Replace the jumper with a resistor. Many values between 220 and 1k or even 2k would probably work (in addition to the 100 ohm), depending on the led, but 330 ohms would be quite safe.
 

jpacman

Member
i have a led that will be suitable for this circuit. i only tested the fan with a variable power supply to see the lowest volts. will this fan still work ?
 

lbenson

Senior Member
>will this fan still work ?

Probably. PWM drive of a mosfet with the picaxe essentially provides a variable voltage: maximum voltage times the percent on (duty cycle). With a 5 volt supply, your motor probably won't start turning until the duty cycle is about 60% (the approximately 3 volts you measured divided by 5 volts).
 
Top