### Author Topic: Calculating a resistor value to use PWM  (Read 5594 times)

#### soggybag

##### Calculating a resistor value to use PWM
« on: April 12, 2009, 10:58:21 PM »
Not sure if the subject is getting the idea across. I have a Microcontroller controlling an LED. I'm using PWM to control the brightness of the LED. I'm putting a range of numbers into the PWM from 0 - 255, which should represent a range of 0 - 100% PWM.

The system is working fine, but I'm thinking there must be an optimum resistor value to use to get the maximum range of brightness out of the LED. Of course there's always the trim pot and ear method. But I was thinking there must be a way to come up with a good number from the data sheet.

I'm planning the use the VTL5C9 or 10. The chip is the avr attiny13. I'm guessing it puts out 5V at 100% PWM.

After thinking about this, I'm realizing there is also the variable of the application. The range of resistance that is useful for the application may not be the full range of resistance for the LDR.

Tuning by ear may be the best choice...

#### soggybag

##### Re: Calculating a resistor value to use PWM
« Reply #1 on: April 13, 2009, 10:29:32 AM »
Should PWM pins use any special filtering. Right now I have the pin running an LED/LDR combo which smooths things out. In general what's the best practice?

#### earthtonesaudio

##### Re: Calculating a resistor value to use PWM
« Reply #2 on: April 13, 2009, 02:26:26 PM »
You want the resistor value that will allow maximum current to the LED without burning it up.  I can't see your datasheet but most LEDs like 15mA max or something.  So find out what voltage the PWM outputs, and what voltage turns on the LED.  Then use Ohm's law.

If your PWM frequency is higher than 20kHz, you won't need any filtering (LDR's respond somewhat slowly so they kinda act as a filtering element by themselves), but keep in mind that just 'cause you can't hear it doesn't mean it's not there.  The PWM frequency will couple into anything close enough and sensitive enough to amplify it.  You can apply high frequency filtering to your audio circuit, or be really clever and null it out using differential circuitry, etc.  Best bet is to check out the circuit from all exit points with a scope (that means output, input, and power connections) and work until it's under control.

#### soggybag

##### Re: Calculating a resistor value to use PWM
« Reply #3 on: April 14, 2009, 01:23:16 AM »

Looking at the data sheet for the VTL5C9:

I need to find a resistor that will give me the most use out of the part. I'm driving the LED from my microcontroller using the PWM output on one of the pins. I'm not completely sure, but it's my guess that the pin outputs PWM at Vcc (5V). And the pulse width goes from 0 to 100%. So at 100% the pin puts out 5V.

The data sheet for the VTL5C9 shows "LED Forward Voltage Drop @ 20 mA: 2.8V (2.2V Typ.)". Which from my calculation of (5v - 2.8v)/20ma = 110 ohms, seems a little low.

Looking at the VTL5C9 shows on and off resistances for the LDR as "input current 2ma resistance 630 ohms". From this I get 1K1 ohms, which seems to make more sense to me.

#### earthtonesaudio

##### Re: Calculating a resistor value to use PWM
« Reply #4 on: April 14, 2009, 07:13:09 AM »
The next step would be to find out the exact output characteristics of the microcontroller, but it sounds like you're on the right track so far.

#### Transmogrifox

##### Re: Calculating a resistor value to use PWM
« Reply #5 on: April 23, 2009, 03:29:17 PM »
I don't think you'll want to exceed more than several mA, total.  5mA is pretty hot for a stompbox circuit.

The better way to approach this is to first determine the range of resistance you need in the circuit where you're applying this. The datasheet for the VTL5C9 gives you output resistance vs. input current curves, so it's all right in front of you.

The datasheet states 50M-ohms dark, 630 Ohms at 2mA.  Given that and the curve, I personally would use the largest resistor possible.  Because the current vs. output resistance curve generally follows a 1/x shape, you're really wasting resolution on the low-current end (ie high resistance).  This can be vitally important with an 8-bit microcontroller.  For example, a 1-bit change could result in a resistance change of several hundred kilo-ohms. Notice the graph for R vs. input current STARTS at 100uA.  At this point for room temp it's 20k at 100uA.  Full dark being several megohms means that you can achieve a very wide swing over the range of 0-100uA.   You may be better served by limiting your current at 500uA.  Again, it depends how low you need it to go.

To balance this, you really need to know the resistance range you need in a given circuit.

Do keep in mind the low response times of the LDR.  No additional filtering will be required.

You can also improve your resolution by toggling bits on the low end currents.  Some elegant programming could be used to provide yourself with an API (application programming interface) delivering linear transfer between the API input and the LDR resistance.  The internal function of the API call would be designed to cancel out the 1/x effect.
trans·mog·ri·fy
tr.v. trans·mog·ri·fied, trans·mog·ri·fy·ing, trans·mog·ri·fies To change into a different shape or form, especially one that is fantastic or bizarre.

#### David

##### Re: Calculating a resistor value to use PWM
« Reply #6 on: April 28, 2009, 09:10:01 AM »
A phenomenon called "duty cycle" has to be considered also.

#### Transmogrifox

##### Re: Calculating a resistor value to use PWM
« Reply #7 on: May 04, 2009, 01:12:33 PM »
A phenomenon called "duty cycle" has to be considered also.

The term "Pulse Width Modulation (PWM)" could be equally termed "Duty Cycle Modulation" .  Soggybag has demonstrated that he has a good grasp on how to account for the effects of duty cycle.

For your benefit, the term "Duty Cycle" only refers to the amount of time the switch is on compared to how much time it is off.  In other words, DutyCycle=Ton/Toff .
100% duty cycle means the output is turned on all the time and no switching is happening. 50% duty cycle produces your classic square wave.
trans·mog·ri·fy
tr.v. trans·mog·ri·fied, trans·mog·ri·fy·ing, trans·mog·ri·fies To change into a different shape or form, especially one that is fantastic or bizarre.

#### nelson

##### Re: Calculating a resistor value to use PWM
« Reply #8 on: May 07, 2009, 07:09:25 PM »
I don't see why you'd be so bothered about the accuracy of such an interaction when the LDR is so innacurate.

I mean, it seems like using an electron microscope to look at a skyscraper.

IMHO.

Have you tried filtering The PWM and measuring the voltage?

That would be a good indicator of what is feeding the LED.

My project site
Winner of Mar 2009 FX-X

#### ElectricDruid

##### Re: Calculating a resistor value to use PWM
« Reply #9 on: May 13, 2009, 10:24:37 AM »
There's some confusion in some of these replies between the Vactrol's *output* resistance (the LDR) and the *input* resistance (the LED).
The output is irrelevant to the question, though the circuit design needs to take it into account.

The microprocessor is outputting a digital PWM signal, so when it's high, it'll be at +5V (or +3.3V if it's a 3.3V part). So the calculation early on of 110ohms is probably right. If it seems low, that's because 2.8V is very high for an LEDs forward drop. Most likely the Vactrol actually uses two LEDs in series or some such similar trick.

You probably don't need any PWM filtering because the Vactrol's LDR response is likely to be fairly slow. That'll depend on your PWM frequency. If you had a PWM freq of 1KHz, that'd be 1mS between the leading edges of your pulses. If the vactrol has a LDR response time of 5mS, it'll smooth the pulses out quite nicely. If you find you're getting PWM feedthrough on the vactrol output, you could increase the PWM frequency until it goes away.

T.

#### Transmogrifox

##### Re: Calculating a resistor value to use PWM
« Reply #10 on: May 14, 2009, 01:52:07 AM »
The microprocessor is outputting a digital PWM signal, so when it's high, it'll be at +5V (or +3.3V if it's a 3.3V part). So the calculation early on of 110ohms is probably right.

The calculation of 110 ohms is dreadfully wrong and meaningless.  LED's (diodes) if simplified,  are best electrically modeled as a voltage source in series with a small resistance.  This resistance is on the order of a few ohms at most.  Many in the milli-ohm range.

If you applied the full 5V to this thing, you wouldn't get 40mA, you'd get exponentially more current and burn the thing up.

The best way to deal with the LED if you want to take the nonlinear voltage/current characteristics out of consideration is to drive them with a constant current source that you switch on and off with the uC output (ie 1mA on, 0mA off).

That way you can reduce the problem down to LED current vs. vactrol output resistance...which is nicely graphed in the datasheet.
trans·mog·ri·fy
tr.v. trans·mog·ri·fied, trans·mog·ri·fy·ing, trans·mog·ri·fies To change into a different shape or form, especially one that is fantastic or bizarre.

#### ElectricDruid

##### Re: Calculating a resistor value to use PWM
« Reply #11 on: May 14, 2009, 11:02:29 AM »
I may have misunderstood, but I didn't think he was trying to model the LED resistance. Instead, he's looking for the best series resistance to use to get the required LED current. And that's simple enough to work out. The datasheet numbers he gave and R=V/I is all you need. There's 2.2V typical given as the volt drop across the LED, so our series resistor has 5V-2.2V=2.8V across it. 2.8V/20mA = 140 Ohms. Pick the nearest available resistor value from your box of resistors, higher for the safe side, and you'll get approx 20mA through the LED.

Obviously in the circuit, it might turn out that the LDR resistance is too low with so much current, so then you could adjust the resistor value upwards to increase the LDR resistance. That's where sticking a pot of a few KOhms in series with the LED resistor and doing it by ear comes in. At least then you can experiment whilst being reasonably sure that you won't blow up the vactrol.

T.

#### Transmogrifox

##### Re: Calculating a resistor value to use PWM
« Reply #12 on: May 18, 2009, 12:32:55 AM »