PDA

View Full Version : How to use a potentiometer resistor to control speed of motor fan manually?Thanks



mengckid
- 21st August 2005, 10:50
Hi,everybody...i am just a beginner to use Pic...and the software that i use to compile is PicBasic Pro

Thus, i would like to know any ideas to use potentiometer resistor of 10k ohm to control the speed of motor fan manually...like i increase the voltage of potentiometer,the fan will run faster...and the opposite.

In pic Basic code,i try to add ADCIN and PWM command into it. ADCIN command is read adc values and store in a variable. Then i write a PWM command to control speed.

Any ideas, or any Pic Basic Pro source codes can help me...thanks :)

markedwards
- 21st August 2005, 13:52
Hi Mengckid,

You did't mention which PIC you were using or what application.
Here is code that I use with hardware pwm (HPWM).
My application requires 5 speeds and a 60% minimum speed setting.
You can add more comparisons or calculate speed settings based on AD1.
There are 2 lines that control acceleration & de-acceleration
I like the hardware pwm best, it runs consistent speed
while your program executes other code. It will work with PWM if you change the last line.

ADCIN 1,AD1 'read channel 1...AN1
AD1 = AD1 >> 6 'justify data

ADJUSTFAN:
IF AD1 < 2 then FANSPEED = 153 '60% min speed
IF AD1 >= 3 then SPEED = 178 '70%
IF AD1 >= 5 then SPEED = 204 '80%
IF AD1 >= 7 then SPEED = 229 '90%
IF AD1 >= 9 then SPEED = 255 '100%
IF SPEED > SPD THEN SPD = SPD + 1 'accelerate
IF SPEED < SPD THEN SPD = SPD - 1 'de-accelerate
hpwm 2,SPD,32760 ' PWM 20,000 Hz

Mark

mengckid
- 22nd August 2005, 02:40
Hi Mengckid,

You did't mention which PIC you were using or what application.
Here is code that I use with hardware pwm (HPWM).
My application requires 5 speeds and a 60% minimum speed setting.
You can add more comparisons or calculate speed settings based on AD1.
There are 2 lines that control acceleration & de-acceleration
I like the hardware pwm best, it runs consistent speed
while your program executes other code. It will work with PWM if you change the last line.

ADCIN 1,AD1 'read channel 1...AN1
AD1 = AD1 >> 6 'justify data

ADJUSTFAN:
IF AD1 < 2 then FANSPEED = 153 '60% min speed
IF AD1 >= 3 then SPEED = 178 '70%
IF AD1 >= 5 then SPEED = 204 '80%
IF AD1 >= 7 then SPEED = 229 '90%
IF AD1 >= 9 then SPEED = 255 '100%
IF SPEED > SPD THEN SPD = SPD + 1 'accelerate
IF SPEED < SPD THEN SPD = SPD - 1 'de-accelerate
hpwm 2,SPD,32760 ' PWM 20,000 Hz

Mark

Thanks u,anyway...:) you are so great,ur code help me much,thanks u

The PIC i am using is PIC16F874A, and the application is used the single turn 10k potentiometer to increase or decrease the speed of motor fan in another pin in PIC. Meaning,i will use a N-channel MOSFET to drive the current to motor fan.

Sorry for that, if I willing to use PWM command to control speed of motor fan, meaning accelerate or de-accelerate it, can I do that?

Hopefully, you can help me, thanks a million :)

mister_e
- 22nd August 2005, 02:51
if I willing to use PWM command to control speed of motor fan, meaning accelerate or de-accelerate it, can I do that?


If you mean PWM instead of HPWM you'll be disapointed, PWM do only few cycle and don't run in background. AND MORE, the signal generated is quite poor. This is really not what you need.

If your PIC don't have any PWM module, you can still use timer interrupt to generate the PWM signal.

HPWM is the way to go. It could be as simple as...


START:
ADCIN 0,MotorSpeed
HPWM MotorSpeed
'
' Other code here
'
GOTO START

CocaColaKid
- 22nd August 2005, 04:12
I have to agree with Mr E, HPWM is definitely the way to go. Once you set it it keeps on running until PWM which needs to be refresh all the time. We use an op-amp with high input impedence along with a small capacitor attached to the IO so that we can do other things with out having to refresh all the time. It's not a smooth an output as the HPWM but it does work. The capacitor slowly discharges through the op-amp.

markedwards
- 22nd August 2005, 19:04
Hi Mengckid,

Your Pic has the hardware PWM built-in so it is your best choice.
Your program can read the ADC (pot) and update the speed variable
and the hardware PWM does the rest.
Depending on your motor voltage & current draw you may need to
consider your Mosfet circuit. Your Mosfet's voltage rating should be
at least twice your DC supply voltage. You need a clamp diode connected
across your motor (cathode to M+ anode to M-).
Your Mosfet has 2 losses that generate heat, conduction loss (dc current squared times the rds on resistance). example 2 Amps x 2 Amps x .1 ohm = .4 watts, requiring a heat sink. The Mosfet has a large gate capacitance that can give slow turn on & turn off times (switching losses). I prefer to use a mosfet driver IC such as TC4426 or TC4427.

The code:
IF SPEED > SPD THEN SPD = SPD + 1 'accelerate
IF SPEED < SPD THEN SPD = SPD - 1 'de-accelerate
will slowly icrement the PWM SPD by + or - 1 count per loop.
This acts like a "soft start" reducing the inrush starting current
and also a filter effect for your pot voltage. You can easily modify to
speed the accelerate/de-accelerate response.

Have fun!

Mark

mengckid
- 24th August 2005, 06:35
Hi Mengckid,

Your Pic has the hardware PWM built-in so it is your best choice.
Your program can read the ADC (pot) and update the speed variable
and the hardware PWM does the rest.
Depending on your motor voltage & current draw you may need to
consider your Mosfet circuit. Your Mosfet's voltage rating should be
at least twice your DC supply voltage. You need a clamp diode connected
across your motor (cathode to M+ anode to M-).
Your Mosfet has 2 losses that generate heat, conduction loss (dc current squared times the rds on resistance). example 2 Amps x 2 Amps x .1 ohm = .4 watts, requiring a heat sink. The Mosfet has a large gate capacitance that can give slow turn on & turn off times (switching losses). I prefer to use a mosfet driver IC such as TC4426 or TC4427.

Mark

HiHi,yaya, thanks u again :)

i got few problem here:

for the code above, I cannot compile this part: The code:
IF SPEED > SPD THEN SPD = SPD + 1 'accelerate
IF SPEED < SPD THEN SPD = SPD - 1 'de-accelerate

the error message is mismatching if loop..then...endif. Thus, i dunno how to fixed it. Thus, here is my code, hopefully you can help me a bit, thanks ya :D


INCLUDE "modedefs.bas"
DEFINE OSC 4

DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds
AD1 var byte
SPEED var byte
SPD var byte

TRISB = %10000000 ' set output for led portb.0
TRISC = %11111000 ' set output for pwm portc.2
TRISA = %111111 ' Set PORTA to all input

ADCON1 = 0 ' for 10-bit ADC and Set all PORTA is analog
ADCON0 =%10000101 'SET A/D Fosc/32, [4MHz], CHANNEL-0 = ON

main:

ADCIN 0,AD1 'read chanel 1...AN1
AD1 = AD1 >> 6 'justify data
gosub ADJUSTFAN
hpwm 2,SPD,300 ' PWM 20,000 Hz

IF SPEED > SPD THEN goto acce
endif
if speed <= SPD then goto deacce
endif

ADJUSTFAN:

IF AD1 < 2 then
SPEED = 153
endif '60% min speed
IF AD1 >= 3 then
SPEED = 178
endif '70%
IF AD1 >= 5 then
SPEED = 204
endif '80%
IF AD1 >= 7 then
SPEED = 229
endif '90%
IF AD1 >= 9 then
SPEED = 255
endif '100%
return

acce: SPD = SPD + 1 'accelerate
hpwm 2,SPD,300
return

deacce: SPD = SPD - 1 'de-accelerate
hpwm 2,SPD,300
return

P/s : The code above is Pic Basic Pro code...


Another problem is how do I get the ADC scaling. Actually, what is ADC scaling mean?? If I wanna do the ADC scaling from 1-32, so how to write in pic basic code? Is it neccessary to iniatiate first, the 1-32 values?

Thanks u,cheers

Regards
Mengckid

mengckid
- 28th August 2005, 15:36
I need some helps~~Can anybody tell me how to scale ADC or ADC scaling meaning if i wanna scale a voltage value from 0V to 5V in ADC Scaling from 1-32 values? How to do that?

In Pic Basic Pro, Do I need to initiate the ADC scaling 1-32 values? like assembely language.

If I wanna do a increasing speed or decreasing speed continuesly, i wanna create a mathematical equations Pic Basic Pro?Can I...anyone got any ideas,can share here....hehe,thanks a million...I really need a help here,thanks you :D

mister_e
- 28th August 2005, 16:06
a range from 0-32!!

if you use a 10Bit resolution


Adcin 0,Initial
Final=Initial>>5


change 5 for 3 if you use a 8 bit resolution.

32= 2^5 or 5 bit resolution

Im i still mixed in my head... maybe, as now, it looks good to me.

10 bit resolution = range of 1024
8 bit resolution = range of 256

1024/32 =>32
256/32 =>8

and blah blah blah... i feel like a pre-school teacher today :)

mengckid
- 30th August 2005, 06:33
Hi...I need some helps here...once again,i will post my Pic Basic Pro Code below,hopefully somebody will help me fixed my code, bcoz i just cant get the result that i want...

The Code:

include "modedefs.bas"
DEFINE OSC 4

' Define ADCIN parameters
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds

B0 var BYTE ' Create B0 to store result

TRISB = %10000000 ' set output for led portb.0
TRISC = %11111000 ' set output for pwm portc.2
TRISA = %111111 ' Set PORTA to all input

CCP1CON = %00001100 ' Set CCP1 to PWM
ADCON1 = 0 ' for 10-bit ADC and Set all PORTA is analog

HIGH PORTB.0

GOSUB GETMBAT 'read voltage variable
main:

ADCIN 0,B0 ' Read channel 0 to B0
B0 = B0 >> 3 'justify data

if (B0>0) and (B0<5) THEN
PWM PORTC.1,10,100
endif
if (B0>5) and (B0<10) THEN
PWM PORTC.1,30,100
endif
if (B0>10) and (B0<15) THEN
PWM PORTC.1,60,100
endif
IF (B0>15) AND (B0<20) THEN
PWM PORTC.1,100,100
endif
IF (B0>20) AND (B0<25) THEN
PWM PORTC.1,127,100
endif
IF (B0>25) AND (B0<28) THEN
PWM PORTC.1,160,100
endif
IF (B0>28) AND (B0<30) THEN
PWM PORTC.1,200,100
endif
if (B0>30) and (B0<32) then
PWM PORTC.1,254,100
endif

GOTO main

GETRESULT:
PAUSEUS 50 'WAIT FOR ADC SETUP
ADCON0.2 = 1 'START CONVERSION BY SETING GO/DONE BIT HIGH

LOOP: ' WAIT FOR GO/DONE BIT TO CLEAR
IF ADCON0.2 = 1 THEN
GOTO loop
ENdif
goto main


GETMBAT:
ADCON0 =%00000101 'SET A/D Fosc/32, [4MHz], CHANNEL-0 = ON
GOSUB GETRESULT 'START CONVERSION
B0 = ADRES 'STORE THE A/D CONVERSION VALUE IN RESULT
RETURN


The PIC that I am using is PIC 16F874A, and i am trying use a 10k potentiometer to control the motor speed (increase or decrease the speed). Thus, I try the HPWM before, just cant get the result also.

Hopefully, somebody will help me...thanks u :)