Shop OBEX P1 Docs P2 Docs Learn Events
PWM to control LED brightness & contrast — Parallax Forums

PWM to control LED brightness & contrast

Cluso99Cluso99 Posts: 18,069
edited 2011-03-22 16:49 in Propeller 1
Q1.
I have a backlight on an LCD display that can be driven by with current up to 280mA. I will never require this, but perhaps up to 100mA may be required.

What I thought is that I would drive an NPN transistor such as 2N2222A or similar in current limit mode with the LED in the collector to 3V3 (or 5V) and a series resistor in the emitter to limit the maximum current to 100mA. Given the gain in the transistor, if I connect a prop output pin to the base of the transistor, I can turn the LED on.

Now, if I use PWM mode, I should be able to effectively reduce the current by PWM switching the transistor on & off.

For you analog experts, am I on the right track ???

Q2. I want to control the contrast on the LCD. This can be done manually with a 10K pot between 5V and Gnd, forming a voltage divider. The voltage required is well below 3V3. I know I could use a digital 10K pot, but I think I should be able to generate a voltage level using a PWM circuit in the prop.

Am I correct in believing the PWM generates on/off pulses with varying widths? Presuming this, how do I make this a voltage level? I presume that I need a series R and then a cap to ground to filter this. Once again, am I on the right track here???

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-18 06:00
    The voltage (and current) from PWM is still the same, instead of 5V constant it's 5V for varying periods of time, so neither voltage nor current are halved because you're running 50% duty. Someone else can "look at it" otherwise, but that's his trouble.

    You may be able to control LCD contrast using PWM without using an op-amp
    (charging a cap for a steady voltage) because LCDs are slow. Can't hurt to try it, likely different results by rep-rate (freq).
  • bennettdanbennettdan Posts: 614
    edited 2011-03-18 15:14
    I agree with PJ Allen, an OpAmp setup as a DAC is what I have found best for brightness control. At the bottom of this wiki is an example showing how to figure the resistor and cap need.
    http://propeller.wikispaces.com/PWM
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-18 15:40
    It's OK to agree with me, but I posted, "You may be able to control LCD contrast using PWM without using an op-amp." Purely speculative on part, mind, but raw PWM ought to do it. There's likely a rep-rate that'll accomplish this.
    Any bets?
  • bennettdanbennettdan Posts: 614
    edited 2011-03-18 17:25
    PJ Allen
    Sorry I missed a comma in my sentence I was agreeing with you that it might be possiable to do it with raw PWM, also I was telling Cluso99 that what I found best is and OpAmp for brightness control.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-18 17:33
    @bennettdan -- There's one way to find out.
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-03-18 20:12
    Thanks guys.

    For the backlight, what I was thinking is that by switching a transistor on and off (by PWM), where the transistor is set to a fixed current source, would have the effect of providing maximum current (eg 100mA) when PWM=100% (always on). Now, if I output PWM=50% then the transistor is only on 50% of the time, which effectively means 50mA (being 100mA but with pulses). Now I know this is not a linear scale with LEDs but that is fine. So for this mode, I am just switching a transistor on/off to provide current to the LED.

    For the contrast, I require a constant voltage. So I would be using the PWM to provide a voltage output. Therefore this would require a resistor and capacitor to filter the PWM output to make this a voltage instead of a switcing output. Otherwise I would use an I2C digital resistor such as this MCP4018T-103E/L http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=MCP4018T-103E/LTCT-ND from DigiKey for 0.39c ea per 100.
  • kwinnkwinn Posts: 8,697
    edited 2011-03-18 20:45
    Cluso, your approach for the backlight will work. For a 100mA max you would need a 27 ohm resistor between the emitter and ground with a 3.3V signal to the base. For controlling the contrast you can use an RC circuit connected to a pin and produce a control voltage between 0 and 3.3V. The exact R and C depends on the current requirement of the LCD (usually small) and the PWM frequency. I have used a 10K resistor and 0.1uF capacitor with pwm frequency over 1KHz in several circuits.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-18 20:57
    I think you can control the contrast with PWM alone, a lot fewer parts that way
    (no op-amps, bias resistors, caps, or digital pots), but it's your deal.
    It wouldn't be like having to build the Pyramids to find out.
    Nobody has mentioned the Propeller even once. Amazing.
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-03-18 22:42
    A Propeller Chip is presumed on this forum :)

    Kwinn: Thanks for your feedback.
    Actually the constant current circuit should have a series resistor from the prop pin to base of the transistor of say 1K, with 2 series 1N4148 or similar to ground with anode to ground. The resistor should be low enough to ensure the transistor is saturated with minimum beta (gain). This makes a voltage drop of about 1.2-1.4V from base to ground. 0.6V is dropped across the base-emitter junction of the transistor leaving ~0.6V to be dropped across the emitter resistor. So 0.6V & 100mA gives ~6 ohms. The only other consideration is to ensure the LED voltage drop is not too large for the circuit (i.e. you cannot use a 3.2V LED with a 3V3 supply while a 1V6 LED is fine.
  • AribaAriba Posts: 2,690
    edited 2011-03-19 04:14
    Cluso
    Yes you are on the right track(s)!

    But I would make the PWM circuit for the LED brightness a bit different (simpler). Like in the first circuit of the attachment. The max. LED current with 100% on is (~3.2V - Vled) / R2. R1 can be 4.7k.
    You can do it also with a current source like in the third circuit, but why?

    For the contrast an R-C is fine to make a controllable voltage out of the PWM signal. Use something like 1k and 100nF if you generate the PWM with the DUTY mode of a counter.

    Andy
    472 x 143 - 3K
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-03-19 07:04
    Thanks Andy. I thought the RC circuit would be fine for the contrast although I was not sure of values. The first circuit is a little simpler than your last, and simpler again than my circuit that used 2 series diodes from base to ground (anode should be to base, not ground as I said above) which limits the base to ~1.2V. I have seen this in an IR LED transmitter circuit.

    An alternative would be to use a pair of MPC4018 10K digital I2C pots, one in the prop/base/gnd circuit of a transistor. So with only 1 extra pin I could add a pair of I2C devices, one in parallel with the eeprom and the second using an extra SDA data pin.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-19 07:16
    You're over-complicating the job.
    Have fun with "tronyx fer prop-haidz".
  • Toby SeckshundToby Seckshund Posts: 2,027
    edited 2011-03-19 07:38
    I almost got to post a while ago, but then thought " Am I talking **** , again ?"

    I was wondering if there would be any aliasing between a PWM pulsed light and the data/display on the LCD ?

    I USED pwm ON A psu fan recently (AVR, sorry for going off processer) and found that on lower rates I could hear the magnetics whining and when I pushed the rate up, out of range, there was still a ticking. Presumably cogging (and there we are, back on processor again)
  • kwinnkwinn Posts: 8,697
    edited 2011-03-19 10:15
    Cluso99 wrote: »
    A Propeller Chip is presumed on this forum :)

    Kwinn: Thanks for your feedback.
    Actually the constant current circuit should have a series resistor from the prop pin to base of the transistor of say 1K, with 2 series 1N4148 or similar to ground with anode to ground. The resistor should be low enough to ensure the transistor is saturated with minimum beta (gain). This makes a voltage drop of about 1.2-1.4V from base to ground. 0.6V is dropped across the base-emitter junction of the transistor leaving ~0.6V to be dropped across the emitter resistor. So 0.6V & 100mA gives ~6 ohms. The only other consideration is to ensure the LED voltage drop is not too large for the circuit (i.e. you cannot use a 3.2V LED with a 3V3 supply while a 1V6 LED is fine.
    Attachment not found.

    Cluso, yes, of coursre the prop pin driving the transistor base needs a current limiting resistor. I am basing what I said on the attached generic circuit. I have used this as the basis for driving displays for many years and it works very well. You do not need diodes between the ground and base to set the current since the voltage on the base will never go above Vcc (3.3 or 5V) That will limit the current based on the emitter resistor.

    Generally I use it with the display being connected to the unregulated power supply input to reduce the load on the regulator. As you say, leds with high forward voltage drops can not be driven with a 3.3V supply, and even on those that can be driven with a 3.3V supply, the forward voltage of the led needs to be taken into account when selecting the emitter resistor.
  • Clock LoopClock Loop Posts: 2,069
    edited 2011-03-19 11:54
    Considering your not running the leds more than 100ma most of the time. It seems like you could use a single propeller with all or most outputs tied together with current limiting resistors on each pin. Just drive the leds with propeller direct output. The propellers package is rated at 200ma. With 30ma per pin. I would try that just for simplicity first.
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-03-19 14:03
    ClockLoop: Thanks for an excellent suggestion. Normally we don't have pins available, but in this instance I do. This particular circuit is on the third prop of a 3 prop system.

    By using a resistor on each prop pin with the junctions of the resistors going to the LED and the other LED pin tied either to ground or to +V would work. I could just enable the appropriate outputs using a ladder style effect similar to the way the TV & VGA objects get their analog output. I have pins available and it is much easier to use a few resistors because they are easily obtained.
    Of course, in a production environment, 4 resistors would cost more than 1 I2C SOT363 device worth 20c because the loading cost is higher. Then again, you have a sourcing and inventory cost. But the simplicity is a winner!!!
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-21 18:30
    Here's a video I made featuring an LCD with its contrast being
    varied by PWM only.
    No opamps, digipots, or any of that lamer kit.

    http://www.youtube.com/watch?v=qT7tq4rN8LE
  • bennettdanbennettdan Posts: 614
    edited 2011-03-21 20:43
    PJ Allen
    Good job, so u have this comming from just a prop pin?
    Do you have a resistor for current limit?
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-03-21 21:30
    PJAllen
    Nice job. In the dark without backlight, can you see any visible pulsations in the display caused by the PWM ???

    You have proved your point nicely with the video. (perhaps you could tame your comments).

    bennettdan: There should be no need for a series resistor as the LCD contrast is voltage driven.

    Thanks everyone for your help. It seems we can use an LCD with a prop pin with PWM for the contrast, and a few pins with series resistors for the backlight. Quite a simple circuit really !!!

    Has anyone viewed this with IE8 as I could only get FireFox to work (IE complained about not having Adobe Flash, even tho' I downloaded it again).
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-22 16:49
    Right about the Contrast pin, it's high impedance, no resistor necessary.

    I didn't go down past 20pct, that's pretty dark, but you can always take
    it solid LOW (no pulse) for all pixels dark/on. Most of the usable variation
    was 20-60pct, but that was at just one viewing angle.

    I used 1 kHz, no backlight: no pulsations, strobing, etc.
    If one were also to PWM the backlight there might be a combination
    where some heterodyning might manifest.
    Interesting possibility? That'd be easy enough to test out.
Sign In or Register to comment.