LED brightness problem

Hi guys, I have a problem solving this particular question. Can you please help me out ?

Write a program to vary the brightness of an led using transistor as an interface.The range of brightness should be divided into 5 levels. The user can either input 'one' , 'two' , 'three' , 'four', 'five' to get the brightness of the corresponding levels. Now to solve this problem, i need to get the maximum brightness of the led in use in order to divide the brightness into 5 levels. How do i get this maximum brightness of the led in use through coding ?

You seem to have at least 3 things here:

A circuit using a transistor that you haven’t posted a schematic for.

Code to interpret a serial stream (perhaps?) and identify keywords like ‘one’, ‘two’, etc

A question about how to control LED brightness.

Use analogWrite to set the PWM level for an LED. If the LED takes 20mA or less you don’t need
a transistor, just a current-limiting resistor of 220 ohms or so.

Since the eye’s response is more logarithmic than linear I’d suggest using values like
10, 20, 40, 100, 255 for the argument to analogWrite()

Hi Mark, The problem is that i must use a transistor as per the instructions given in the question. And I didn't have a problem sending those keywords as serial input and their recognition as I've done that before for previous problem statements. My only problem is finding the maximum brightness of that particular LED in question. Is there a way that i can measure it ? Pardon me if I'am not clear yet. And I cant input analog write values myself. The question has a sample answer such as this:

For example, if the maximum brightness is 100, each of those 5 levels should be 20. if the user enters 'three' , the brightness should be corresponding to level 3, i.e 60.

If you give the LED the maximum continuous forward current (from the data sheet), that should be maximum brightness. If you give it more current it may get brighter, briefly.

You drive the LED via a transistor using PWM. The maximum brightness of your LED plus the resistor you're using with it plus the supply voltage will always be when the PWM value is 255. Lower values will be less bright.

No measurement is required unless you really need to know the actual light intensity in lux and that's a rather different problem.


Hi Slipstick, I guess finding the maximum light intensity in lux will solve my problem. How do I do that ?

  1. Read Reply #3 again.

  2. Use a MOSFET (you didn't say it had to be a BJT). Use PWM to drive it between full-on and full-off. Its resistance will be nearly zero when full-on.

  3. Set the current using a resistor computed from LED's Forward Current, Forward Voltage Drop (both from datasheet) and your supply voltage.

  4. PWM value of 255 will be full brightness. PWM value of 0 will be off. As suggested, set your in-between values assuming logarithmic response of the human eye.

Hi Slipstick, I guess finding the maximum light intensity in lux will solve my problem. How do I do that ?

I think you are over-complicating the problem. Does your assignment specifically call for brightness measurements in lux? If not, the instructor probably meant “percentage of full scale PWM”. In that case mapping 0-100 to 0-255 should be sufficient (just multiply the percentage by 255 and then divide by 100).

Yes @JohnWasser you were absolutely right, I misinterpreted the question. Il try out your suggestion. Cheers mate, Thanks.

255 is the max. If you want five brightness levels from 1 to 5, and 1 is "off", then ((b-1)*255)/4 will give you the brightnesses you want. If 1 is not off, but just low (and zero is off), then you want (b*255)/5.

if you want "gamma", then it's pow ((b-1)/4.0, gamma) * 255 or pow(b/5.0, gamma) * 255.

@gfvalvo Yes I interpreted the question wrongly sir, will map the values and get the output as you guys suggested.