Smooth LED fading via PWM - Page 2


Closed Thread
Page 2 of 2 FirstFirst 12
Results 41 to 55 of 55
  1. #41
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Hi Hank,
    No, not really....
    Code:
    Defalt_Config DATA 255
    Will store the value 255 at location 0 in EEPROM and give that location the label or name Default_Config. If you want a value to be stored at location 255 you need to use
    Code:
    DATA @255, 42
    Or possibly (not clear to me how the label thing it works)
    Default_Config DATA @255, 42[code]
    and then when the user changes that value you do
    Code:
    userValue VAR BYTE
    WRITE 255, userValue
    or possibly (still not clear how that "label" works)
    Code:
    userValue VAR BYTE
    WRITE Default_Config, userValue
    Sorry if I'm confusing you...
    /Henrik.

  2. #42
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Ok Henrik...I've just used your 'read the EEprom serially out onto my PC screen' (rather than try to work how to look at the data with the pickit2 itself) - it worked a treat....and certainly alleviates some angst as to "I wonder what's what inside the Eeprom space)

    And I was clearly wrong with respect to getting my user defined byte tucked away to slot 255, this is obviously what I needed...

    Code:
    WRITE 255, default_config_shadow
    (definitely a case of RTFM!)

    Anyway, I've learnt something from this little jaunt - I'm happy :-) (tks Henrik)

    Edit: Oops - just seen your reply (was obviously typing mine while you were replying) ...anyway, the penny has dropped here!
    Last edited by HankMcSpank; - 21st September 2011 at 20:20.

  3. #43
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Hank,
    Something to tuck away in your cranium while working with the EEPROM: It has a limited number of write cycles.

    I can't remember the number of write cycles, but you will NEVER wear it out just by reprograming it. (EDIT: The 18F4620 is rated minimum 100K and typical 1M write cycles)

    But you could wear it out if you repeatedly write a value during the operaitons of your code. So only make the WRITE when the user actually changes the vaule.
    Last edited by SteveB; - 21st September 2011 at 21:30. Reason: Added info on write cycle ratings

  4. #44
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Thanks Steve ...fortunately, this is likely to be a one shot selectable option (ie set & forget) - I doubt it'll ever be updated more than a few times.

  5. #45
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    I have to admit that I haven't read all the posts, but I noticed one thing awhile "up". PLEASE don't drive a LED directly from a PIC pin! LEDs are not 'lamps'. LEDs ARE DIODES! It is CURRENT, not voltage that lights them. Since they are a diode, if you put a slowly rising voltage across them, they (at first) draw no current whatsoever. Then, when they get to the diode forward voltage (which is determined by the technology of the LED), all of the sudden, they light. So, you can have no light at all at 2.2V, and burn them up at 2.3V. If a LED with a forward voltage of 2.3V was connected to a 3V source, it wouldn't last long. So why do they work when you connect them directly to PIC pins? It is because the PIC pins have very small FETs driving them, and they have an "effective" resistance. That is, they cannot supply a huge amount of current. This current-limiting allows both the PIC and the LED to survive.
    You have seen the LED flashlights that have nothing but white LEDs and 3 - AAA batteries. No resistor. The forward voltage of most white LEDs is between 3.1 and 4V (Wikipedia). The 3 batteries in series produce about 4.5V. It all works because batteries are not perfect power sources, they have an Equivalent Series Resistance (ESR) that acts like a resistor in series with the battery. This ESR allows things to work - kind of. But have you ever noticed that while you expected the LED flashlights to work forever - they don't? They fail because the makers don't put in a little extra resistance in the leads of each LED to balance the LEDs and to control the current.

    Back to PICs: The PIC pins outputs are not really resistors. They may act somewhat like resistors, but they are not. The "resistance" of the pin can vary quite a bit over Vcc variations and from device to device and even pin-to-pin. As a result, the results can be unpredictable. Please put a resistor (at least) in series with your LED.
    Charles Linquist

  6. #46
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Quote Originally Posted by Charles Linquis View Post
    The PIC pins outputs are not really resistors. They may act somewhat like resistors, but they are not. The "resistance" of the pin can vary quite a bit over Vcc variations and from device to device and even pin-to-pin. As a result, the results can be unpredictable. Please put a resistor (at least) in series with your LED.
    I agree they're not what we'd deem traditional resistors...but for supply of 3.268V to the (blue) LED, I'm seeing 0.337V dropped across the PIC pin - that equates to a series DC resistance in any language. I take your point about variations (though I'm not seeing it pin to pin). So with the PIC pin V drop, that only leaves about 2.95V for the blue LED (with a rated fwd voltage of about 3.2V) ...there's not much chance a blue LED will blow with just 2.95V across it (the current through it is fairly meagre at that voltage level). Fear not, I appreciate your input (& concern), but seriously...this isn't a production run - this is for my own use (& a couple of mates if my project-ette turns out to my liking) ...simply towards learning about PICs & pushing my own (small!) boundaries...I posted up my earlier findings for interest (wrt actual real world IO pin resistance) as opposed to evangelising to others about going 'resistorless'!

    I do wonder though since 50 ohms is a surprisingly high figure, just how many factor the PIC's pin resistance in when calculating a LED resistor value? (I suspect not many) ...the resistance certainly can't be disregarded becuase certainly with relatively low value resistors normally in play for LEDs, the PIC pin's inherent resistance is gonna severely skew the desired end result.
    Last edited by HankMcSpank; - 22nd September 2011 at 00:53.

  7. #47
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    I generally use 2N7002's for driving things like that. They are great, logic-level N-channel FETs that have a pretty low ON resistance. Cheap too.
    Charles Linquist

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


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Only if using PWM on a PIC pin , you can directly drive a transistor base without a resistor.
    That will save you one component (resistor) and also space on PCB.

    If you use Mosfet with PWM, you can also directly drive the gate without resistor. But this time, you need to have a resistor between the gate and GND. This resistor is not needed if not using PWM.
    Last edited by sayzer; - 22nd September 2011 at 05:23. Reason: typo
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  9. #49
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Quote Originally Posted by Charles Linquis View Post
    I generally use 2N7002's for driving things like that. They are great, logic-level N-channel FETs that have a pretty low ON resistance. Cheap too.
    Since I'm sinking current into the pin, I'm using a common (ie shared by all pins) P Channel Mosfet (with something like .2 Ohm 'on' resistance) & inverted PWM on the ECCP module ....it works well.

  10. #50
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    For any N-Channel small MosFet needs (up to 2A) have a look at the Si2308 too. It is working fine from 4.5 Volts with Rds of 0.192 and Id of 2.1 Amp.


    And the best P-Channel Power Mosfet I have find maybe is the SUM110P04-05-E3. Usually the P-channel have high Rds and low Id specs.

    Ioannis

  11. #51
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    actually, I was mistaken the RDS of my Mosfet is about 0.7 Ohms.

    I bought these P channel Mosfets when CPC Farnell were clearing them out for about 5p each...

    http://www.farnell.com/datasheets/468737.pdf

    I think I bought about 100 ....I couldn't believe it when they arrived...every Mosfet was sealed individually in a 5cm plastic bag, & then inside the plastic bag, each MOSFET was then sealed in it's SMD wrapping! It takes me more time to get to the mosfet than to make my PCB!

  12. #52
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Ok, I missing somethinh fundamental here...wondering if anyone can embarrass me!

    I'm putting some data into eeprom...
    Code:
    data  0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, _
    1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, _
    2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4, _
    4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,8,8, _
    8,8,8,8,9,9,9,9,10,10,10,10,10,11,11,11,11,12,12,12, _
    13,13,13,13,14,14,14,15,15,15,16,16,16,17,17,18,18,18, _
    19,19,20,20,20,21,21,22,22,23,23,24,24,25,26,26,27,27,28, _
    29,29,30,31,31,32,33,33,34,35,36,36,37,38,39,40,41,42,42, _
    43,44,45,46,47,48,50,51,52,53,54,55,57,58,59,60,62,63,64, _
    66,67,69,70,72,74,75,77,79,80,82,84,86,88,90,91,94,96,98, _
    100,102,104,107,109,111,114,116,119,122,124,127,130,133,136, _
    139,142,145,148,151,155,158,161,165,169,172,176,180,184,188, _
    192,196,201,205,210,214,219,224,229,234,239,244,250,252
    note that last value 252....ok, when I read out the contents of eeprom...

    Code:
    Loc000: 000
    Loc001: 001
    Loc002: 001
    Loc003: 001
    Loc004: 001
    Loc005: 001
    Loc006: 001
    Loc007: 001
    Loc008: 001
    Loc009: 001
    Loc010: 001
    Loc011: 001
    Loc012: 001
    Loc013: 001
    Loc014: 001
    Loc015: 001
    Loc016: 001
    Loc017: 001
    Loc018: 001
    Loc019: 001
    Loc020: 001
    Loc021: 001
    Loc022: 001
    Loc023: 001
    Loc024: 001
    Loc025: 001
    Loc026: 001
    Loc027: 001
    Loc028: 001
    Loc029: 001
    Loc030: 001
    Loc031: 002
    Loc032: 002
    Loc033: 002
    Loc034: 002
    Loc035: 002
    Loc036: 002
    Loc037: 002
    Loc038: 002
    Loc039: 002
    Loc040: 002
    Loc041: 002
    Loc042: 002
    Loc043: 002
    Loc044: 002
    Loc045: 002
    Loc046: 002
    Loc047: 002
    Loc048: 002
    Loc049: 002
    Loc050: 003
    Loc051: 003
    Loc052: 003
    Loc053: 003
    Loc054: 003
    Loc055: 003
    Loc056: 003
    Loc057: 003
    Loc058: 003
    Loc059: 003
    Loc060: 003
    Loc061: 003
    Loc062: 003
    Loc063: 004
    Loc064: 004
    Loc065: 004
    Loc066: 004
    Loc067: 004
    Loc068: 004
    Loc069: 004
    Loc070: 004
    Loc071: 004
    Loc072: 004
    Loc073: 004
    Loc074: 005
    Loc075: 005
    Loc076: 005
    Loc077: 005
    Loc078: 005
    Loc079: 005
    Loc080: 005
    Loc081: 005
    Loc082: 006
    Loc083: 006
    Loc084: 006
    Loc085: 006
    Loc086: 006
    Loc087: 006
    Loc088: 006
    Loc089: 007
    Loc090: 007
    Loc091: 007
    Loc092: 007
    Loc093: 007
    Loc094: 007
    Loc095: 008
    Loc096: 008
    Loc097: 008
    Loc098: 008
    Loc099: 008
    Loc100: 008
    Loc101: 009
    Loc102: 009
    Loc103: 009
    Loc104: 009
    Loc105: 010
    Loc106: 010
    Loc107: 010
    Loc108: 010
    Loc109: 010
    Loc110: 011
    Loc111: 011
    Loc112: 011
    Loc113: 011
    Loc114: 012
    Loc115: 012
    Loc116: 012
    Loc117: 013
    Loc118: 013
    Loc119: 013
    Loc120: 013
    Loc121: 014
    Loc122: 014
    Loc123: 014
    Loc124: 015
    Loc125: 015
    Loc126: 015
    Loc127: 016
    Loc128: 016
    Loc129: 016
    Loc130: 017
    Loc131: 017
    Loc132: 018
    Loc133: 018
    Loc134: 018
    Loc135: 019
    Loc136: 019
    Loc137: 020
    Loc138: 020
    Loc139: 020
    Loc140: 021
    Loc141: 021
    Loc142: 022
    Loc143: 022
    Loc144: 023
    Loc145: 023
    Loc146: 024
    Loc147: 024
    Loc148: 025
    Loc149: 026
    Loc150: 026
    Loc151: 027
    Loc152: 027
    Loc153: 028
    Loc154: 029
    Lo1155: 029
    Loc156: 030
    Loc157: 031
    Loc158: 031
    Loc159: 032
    Loc160: 033
    Loc161: 033
    Loc162: 034
    Loc163: 035
    Loc164: 036
    Loc165: 036
    Loc166: 037
    Loc167: 038
    Loc168: 039
    Loc169: 040
    Loc170: 041
    Loc171: 042
    Loc172: 042
    Loc173: 043
    Loc174: 044
    Loc175: 045
    Loc176: 046
    Loc177: 047
    Loc178: 048
    Loc179: 050
    Loc180: 051
    Loc181: 052
    Loc182: 053
    Loc183: 054
    Loc184: 055
    Loc185: 057
    Loc186: 058
    Loc187: 059
    Loc188: 060
    Loc189: 062
    Loc190: 063
    Loc191: 064
    Loc192: 066
    Loc193: 067
    Loc194: 069
    Loc195: 070
    Loc196: 072
    Loc197: 074
    Loc198: 075
    Loc199: 077
    Loc200: 079
    Loc201: 080
    Loc202: 082
    Loc203: 084
    Loc204: 086
    Loc205: 088
    Loc206: 090
    Loc207: 091
    Loc208: 094
    Loc209: 096
    Loc210: 098
    Loc211: 100
    Loc212: 102
    Loc213: 104
    Loc214: 107
    Loc215: 109
    Loc216: 111
    Loc217: 114
    Loc218: 116
    Loc219: 119
    Loc220: 122
    Loc221: 124
    Loc222: 127
    Loc223: 130
    Loc224: 133
    Loc225: 136
    Loc226: 139
    Loc227: 142
    Loc228: 145
    Loc229: 148
    Loc230: 151
    Loc231: 155
    Loc232: 158
    Loc233: 161
    Loc234: 165
    Loc235: 169
    Loc236: 172
    Loc237: 176
    Loc238: 180
    Loc239: 184
    Loc240: 188
    Loc241: 192
    Loc242: 196
    Loc243: 201
    Loc244: 205
    Loc245: 210
    Loc246: 214
    Loc247: 219
    Loc248: 224
    Loc249: 229
    Loc250: 234
    Loc251: 239
    Loc252: 244
    Loc253: 250
    Loc254: 252
    Loc255: 255
    I get 252 as the 2nd to last number....with 255 being the final number (I'm guessing all 1s is a default value of unused eeprom slots?)

    therefore there must be just 255 values in that table, but if I add another value on the end (the 256th slot) ...saya number of 253...

    Code:
    data  0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, _
    1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, _
    2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4, _
    4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,8,8, _
    8,8,8,8,9,9,9,9,10,10,10,10,10,11,11,11,11,12,12,12, _
    13,13,13,13,14,14,14,15,15,15,16,16,16,17,17,18,18,18, _
    19,19,20,20,20,21,21,22,22,23,23,24,24,25,26,26,27,27,28, _
    29,29,30,31,31,32,33,33,34,35,36,36,37,38,39,40,41,42,42, _
    43,44,45,46,47,48,50,51,52,53,54,55,57,58,59,60,62,63,64, _
    66,67,69,70,72,74,75,77,79,80,82,84,86,88,90,91,94,96,98, _
    100,102,104,107,109,111,114,116,119,122,124,127,130,133,136, _
    139,142,145,148,151,155,158,161,165,169,172,176,180,184,188, _
    192,196,201,205,210,214,219,224,229,234,239,244,250,252,253
    it won't compile (compiler says stack over flow)


    So if there are only 255 values being stored in my eeprom, then why won't it let me squeeze in a byte to the final (256th byte) eeprom slot ...rather than defaulting the final value to 255?

    Any ideas?

  13. #53


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Quote Originally Posted by sayzer View Post
    Only if using PWM on a PIC pin , you can directly drive a transistor base without a resistor.
    That will save you one component (resistor) and also space on PCB.

    If you use Mosfet with PWM, you can also directly drive the gate without resistor. But this time, you need to have a resistor between the gate and GND. This resistor is not needed if not using PWM.
    Hi sayzer - I've been working on driving up to 10 LEDs with a BS170 MOSFET and have connected the PIC pin directly to the gate of the 'FET. I had seen a circuit diagram with a resistor connecting the PIC -> gate to end, but then heard that this wasn't needed. What size resistor should I use?

  14. #54
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Not necessary to add resistor to Fet gates.

    Only if you had bipolar transistors.

    Ioannis

  15. #55


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Thanks heaps, Ioannis.

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