Fade out an LED using PWM?


Closed Thread
Results 1 to 28 of 28
  1. #1
    RossW's Avatar
    RossW Guest

    Question Fade out an LED using PWM?

    I'm trying to code a circuit that will gradually 'fade' out an LED. This is an illusion, mind you, as naturally and LED is either high or low. Someone mentioned that they can simulate this using a 5 channel PWM circuit, but I haven't a clue what they're talking about.

    Does anyone have any ideas, or even better, some sample code?

    Cheers,
    Ross

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello RossW,

    Ross>>I'm trying to code a circuit that will gradually 'fade' out an LED. This is an illusion, mind you, as naturally and LED is either high or low. Someone mentioned that they can simulate this using a 5 channel PWM circuit, but I haven't a clue what they're talking about.

    Does anyone have any ideas, or even better, some sample code?<<

    Well a PWM just varies the width of a pulse between 0 and 256 (256 being steady output). And the PWM can work while other things are going on in your chip...

    You can simulate a PWM, or you can use a PWM... A easy way to make a dimmer, is to vary the number of times the 5 volts is applied to the light.

    LED var byte.
    Counter var byte
    Counter2 var byte
    Counter3 var bye


    For Counter= 256 1 step -1

    For Counter2=1 to 256
    For Counter3=1 to 256
    LED high 'turn on LED
    Pause Counter 'wait a while
    LED Low 'turn off LED
    Next counter3
    Next counter2

    Next Counter


    Though I have not check this out, I think it will work for you.

    It is based upon the principal that your eye is not as fast as we wish. You can change the variables to words and save some code.. and get rid of a for-next statement. I am sure there are other ways to accomplish this... but my mind is tired, and this was the easiest way I thought of off the top of my head.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    CBUK's Avatar
    CBUK Guest


    Did you find this post helpful? Yes | No

    Default

    when im in the office ill check with the book, but theres an actual pwm command that does it all for you, im looking to do the same but with RGB leds (hence my posting on the 3 hardware pwm pics) to fade through the rainbow... i shall try an example shortly.

    Chris

  4. #4
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    Dwayne - I'll try that out tonight.
    Chris - I'd love to see your sample code!

    Ross

  5. #5
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    Dwayne,

    The LEDs do not appear to be 'fading out' - in fact, they appear to be steady on. Here's the code exactly as it's compiled:

    LEDs VAR BYTE
    Counter VAR BYTE
    Counter2 VAR BYTE
    Counter3 VAR BYTE

    LEDs = 3 'Light RB0 & RB1 pins
    TRISB = 0 'Initialize all pins on PORTB to output

    Loop:

    For Counter = 255 TO 0 STEP -1
    For Counter2 = 0 TO 255
    For Counter3 = 0 TO 255
    PORTB = LEDs
    Pause Counter 'wait a while
    PORTB = 0
    Next Counter3
    Next Counter2
    Next Counter

    Pause 3000
    GoTo Loop

    I think part of the problem is that there is no pause after going low, so they never appear to be off. I don't understand why it doesn't finish the 3 loops and exit with the LEDs off, wait 3 seconds, and then start again?

  6. #6
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Ross,

    there is an easy way to achieve that "fading effect"
    even on a PIC with no Hardware PWM, use Software PWM:

    Duty VAR BYTE
    Cycle VAR BYTE
    LED VAR PortB.1

    ' changing the value for "Cycle" will change the time it takes
    ' to fade from 100% to 0%

    Cycle=10

    Loop:
    For Duty=255 to 0 Step -1
    PWM LED,Duty,Cycle
    Next
    Pause 3000
    GOTO Loop


    rgds
    Last edited by NavMicroSystems; - 5th August 2004 at 11:02.

  7. #7
    CBUK's Avatar
    CBUK Guest


    Did you find this post helpful? Yes | No

    Default

    yes using the PWM command is by far the better way to get pulse width modulation without the worry for the individual for next loops...

  8. #8
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello RossW,

    RossW>>The LEDs do not appear to be 'fading out' - in fact, they appear to be steady on. Here's the code exactly as it's compiled:<<

    I am in the process of moving, so I have no test equipment to set up... but I htink tonight I will have some stuff up and running. I am answering you from my work place <g>.

    What that does, is simulate a PWM... I could have a line or two out of place. What I will do, I try it at home and send you the code...

    I may have a line or two out of position. Like I said before, I had not checked the program out, because I typed it in at work. But I will try to get it going for you. It works great with chips that don't have PWM, or need that PWM for something else...

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  9. #9
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Originally posted by Dwayne
    ....But I will try to get it going for you. It works great with chips that don't have PWM, or need that PWM for something else...
    Dwayne,

    have I missed somethig?

    why re-invent the wheel?

    If there is no PWM-Hardware or the PWM Port is already in use
    you can use PBP's PWM command.
    (see my example)

    However, HPWM would be the best choice as it runs in background.

    I mean if you would like to display something on an LCD
    you would use the PBP LCDout command wouldn't you?

    Or would you set the LCD-Bits individually and clock the LCD manually?

    best regards

    Ralph

  10. #10
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Ralph,


    Ralph>>have I missed somethig?

    why re-invent the wheel?<<

    ROFL!! No my friend... *I* missed something...I was so focused on the PWM on the chip. I was thinking HPWM the whole time...

    Anyhow... I like to reinvent the wheel <g> Makes my life more complicated.

    I was sitting at my desk here, and figured out why that example did not work...I needed a "0" pause that was exactly opposite of the "1" .

    Loop
    High LED
    Pause Length
    Low LED
    Pause 256-Length
    goto Loop.


    Ralph >>If there is no PWM-Hardware or the PWM Port is already in use you can use PBP's PWM command.
    (see my example)<<

    Superb example... Just caught me a little brain dead...<g>

    Ralph >>I mean if you would like to display something on an LCD
    you would use the PBP LCDout command wouldn't you?<<

    You really don't want the answer to this do you???<g>
    No, I do not use the LCDout command to display and control my LCD.

    Ralph>>Or would you set the LCD-Bits individually and clock the LCD manually?<<

    You asked.... Yes.. I do.


    Your friend,

    Fred Flinston <g>


    Thanks Ralph.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  11. #11
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    How do I find out if my chip has hardware pwm? I'm thinking of using a 12F629 or a 16F628 ...

  12. #12
    CBUK's Avatar
    CBUK Guest


    Did you find this post helpful? Yes | No

    Default

    find the data sheet, and it'll tell you what special functions the PIC's operate. with hardware PWM you assign the HPWM command to one of the hardware pins (again specified by the manufacture's data sheet)
    the 16F877 of which is one of my common use pics has 2 hardware ports.

    Chris

  13. #13
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Rossw

    Rossw>>How do I find out if my chip has hardware pwm? I'm thinking of using a 12F629 or a 16F628 ...<<

    The Data sheet will tell you...

    The 16F628 has one on Port B3... Same as the 16F648A

    Dwayne

    PS. With the Built in PWM, your chip and be doing other functions as the chip is outputting the train.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  14. #14
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Dwayne,

    we are a bit off topic but

    you should immediately delete your installation of PBP from your harddisk and shred the manual.
    (REAL men don't read instructions!)

    Go assembler or even better delete that MPASM as well and use a HEX editor to create your Hex-Files directly.

    This way you don't have to wait for the compiler and assembler to finish, your hex file is immediatly ready to be burned onto the PIC !

    This reminds me of my very first PROM-programmer
    ( No Joke!)
    it consisted of a socket for the PROM, a DIP-Switch, a Push-Button and a Battery.

    Handling was pretty simple:
    Use the DIP-Switches to set the Bits and use the Button generate a "PROGRAM" Pulse

    This Type of Programmer can certainly be modified for use with a PIC.

    So you do not even have to struggle with your Programmer Software, just delete it too, it is no longer needed!

    Having said this, you don't even need a HEX-Editor, burn the Bits directly to the PIC using my programmer!

    And you gain a lot of free disk space.

    But do you really need PICs and a PC?

    a bunch of relays would do the job as well and if you like it "hitec" I still have some tubes in my cellar I could offer.

    *LOL* *ROFL*

    Ralph
    Last edited by NavMicroSystems; - 5th August 2004 at 17:49.

  15. #15
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Ralph,

    Ralph>>we are a bit off topic but<<

    Hey! its about Micro-Chips right??

    Ralph>>you should immediately delete your installation of PBP from your harddisk and shred the manual.<<

    Hu humm.. That manual should be big enough to roast a hot dog. Best tasting 200 dollar hot dog I have ever had...


    Ralph >>This reminds me of my very first PROM-programmer
    ( No Joke!) it consisted of a socket for the PROM, a DIP-Switch, a Push-Button and a Battery.<<

    Yes!! Yes!! Did you include the UV light??? that takes 20 lousy min to erase your chip to zelch?

    Ralph>>Having said this, you don't even need a HEX-Editor, burn the Bits directly to the PIC using my programmer!<<

    My hearts a beating...Where is it??? <chuckle>

    Ralph>>And you gain a lot of free disk space.<<

    My 8088 4.77mhz IBM hercules graphic card and 15 meg Hard drive is jumping for joy.... CoCo RS computer coughed, My CPM burped. My VIC20. rolled over. Now I am left with my LIttle TI99A

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  16. #16
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Ralph,

    Ralph>>we are a bit off topic but<<

    Hey! its about Micro-Chips right??

    Ralph>>you should immediately delete your installation of PBP from your harddisk and shred the manual.<<

    Hu humm.. That manual should be big enough to roast a hot dog. Best tasting 200 dollar hot dog I have ever had...


    Ralph >>This reminds me of my very first PROM-programmer
    ( No Joke!) it consisted of a socket for the PROM, a DIP-Switch, a Push-Button and a Battery.<<

    Yes!! Yes!! Did you include the UV light??? that takes 20 lousy min to erase your chip to zelch?

    Ralph>>Having said this, you don't even need a HEX-Editor, burn the Bits directly to the PIC using my programmer!<<

    My hearts a beating...Where is it??? <chuckle>

    Ralph>>And you gain a lot of free disk space.<<

    My 8088 4.77mhz IBM hercules graphic card and 15 meg Hard drive is jumping for joy.... CoCo RS computer coughed, My CPM burped. My VIC20. rolled over. Now I am left with my LIttle TI99A

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  17. #17
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    So if I want to fade in, would I use:

    For duty = 0 to 255
    .
    .
    .


    ??

  18. #18
    CBUK's Avatar
    CBUK Guest


    Did you find this post helpful? Yes | No

    Default

    indeed!

    or use the seed command and a 255 limit loop to give a random brightness

  19. #19
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    Seed command? What's that? I could really use something that gives random 'brightness' for another aspect of this project.

  20. #20
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    What Chris wants to tell you is:

    use the RANDOM command

    See Manual Section 5.60

    example:

    RND VAR BYTE
    LED VAR PortB.1


    Loop:
    RANDOM RND
    PWM LED,RND,1000
    GOTO Loop

    rgds

  21. #21
    CBUK's Avatar
    CBUK Guest


    Did you find this post helpful? Yes | No

    Default

    i think you will find there is also a seed command that is used in PBP ive used it and cannot determine the difference between that and the random command... apart from about 2 letters hehe

  22. #22
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    @ CBUK

    would you please give RossW (and all others) a reference to the Manual and a code example.

    And please check your PM
    Last edited by NavMicroSystems; - 5th August 2004 at 23:04.

  23. #23
    CBUK's Avatar
    CBUK Guest


    Did you find this post helpful? Yes | No

    Default

    with reference to the manual its just under seed, in alphabetical order as is the whole manual, or do i have a different version. also if you check the 'help' from PBP it gives the same documentation with an example. i shall sort page numbers out in the office friday.
    and my PM enlighten me to the way this forum works?

    ta

    chris

  24. #24
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    @ CBUK

    ???
    Last edited by NavMicroSystems; - 5th August 2004 at 23:27.

  25. #25
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Rossw

    Here is the Code that I promised you... But, like Ralph said... PBP has a built in function, and using my code is like re-inventing the wheel... <g> But I promised it to you anyhow.... It is cycling just like the PWM...different timing, but principal is the same... I tested it on a LED, and the LED cycled... you may want to change the parameters to allow the LED to go all the way out...

    @ DEVICE PIC12F675,INTRC_OSC_NOCLKOUT,WDT_ON,PWRT_OFF,BOD_O N,PROTECT_OFF,CPD_OFF,MCLR_OFF
    ANSEL=%00000000
    CMCON=%00010111
    TRISIO=%00011000
    ADCON0=%00000000
    Counter var byte
    Counter2 var word
    Loop:
    For Counter=0 to 255
    For Counter2=0 to 62
    GPIO.1=1
    Pauseus Counter
    GPIO.1=0
    pauseus 255-Counter
    Next Counter2
    Next counter
    GPIO.0=1
    GPIO.0=0
    Goto Loop
    end


    If you want to use the PWM on your 628... here is the code for it...It has some extra trash in it... but who cares <g>


    C1 var byte
    TrisA = %11111111
    TrisB = %00000000
    PortB = %0
    PR2= $003F
    CCPR1L = 0
    CCP1CON = %00001100
    TRISB.3 = 0
    T2CON = %00000110
    'I BYTE
    CCPR1L=0
    PAUSE 5000

    LOOPIT:
    For c1=0 to 60
    CCPR1L=C1
    PAUSE 1000
    Next C1
    GOTO LOOPIT
    END
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  26. #26
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    NavMicroSystems - it worked! With cycle = 4 I get the exact fade out appearance I was looking for. I tried the 'fade in' with For duty = 0 to 255 and that works beautifully, too.

    I'm so glad I bit the bullet and got PBP - this makes programming chips so much easier than assembly.

  27. #27
    RossW's Avatar
    RossW Guest


    Did you find this post helpful? Yes | No

    Default

    Dwayne - in the second bit of code you provided, is that using a built-in hardware pwm on the 628? I'd like to try using a hardware pwm, too, to compare the differences.

    Ross

  28. #28
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Ross,

    to use HPWM on the 16F628 your LED must be connected to RB3
    (This is the only Hardware PWM Port on the 628)

    Duty VAR BYTE
    Freq VAR Word
    LED VAR PortB.3

    ' changing the value for "Duty" will change the brightness
    Duty=127 ' about 50% Brightness

    Freq=1000 '1 kHz


    HPWM LED,Duty,Freq


    The difference is once you have initialized in runs forever in Background while your PIC is doing other Things.

    See PBP Manual Section 5.29 for reference.

    regards

    Ralph

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. Free Project - 245 LED Display
    By T.Jackson in forum Code Examples
    Replies: 221
    Last Post: - 16th August 2009, 04:59
  3. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  4. LCD will not start
    By btaylor in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 24th May 2007, 02:30
  5. LED fade without PWM command
    By Nick in forum mel PIC BASIC
    Replies: 7
    Last Post: - 29th June 2005, 20:56

Members who have read this thread : 1

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

Posting Permissions

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