EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Driving LED with PWM, Beginner's Question

Started by syr123 February 17, 2009
Hi all!

True beginner here, but was very excited to find this site.  I would
greatly appreciate if someone could shed some light on whether or not I am
going about a project correctly.  Here are the facts:

Using - 

PIC16F690 with an external 20Mhz crystal oscillator, powered by 5V (at
300mA? at least that's what the wall wart says - seems to be about 1 amp
when I measure it).

I am trying to drive an LED with PWM on the CCP1 pin.  I have written an
app that uses 10 bit resolution for the duty cycle, and varies it between 0
and 100%.  This appears to be working correctly - the LED fades from
completely off to rather bright.

I am, however, trying to figure out whether I should have a resistor in
series with the LED or not.

I am powering a blue LED.  Its voltage rating is 4V, the current test is
20mA.  

My best measurements with an oscilloscope and multimeter seem to indicate
that at 100% duty cycle, the PWM pin is outputting 5V at 32.6mA.  I don't
know if this is correct, and would love to even know where in the datasheet
I could look to know what the pins can potentially output - is this called
sourcing, or sinking, a voltage?

Anyway, it seems to me that if:

R = (Vsource - Vled)/I

Then I should have a resistor of size:

R = (5-4)/.02
R = 1/.02
R = 50ohms (nearest greater = 56ohms)

So, my final questions are:

1.  Do the readings I've taken for the PWM pin at 100% duty cycle make any
sense?  How could I find in the data sheet for the PIC16F690 what the pin
can "output"?

2.  I'm trying to get the most brightness possible out of the LED.  If my
readings are all right, it seems like this resistor in series with the LED
is correct for 100% duty cycle.  Do I need it when I'm doing PWM?  What
about at the smaller duty cycles, will this make it very dim?

Any help would be much, much appreciated!

-Syr


On Tue, 17 Feb 2009 17:30:23 -0600, "syr123"
<michael.frederickson@gmail.com> wrote:

>Hi all! > >True beginner here, but was very excited to find this site. I would >greatly appreciate if someone could shed some light on whether or not I am >going about a project correctly. Here are the facts: > >Using - > >PIC16F690 with an external 20Mhz crystal oscillator, powered by 5V (at >300mA? at least that's what the wall wart says - seems to be about 1 amp >when I measure it). > >I am trying to drive an LED with PWM on the CCP1 pin. I have written an >app that uses 10 bit resolution for the duty cycle, and varies it between 0 >and 100%. This appears to be working correctly - the LED fades from >completely off to rather bright. > >I am, however, trying to figure out whether I should have a resistor in >series with the LED or not. > >I am powering a blue LED. Its voltage rating is 4V, the current test is >20mA. > >My best measurements with an oscilloscope and multimeter seem to indicate >that at 100% duty cycle, the PWM pin is outputting 5V at 32.6mA. I don't >know if this is correct, and would love to even know where in the datasheet >I could look to know what the pins can potentially output - is this called >sourcing, or sinking, a voltage? > >Anyway, it seems to me that if: > >R = (Vsource - Vled)/I > >Then I should have a resistor of size: > >R = (5-4)/.02 >R = 1/.02 >R = 50ohms (nearest greater = 56ohms) > >So, my final questions are: > >1. Do the readings I've taken for the PWM pin at 100% duty cycle make any >sense? How could I find in the data sheet for the PIC16F690 what the pin >can "output"?
Without the current limiting resistor, the output port structure itself is limiting the available current. However: Section 17.0 Electrical Specifications Absolute Maximum Ratings ... "Maximum output current sunk by any I/O pin 25 mA Maximum output current sourced by any I/O pin 25 mA Maximum current sunk by PORTA, PORTB and PORTC (combined) 200 mA Maximum current sourced PORTA, PORTB and PORTC (combined) 200 mA "NOTICE: Stresses above those listed under &#4294967295;Absolute Maximum Ratings&#4294967295; may cause permanent damage to the device."
>2. I'm trying to get the most brightness possible out of the LED.
The maximum brightness occurs just once, briefly, before it lets the magic smoke out. It is what it is and it's rated how it's rated.
> If my >readings are all right, it seems like this resistor in series with the LED >is correct for 100% duty cycle.
With the caveat that you're not likely to be able to source all the way up to Vcc, particularly when driving near (or above) the rated current.
> Do I need it when I'm doing PWM?
Yes. Never exceed a device's absolute maximum ratings. There may be instances where doing so is required and desired but if you have to ask, it safe to assume that it's not a particularly good idea. You *might* be able to get a slightly brighter appearance by overdriving the LED at less than a 100% duty cycle but you would have to run the numbers to determine all side effects. Probably beyond the present scope...
> What >about at the smaller duty cycles, will this make it very dim?
The perception of brightness is roughly a log function. A 10-bit PWM will do nicely for ten distinct brightness levels. The current is what the current is and you do need a resistor. -- Rich Webb Norfolk, VA
Hi Rich -

Thanks so much for your prompt and informative follow up.  Seems like this
board will be a great learning experience for me.

Much appreciated!!

Best,
Syr

The 2024 Embedded Online Conference