Page 2 of 6 FirstFirst 123456 LastLast
Results 11 to 20 of 53

Thread: Auto brightness with PIC (PWM) and LDR for Lilliput 701 LED Backlit

  1. #11
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266
    Fixed the code, added fading when going into standby mode and corrected some errors:

    Code:
    ;LED Backlight PWM program V3 with instantaneous fading
    ;Written for Mazda3 CarPC by Tim Stocker
    ;
    ;Output 2 - LED Backlight
    ;Input 4 - LDR with 10Kohm Resistor
    ;Input 0 - Power-on V+ from monitor (9.45V = 65 w/100kOhm & 33.3kOhm voltage divider)
    
    
    symbol LED = 2
    symbol LDR = 4
    symbol PWR = 0
    
    setfreq m8								;overclocking to 8MHz
    
    Startup:	
    				
    	readadc PWR,b3		
    	if b3<50 then Standby				
    	readadc LDR,b1						
    	if b1>=160 then Sunny					
    	if b1>80 and b1<160 then Dusk
    	if b1<=80 then Night
    	goto startup
    
    Sunny:
    
    	readadc PWR,b3
    	if b3<50 then subsunny
    	pwmout LED, 86, 275
    	readadc LDR,b1
    	if b1<=160 then DES_Sunny_to_Dusk
    	goto Sunny
    	
    subsunny:
    	b4=255
    	goto Standby
    
    
    DES_Sunny_to_Dusk:
    
    	readadc PWR,b3
    	if b3<50 then subsunny
    	b2=255
    	for b2 = 255 to 100 step -1
    	   pwmout LED, 88, b2
    	   pause 15	   			
    	next b2
    	goto Dusk
    	
    
    ASC_Dusk_to_Sunny:
    
    	readadc PWR,b3
    	if b3<50 then subdusk
    	b2=100
    	for b2 = 100 to 254
    	   pwmout LED, 88, b2
    	   pause 15	   			
    	next b2
    	goto Sunny
    
    Dusk:
    
    	readadc PWR,b3
    	if b3<50 then subdusk
    	pwmout LED, 90, 100
    	readadc LDR,b1
    	if b1>=180 then ASC_Dusk_to_Sunny
    	if b1<=80 then DES_Dusk_to_Night
    	goto Dusk
    	
    subdusk:
    	b4=100
    	goto Standby
    		
    ASC_Night_to_Dusk: 
    
    	readadc PWR,b3
    	if b3<50 then subnight
    	b2=20
    	for b2 = 20 to 100
    	   pwmout LED, 92, b2
    	   pause 20	   			
    	next b2
    	goto Dusk
    
    DES_Dusk_to_Night:
    
    	readadc PWR,b3
    	if b3<50 then subdusk
    	b2=100
    	for b2 = 100 to 20 step -1
    	   pwmout LED, 92, b2
    	   pause 20	   			
    	next b2
    	goto Night
    	
    Night:
    
    	readadc PWR,b3
    	if b3<50 then subnight
    	pwmout LED, 94, 20
    	readadc LDR,b1
    	if b1>=100 then ASC_Night_to_Dusk
    	goto Night
    
    subnight:
    	b4=20
    	goto Standby
    	
    Standby:
    
    	b2=b4
    	for b2 = b4 to 0 step -1
    	   pwmout LED, 86, b2
    	   pause 5	   			
    	next b2
    	do
    	 pwmout LED, off
    	 readadc PWR,b3
    	loop until b3>50
    	goto Startup
    Love

  2. #12
    Constant Bitrate
    Join Date
    Mar 2010
    Posts
    159
    I just read the datasheet on the PICAXE and it looks easier to use than I thought.
    I believe I can read your code and figure out how it's working. This is going to be a fun project.
    I'm off to place an order for some of these things.
    Thanks again,
    davidk

  3. #13
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266
    It's extremely easy to use. I will probably add more notes to my code so it's easier to understand.
    Love

  4. #14
    Raw Wave
    Join Date
    Nov 2009
    Posts
    2,119
    Good stuff Reegler!

    Though its a big jump from circuitry etc, it is the simplest way to do this stuff. Gone are the days when non-PWM techniques could be used. (Though I'd use PWM for efficiency on incandescents.... and necessity for flouros.)


    So many want a vehicle's lighting to be soft start (brighten), then on, then dim after a delay; controlled by door or unlock or remote start but overridden by headlights or....
    The logic alone is almost easiest with a PIC, but add the up/down dimming....
    Try that with circuitry!


    Now, will you add a capacitor to smooth out the PWM voltage?
    (That's a joke. I'm just crapping on an old experienced dude that though that; and that one LED string can rob current from another etc...)

  5. #15
    Constant Bitrate
    Join Date
    Mar 2010
    Posts
    159
    Damn these little things have a lot of commands to learn!
    I was going over your code and I think I've got it sorted. It is pretty simple I guess.

    One question I have is how you measured the voltage for the LED's at ~9 volts? Just put a meter on them while still hooked up to the display?
    Then just feed them that voltage using PWMOUT? Are you using a current limiting resistor? Again, measure the draw while still hooked up to the display?
    If the display is feeding them PWM power, do you get somewhat accurate readings?

    Thanks
    davidk

  6. #16
    Raw Wave
    Join Date
    Nov 2009
    Posts
    2,119
    PWM is independent of voltage.
    It opens and closes the circuit, ie, turns the supply on & off.
    The amount of on time to total time (on & off) is the duty cycle - eg, 1 part on to 9 parts off = 10%.

    For LEDs that should mean about 10% of "full on" brightness. (Though our eyes may perceive it more differently to 10% than the LED might.)


    To dim a 12V (string of) LEDs that might have a 100 Ohm series resistor, normally the PWM would merely be added - ie, between ground and the -ve (Kathode) of the lowest LED.

    If it's a 20mA string, the 100R will drop 2V and the LEDs the rest (ie, 10V).
    By switching on & off quickly, the LEDs still see the full 10V, but only get partial current as averaged out over time. (That is why PWM works on LEDs & flouro lamps etc that require a certain voltage to operate - the voltage they see does not drop.)


    In practice, LEDs can tolerate much higher peak currents - eg, a 20mA LED might tolerate 200mA at a 5% duty cycle or max of 10mSecs.
    Because of the way our eyes work, we can use that to reduce electrical power used for the same LED brightness.
    It also means you can omit resistors - ie, by limiting the peak pulsed current to what the LED can tolerate, run a 2V LED from a 12V supply without any current limiting resistor.


    But to confuse the above with average voltages and currents is a no-no, although "average current" is valid - but not average voltage - at least from the POV of the LEDs etc.
    As I explain it, WHILST the LED is on, it only sees its full voltage so it is happy. (If it gets zero volts, it is off, dead, deceased, non-compusmentus etc so it doesn't care.)
    Averaged over time, we see it with its average current, hence dimmer.


    And if you understand the above, you might understand my joke about using a cap to filter out the PWM voltage. That reduces it to an analog dimmer which will not work (linearly) with CFLs, flouros, LEDs etc.


    And DMMs won't work well without some smoothing filters etc. They are likely to sample at 0V or 10V or their sample period overlaps both and gets something in between.
    True-RMS may read ok, but PWM is technically DC not AC, though it is also equivalent to AC with a DC offset (eg, +5V with +/-5V squarewave), and meters can sense differently (eg, thru diodes, or thru a series cap...)
    Analog meters should give an average reading. (And DMMs with analog displays or bargraphs may not be analog!)
    Last edited by OldSpark; 03-23-2011 at 01:08 AM. Reason: spelink; And DMMs...

  7. #17
    Constant Bitrate
    Join Date
    Mar 2010
    Posts
    159
    Thanks OldSpark,
    I do get it (and the joke about the capacitor). I just wasn't sure.

    I've got a 701 display as well so, since he's got it working, I can just follow his lead on voltages.

    I'm pumped to try these little PIC's. I can see many places I could use them. The only thing that will limit me is my programming ability (or lack there of).
    Luckily my son is at college for programming

    Thanks,
    davidk

  8. #18
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266
    Wow... what he said

    The original LED backlight driver was outputting 9.46V so I knew that was about the brightness I could go. I started out with the circuit hooked up to regular LED with a 220Ohm resistor and just played around with the commands and dimming. Then I hooked it up to the backlight and started low so, for example. "pwmout LED,80, 100" and then went up from there. It was a lot of trial and error and that is why I started with the regular LED. Also I entered EVERYTHING into excel, huge help. It allows me to interpolate if needed and just to have all the info is awesome.

    I understand your "joke" because using the cap essentially negates the PWM's purpose but I do use Caps for when I use PWM for fans in conjunction with my Fusion Brain.

    My dilemma now is where to put the LDR so that it is not easily affected shadows and headlights.

    Also there has to be a better way to go into standby mode. I know the "setint" command which just activates a interrupt command but I'm not sure how to interface that with an ADC input so that if LDR<50 then low, if LDR>=50 then high. The problem is that the PIC is reading the voltage the monitor was originally supplying to the backlight and using that to determine whether to go into standby mode but is slowing degrades from 9.5V to 0V in about 2 seconds so it's not instantaneous. I'm currently using 256 bytes out of 256 bytes so I have no more room.

    ---------- Post added at 08:11 AM ---------- Previous post was at 08:07 AM ----------

    Check out their forum for more questions and info. It's pretty much where I went for my questions! http://www.picaxeforum.co.uk
    Love

  9. #19
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266
    Sorry to keep bugging everyone with code updates but it annoys me that I didn't do it right the first time

    The countdown should be to 1 not 0 as when it was zero it was shooting to 255 before turning off the PWMout command.

    Code:
    Standby:
    
    	b2=b4
    	for b2 = b4 to 1 step -1
    	   pwmout LED, 86, b2
    	   pause 5	   			
    	next b2
    	do
    	 pwmout LED, off
    	 readadc PWR,b3
    	loop until b3>50
    	goto Startup
    Love

  10. #20
    Raw Wave
    Join Date
    Nov 2009
    Posts
    2,119
    Alas, the ever present bugs....
    The constant improvement...

    LOL - I love it! I hope you do too. Programming is a female canine when first starting - a classic case of needing to know everything before you understand anything (ie, a steep learning curve...).

    But then things start to fall in place...
    And then often the addiction....


    IMO the best/easiest learning is tactical - PICk a simple project and do it. (It's best with others that know the same 0.0024% that you know because 2 or 3 time 0.0024% is about 85% there...)

    Later comes the revision of those other useless, complex & crappy commands/instructions you didn't use or understand - but all of a sudden they make sense.
    Or the brainstorm that conjures radical & simpler or shorter coding... (Unlike a final year digital lecturer that used successive addition or subtraction to multiply or divide numbers.. I wonder why he taught instead of a real job....? (LOL!!))


    But it sounds like you have jumped the biggest hurdle - the initialisation of a PIC design.
    From now on, you will probably solve ANY logic combination if inputs or actions with a PIC - even if the logic could be done with a few diodes or gates... (Why? 'cos later mods & complexities are simply programming; no PCBs or wiring or components and burnt/soldered fingers!)

    But as it is, these "simple" (NOT!) dimmer applications are perfect for PICs. Any electronic solution becomes very complex. Geez - I can even convert ordinary mechanical car-ignition distributors to sequential timing using a simple $2 chip - but not an up/down delayed dimmer....


    And if you get the PWM cap as a joke, you are about 20 years ahead of an "experienced" dude that just didn't get it (the joke being his/her insistence that a cap was needed...) - and we were talking LEDs - not motors....


    Congrats to you both!

Page 2 of 6 FirstFirst 123456 LastLast

Similar Threads

  1. Replies: 53
    Last Post: 05-13-2010, 04:57 AM
  2. Replies: 4
    Last Post: 03-21-2010, 06:20 AM
  3. Lilliput 629 or 702? - 2008 model LED backlit?
    By FastRX8 in forum LCD/Display
    Replies: 5
    Last Post: 09-05-2008, 10:15 AM
  4. lilliput auto-on mod and changing brightness
    By warnockm in forum LCD/Display
    Replies: 5
    Last Post: 01-28-2005, 05:02 PM

Bookmarks

Posting Permissions

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