PWM fan speed control with a 12F683


Closed Thread
Results 1 to 18 of 18
  1. #1
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305

    Default PWM fan speed control with a 12F683

    Ladies and Gents,
    I'm trying to use a 12F683 to control a fan motor. I want to be able to push a button and have the fan cycle through speeds from off to high in 3 steps.

    The problem is I can't figure out how to use PWM indefinitely.

    This is my program. I've got the push button on GPIO.3 and the fan currently on GPIO.5. It works kinda but the selected speed only lasts for the length of time PWM is set. During that time you can't select another speed.

    Your thoughts would be appreciated.

    clear
    define osc 4
    @ device mclr_off 'the input will be through GPIO.3
    ansel = 0 'all ports digital
    cmcon0 = 7 'turn off comparators
    adcon0 = 0

    touchnumber var byte 'count the number of touches
    outTOfan var gpio.5 'LED symbolizing fan speed
    trisio = %001111
    touchnumber = 0

    starthere:
    outtofan = 0
    If gpio.3 = 1 then goto main
    goto starthere
    main:
    touchnumber = touchnumber + 1
    if touchnumber =4 then touchnumber = 0
    select case touchnumber
    case 0
    gpio.5 = 0
    case 1
    pwm gpio.5, 60,1000
    case 2
    pwm gpio.5,150, 1000
    case 3
    pwm gpio.5, 255, 1000
    end select
    goto starthere

    end
    Last edited by AvionicsMaster1; - 30th June 2011 at 20:55. Reason: add tags

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    use HPWM instead.

    You set the frequency, set the duty cyle (motor speed)... and you're done. It runs in background with the internal CCP module and it will run as long as you want... or power failure (duh....).
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Try this code.
    LED is on GPIO.2
    Button must be pulled down to GND by a resisitor (10K).
    And must be pulled high by the button (to Vdd).



    Code:
    @ DEVICE PIC12F683, FCMEN_OFF, IESO_OFF, INTRC_OSC_NOCLKOUT , WDT_OFF, PWRT_ON, MCLR_OFF, PROTECT_ON, BOD_OFF
    OSCCON  = %01110001     ' Internal 8MHz osc.
    DEFINE OSC 8
    ' ======= Common Settings =================================
    ADCON0 = 0
    ANSEL  = 0
    CMCON0 = 7           ' Comparators off.
    TRISIO = 0
    GPIO   = 0
    ' ============= PWM Settings =========
    CCP1CON =   %00001100       ' CCP1, PWM mode
    PR2     =   250             '
    T2CON   =   %00000101       ' TMR2 on, prescaler 1:4
    CCPR1L  = 0
    Speed var CCPR1L
    Btn  Var GPIO.3  ' Assuming ; the pin is pulled down to GND by 10K resistor, and button pulls it high to Vdd.
    ' LED is connected to GPIO.2 (on board PWM module)
    pause 10
    Start:
        if btn then 
            if speed + 85 < 256 then    ' 0, 85, 170, 255, 0 
                speed = speed + 85
            else
                speed = 0
            endif
     
            while btn : pause 20 : wend
        endif
     
        pause 2
    goto Start
    end
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Thanks Sayzer,
    I've had some other issues to resolve before I finally got to try it today. It works fine and does exactly what I want to accomplish.

    I do have a few questions about how the voodoo that itdoo works but I want to experiment a bit more before I ask them.
    Last edited by AvionicsMaster1; - 19th July 2011 at 03:13. Reason: fat fingered

  5. #5
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Since you were able to provide an answer for that, do you have any ideas how I can run two outputs simultaneously but each at different output levels?

    What I want to do is have a switch turn on a fan at stepped levels and another switch that controls a light/LED that can be set to a different level.

    I know that isn't what the post started with but I assumed I'd be able to use the PWM command for setting the levels and maybe erroneously thought I could accomplish that for two different outputs.

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Since you have an onboard PWM module running in the background, you can also have a second pwm by software running in the foreground.

    The code would be as follows.
    You are now using two buttons, one is for LED and the other is for Fan.
    I suggest that you use Fan on GPIO.2 (on board module), and LED on GPIO.5 pin.
    Also, you have button on GPIO.3, and you are puling it down .
    Nowyou have a second button but is is being pulled high.



    Code:
    @ DEVICE PIC12F683, FCMEN_OFF, IESO_OFF, INTRC_OSC_NOCLKOUT , WDT_OFF, PWRT_ON, MCLR_OFF, PROTECT_ON, BOD_OFF
     
     OSCCON   = %01110001         ' Internal 8MHz osc.
     DEFINE OSC 8
     
     ' ======= Common Settings =================================
     ADCON0 = 0
     ANSEL   = 0
     CMCON0 = 7                     ' Comparators off.
     
     TRISIO = 0
     GPIO     = 0
     
     ' ============= PWM Settings =========
     CCP1CON =     %00001100             ' CCP1, PWM mode
     PR2         =     250                         '
     T2CON     =     %00000101             ' TMR2 on, prescaler 1:4
     CCPR1L   = 0
     Speed VAR CCPR1L ' Speed variablefor LED brightness.   
     
     Speed2 VAR BYTE   ' Speed2  variable for fan speed.
     
     ' GPIO.0         ' Empty.
     ' GPIO.1         ' Empty.
     ' GPIO.2         ' LED is connected to GPIO.2 (on board PWM module)
     Btn   VAR GPIO.3   ' Assuming ; the pin is pulled down to GND by 10K resistor, and button pulls it high to Vdd.
     Btn2 VAR GPIO.4   ' Second button. But the pin is pulled high internally. So no external pull up or pull down resistor is required.
                      ' Have a button between this pin and GND (unlike the first button).   
     Fan   VAR GPIO.5   ' Fan motor output. 
     
     
     INPUT Btn2             ' Make the second button pin an input pin.
     OPTION_REG.7 = 0 '
     WPU.4 = 1               ' Enable internal pull up resisitor for Second button pin (GP4)
     
     
     
     PAUSE 10
     Speed2 = 0
     
     ' First button, btn, controls the LED via onboard PWM module.
     ' Second button controls the FAN via software PWM via GPIO.5 output pin and GPIO.4 input pin.
     ' Having the Fan on GPIO.2 and LED on GPIO.5 would be better I think.
     
     Start:
     
              IF Btn = 1 THEN           ' When this button  is pressed, it pulls the pin high.
                     IF Speed + 85 < 256 THEN       ' 0, 85, 170, 255, 0 
                              Speed = Speed + 85
                     ELSE
                             Speed = 0
                     ENDIF
             
             WHILE Btn = 1           ' When this button is being pressed, have no interruption on Fan control.
                             PWM Fan,Speed2,1   
                        WEND
         ENDIF
         
         IF Btn2 = 0 THEN           ' When this button  is pressed, it pulls the pin low.
                     IF Speed2 + 85 < 256 THEN       ' 0, 85, 170, 255, 0 
                              Speed2 = Speed2 + 85
                     ELSE
                             Speed2 = 0
                     ENDIF
             
             WHILE Btn2 = 0                 ' When this button is being pressed, have no interruption on Fan control.
                             PWM Fan,Speed2,1    
                         WEND
         ENDIF
         
         
         PWM Fan,Speed2,1   ' Always drive the fan output.  
         
         
     GOTO Start
     
     END
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    You are my hero. I've yet to try it but I really appreciate your help. Thanks

  8. #8
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    I have a couple of questions I hope you'll answer so I can understand what's going on better.

    Why is it important to put the fan on gpio.2? I realize it's the "on-board pwm module" but what does that buy you in either performance or code simplicity? Why couldn't I put it on any pin like the LED?

    Why turn all these off: FCMEN_OFF, IESO_OFF, WDT_OFF ?

    Are these two statements redundant:
    OSCCON = %01110001 ' Internal 8MHz osc.
    DEFINE OSC 8


    Thanks for your time and patience.



  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Why turn all these off: FCMEN_OFF, IESO_OFF, WDT_OFF ?
    Since you're using the internal OSC... there's no need to use the internal/external switchover feature, same goes for FailSafe clock.... check out the datasheet for the whole set of crispy and ... obscure... details

    WDT_OFF? It's really up to you to decide, I tend to left it to ON... but, it's always case by case... still the datasheet will provide you some detail about the benefits of WDT to ON.

    Are these two statements redundant:
    OSCCON = %01110001 ' Internal 8MHz osc.
    DEFINE OSC 8
    No they're not, the OSCCON line specify the OSC speed to the PIC, while the DEFINE OSC specify the OSC Speed to the compiler.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Got the program and got the circuit set up. It seems to work well with LEDs but feeding the output to a FET, at least the FET I'm using, won't trigger the FET.

    I looked at it with a scope and I'm getting barely over a 2V Peak signal. I assumed I'd get 5V square wave pulses spaced to get the right voltage from the PWM module. Did I do something wrong or do I need to find another FET?

    I do appreciate the help and your patience.

  11. #11
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    What FET?
    MosFet?

    Need to see the circuit anyway.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  12. #12
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Ok forget about the FET and talk to me about the PWM signal. Should it be 5V pulses or 2.2V pulses? If 5V, what do I need to set to make it be that level?

  13. #13
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Using Sayzer's code, attached, I've been able to control an LED as desired.

    Coupling that to a larger current drawing device is a problem. Of the schematic page provided, the top drawing has pulses at GP2 to the MOSFET of about 2.3 volts and won't power on the fan. I thought once I figured out how to get the pulses up to around 5 volts the FET would turn on and I'd be golden.

    The lower schematic adds R1 and R2 so I get around 4.8 volt peaks with varying RMS values but even with the larger pulse size it still won't turn on the fan. I felt really stupid after I figured that one out.

    I can connect the gate to +5V and it will turn the FET on and drive the fan. Although I don't think it's bringing the fan up to full speed.

    So what in hell am I missing? Why won't the chip drive the FET to turn on the fan?
    Attached Images Attached Images  
    Attached Files Attached Files

  14. #14


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Check PWM freq,
    Name:  Capture19.JPG
Views: 7934
Size:  73.3 KB
    If too high, the inductance of the motor affects operation, add diode across motor pointing up, returns energy to motor when MOS FET switches off. Hardware PWM freq only goes so low. May need INT to PWM or do it in basic.
    The ckt shows P-channel MOS but your N-channel setup is ok.

    Don

  15. #15
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    I didn't have a coil so I tried the cap across the motor and voila!

    Thanks.

    I still can't get the fan to do three speed steps it's either on or off. Any ideas?

  16. #16
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Not all fans can be controlled by PWM.

    Try your fan directly with 5V power supply, and see if it runs.
    Most likely, it won't run at 5V.
    Increase the voltage and see at what voltage it will start running.

    If it starts running at around say 10V ~ 11V, then you will have to change your fan, which most probably is controlled by an internal electronic controller.

    Try your circuit with a simple traditional DC brushed motor and see if your PWM works.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  17. #17
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    You are so smrt. I mean smart.

    I assumed a DC fan would run slower at half of rated voltage and slower still at quarter of rated voltage. When I tried your test on my test fan, sure enough it wouldn't run on 8 volts and labored significantly on 10.

    I've yet to try the PWM on a real DC fan without a speed controller but I'm sure it will work just fine.

    Thanks.

  18. #18
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,961


    Did you find this post helpful? Yes | No

    Default Re: PWM fan speed control with a 12F683

    Moved from Schematics.

    Robert

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts