View Full Version : LED at different level of intensity

03-03-2005, 10:10 AM

I am trying to program my BS2 to have various LED at different levels od brightness. I tried to use PWM but it will keep the LED on at any intensity but for a limited amount of time then the LED turns off. The only solution I found was to put the PWM command in a loop but the problem was that I couldn't do anything else while the loop was running.

I would like the LED which I can varry in brightness to remain at any given intensity as if it were just on or off.

I hope someone understands what I am trying to do and can offer a solution!



03-03-2005, 10:31 AM
How many levels of brightness do you require ?

Check outˇ the "SX-Video Display Module"

www.sxvm.com (http://www.sxvm.com)

03-03-2005, 10:55 AM
how do you want to control the brightness? you could use a potentiometer to manually set it

Bruce Bates
03-03-2005, 01:10 PM
Carlo -

The PWMPAL offered by Parallax is one solution, for up to 4 channels of PWM:

The PAK-5 co-processor is another solution, for up to 8 channels of PWM:


Bruce Bates

03-03-2005, 03:13 PM

I would just need 3 or 4 levels of brightness and I want to control it through outputs from the computer using SERIN. Possibly I would want to work with what I already have without needing to buy any new part.


03-03-2005, 06:37 PM
Hi all............

How about a digital pot ?

Can be driven direct from the stamp. Have not tried this myself, you may have to consider the power dissipated.



03-03-2005, 07:17 PM
If you only need 4 levels of brightness (off,very dim,normal,bright), then you can just use two pin with different values of resistors. Try something like a 470 ohm, and a 220 ohm resistors. When both pins are off the led is off. When just the pin with the 470 ohm resistor is on the led will be dim, when just the pin with the 220 is on the led will be normal, when both pins are on the led will be bright.

You may have to play around with the values to get 4 distinct brightnesses.
By "off" I mean make the pin an input NOT low.


Check outˇ the "SX-Video Display Module"

www.sxvm.com (http://www.sxvm.com)

03-03-2005, 08:49 PM
You could use the multi-tasking framework Jon Williams has put out for PBasic. one of the tasks could be turning on and off the LED in a duty cycle necessary to make it look dim. In effect, your program loop would do the PWM needed to light the LED dimly.

03-04-2005, 12:35 AM
There are two obscure matters of physics here to confust things. One is a LED is a current controlled device, meaning if you wish to half the intensity you need to half the current. Next is the human eye is a peak detector, it tends to integrate the peaks of a rapidly flashing light as the percieved intensity.

Put these together and you find pulse modulation for LED brightness doesn't work as you would first think. To a large degree your eye percives one intensity (the peak one).... it may see it flash, but the flash will be the same brightness. This is one reason multiplexed LED disdplays work so well.

Bean's suggestion of the multiple resistors is a safe a and sure way to go, since you have a limited number of intensities to light.

Under my professional hat I'm working on a project that requires calibratable light sources (red and blue), where I have to regulate the intensity of each color flash. To acheive that I have an I2C D2A driving a voltage to current converter, then to the LED. A micro controller (not a Stamp though a Stamp is smart enough and fast enough for this) uses a photo diode to read the intensity and control the LED drive to keep the intensity constant. Seems like a lot of work for little gain, but it makes sense in the overall application.

03-04-2005, 02:03 AM
Also, an LED is not a linear light device. Thus from 5 mA to 8 mA, its brightness may change with current. From 10 mA to 15 mA, it looks full bright. So the two resistor idea will work pretty well -- but you'll have to experiment with WHICH two resistors to use to get the right currents for your LED.

03-04-2005, 10:15 AM
Have you looked at the MAX7219?ˇ I have not tried it but the data sheet says it will control up to 64 individual LEDs with brightness control.ˇ I believe it usesˇon board PWM for the brightness control.ˇ I have read the data sheet a couple of times and I can't figure out if the brightness control can vary the brightness for the individual LEDs.ˇ Perhaps someone with a better eye for data sheets can decipher it and relay it back to us in laymen's terms.

I would be very interested.

Chris Isaacson

03-04-2005, 10:18 AM
Off the top of my head. The brightness controls ALL LEDs and you must supply the PWM signal.

Check outˇ the "SX-Video Display Module"

www.sxvm.com (http://www.sxvm.com)

03-04-2005, 01:03 PM
The max7219 controls brightness by writing a value to it.

I posted code that describes in some detail this feature. I will look up the post, copy the section about brightness and repost.

Better yet, more than just the brightness portion, but how to use it.

;------[MAX7219 Operation]----------------------------------------------------
;The 7219 must first be initialized. Initialization is done by sending serial
;data to the IC. Four 16 bit values sent will complete the initialization.

The initialization sets
1) decode mode
2) shutdown mode
3) scanlimit
4) intensity

;To send a 16 bit value load (p16 of 7219) is brought LOW, then each of 16 bits (msb 1st) is
;clocked into the IC, and load is then brought HIGH to complete the shifting
;of 16 bits into the device

;For each word sent to the IC, the most significant byte isˇan address and the
;lower byte sets the option for each particular function.
;For the descriptions that follow, values will be shown as $Xx. The upper case x
;represents the upper nibble of a byte and the value is "don't care" and the lower case
;x represents some actual value

;DECODE MODE - Address $X9
;ˇ $X0 = No decoding
;ˇˇ$X1 = Decode digit 0 (BCD)
;ˇˇ$XF = Decode digits 0-3 (BCD)
;ˇˇ$FF = Decode digits 0-7 (BCD)

;Sending $09FF sets decode mode for all digits and instructs that the least
;sig nibble is BCD

The basic stamp code to do the above is:

DIRS = $000Eˇˇˇˇˇˇˇˇˇˇˇˇ 'pins 1 2 3 output,all other pins inputs
DOUT PIN 1ˇˇˇˇˇˇˇˇˇˇˇˇˇˇ 'pin 1 of stamp to pin 1 of 7219
Load PIN 2ˇˇˇˇˇˇˇˇˇˇˇˇˇˇ 'pin 2 of stamp to pin 12 of 7219
CLK PIN 3ˇˇˇˇˇˇˇˇˇˇˇˇˇˇˇ 'pin 3 of stamp to pin 13 of 7219

;in my program there is a call to the subroutine DecodeMode:

ˇ LOW Load
ˇ HIGH Load

; next you need to set the shutdown mode to active

;Optionsˇ$X0 = Shutdown
;ˇ$X1 = Normal Operation
;Sending $0C01 sets the IC for normal operation

;The BS2 code to do the above is:

;in my program there is a call to the subroutine ShutDownMode:

ˇ LOW Load
ˇ HIGH Load

;Next you need to tell the 7219 how many digits are to be used.

;SCANLIMIT - Address $XB
;Optionsˇ$Xx where the least significant nibble sets the number of digits
;to enable. digit 0, x = 0. digits 0 & 1 x = 1. digits 0, 1, & 2 x = 2
;Sending $0B02 enbables digts 0, 1 & 2
;Sending $0B07 enables all 8 digits

;the BS2 code to do the above is:
;again, this is called with a subroutine

ˇ LOW Load
ˇ HIGH Load

;Next set the intensity of the the LED segments

;INTENSITY - Address $XA
;Optionsˇ$00 = minimum intensity
;$FF = maximum intensity
;See MAX7219 data sheet for resistor selection and current

;To set "medium" intensity ( I had a 47 k ohm resistor from pin 18 of the 7219 to vcc)

The bs2 code to do this is:

'Set intensity of LEDs
'LSNibble 0 = min intensity and F = max intensity

ˇ LOW Load
ˇ HIGH Load

Write back if you have questions.

Post Edited (KenM) : 3/4/2005 6:07:19 AM GMT

03-04-2005, 07:14 PM
Opps, I was thinking of a different driver chip.
Sorry about that.

Check outˇ the "SX-Video Display Module"

www.sxvm.com (http://www.sxvm.com)

03-05-2005, 02:58 AM
I managed to make the LED work using 2 pins now I am playing around with the resistors as Bean suggested. However I was reading the BS manual and I read this:

General-purpose I/O pins: each can sink 25 mA and source 20
mA. However, the total of all pins should not exceed 50 mA (sink)
and 40 mA (source) if using the internal 5-volt regulator. The total
per 8-pin groups (P0 – P7 or P8 – 15) should not exceed 50 mA
(sink) and 40 mA (source) if using an external 5-volt regulator.

Does this mean I can only plug 2 LEDs at 20mA to the stamp? Could someone please explain what this means?


03-05-2005, 03:37 AM
Basically yes, sourcing 2 LEDs at 20 mA each will put you at the limit of the Stamp current output.

However,ˇthat limitation is VERY easy to work around. Parallax documentation has addressed how to avoid the issue, but it is easier for me to simply tell you and post a schematic.

But please do read the parallax documention.

The transistor you can get at Radio Shaft

This configuration will consume about 200 uA or less from the stamp pin.

Depending on how many of these guys you put on the 5volt line, you might need to consider an external 5v regulator.
I don't use a stamp often these days, so I cannot tell you how much current the stamp on-board 5v regulator can handle.
I am sure some one else can chime in with that info.

This circuit assumes the LED forward voltage is 1.8, and the stamp output is a full 5 volts.

If so, the voltage on the anode of the LED (or transistor emitter) would be about 4.3 volts.
4.3 volts - 1.8 volts is 2.5 volts left over across the resistor.
2.5 volts / 125 ohms is 20 mA.


Post Edited (KenM) : 3/4/2005 8:44:08 PM GMT

03-05-2005, 04:00 AM
You can also use a darlington array chip (LM2803?), with some resistor SIP packages, to do the same thing with fewer parts. You've been saying all along "control LED" -- that's singular LED, so that's what we've been suggesting. How many LED's do you really want to control?

The Maxim 7219 can control 64 LED's at one time, WITH brightness control, and NO external circuitry (OK, maybe ONE resistor), so if you've got LOTS of LED's you may want to go that route.

03-05-2005, 04:11 AM
Sorry about the misunderstanding! I would like to use about 10 LEDs if possible and some (around 4) I would like to vary in intensity possibly each at a different level.

03-05-2005, 05:11 AM
Sounds like a job for a couple of 74HC595 shift-register chips. They latch, they use 3 pins of the BS2, they have sufficient current to do the job. You'd use SHIFTOUT to send the bit patterns to the chips.

You can control 8-LED's per '595 (or 4-LED's if you're using the two-resistor current-limit method). So dedicate one to the 4 'dimmable' LED's, and the other to the rest of your LED's.

03-05-2005, 07:02 AM
Pardon my digression, but what do you mean when you say they "latch"?ˇ

Paul Baker
03-05-2005, 08:07 AM
a latch is somewhat akin to a register, but the enable signal controls the behavior a little differently, at one state, whatever is on the input immediatly shuttles through to the output (like a buffer) when you change the enable pin to the other state, whatever is at the input at the time of transition, the output is "latched" ie is held at that level. So you can think of a latch as a cross between a buffer and a register depending on what level you have the enable pin set to.

Without looking at the datasheet, I believe what allan is refering to is that you can shift the contents of the register without affecting what the outputs are, then when your done shifting, you relatch the outputs to update based on what the internal registers contain, this avoids seeing the shifting action on the outputs.

Post Edited (Paul Baker) : 3/5/2005 1:11:33 AM GMT

03-07-2005, 05:17 AM
for your 4 controlled leds + 6 noncontrolled leds you could use a 78L05 or other voltage regulator and bypass the stamps onboard reg by going to the 5v pin. this gives i think up to 100ma. you need to put two of the controlled leds on one side of the stamp and the other two on the other(can't source more than 50ma a side). use the resistor dimming you've tried. then drive the 6 leds through a separate mosfet or mosfets depending on your main voltage. i'm doing this now with a 4+4 setup on a solar project i've got set up where it turns on successively more leds as the battery gets charged up. works fine for me.

here's an led calc i use for getting max brightness:


03-08-2005, 10:43 PM

I bought a few 2N3904 transistors as Ken suggested me and I connected it to the circuit as in Ken's diagram but it doesn't seem to work. I don't know that much about transistors that's why I'm a bit stuck.

Would anyone be able to give me some suggestions?



03-08-2005, 10:48 PM

Check the following:

Is the transistor ground connected to the same ground as the stamp?

As you look at the transistor with the flat side towards you, and the leads pointing down, from left to right the leads are emitter, base, collector

When you make the stamp pin go high, connect one lead of a voltmeter to ground, then what is the voltage on the base, emitter and collector?

What color is the LED you are trying to use, the circuit I posted is assuming you are using a "red" led that uses only about 1.8 volts forward biased.

Possibly misread the resistor value, and the one you are using is too large (value, not physical size).

Remove the resistor and measure the ohms.


03-09-2005, 12:18 AM
It works, I didn't have the transistor on the same ground.

Thank you

03-09-2005, 11:04 PM
I need your help again!!

I connected the transistor as in the diagram with the base directly to the pin but it draws almost 30mA that is more than if I put only the LED. So I am back again to the original problem. I tried connecting a resistor to the base of the transistor that solves the problem, but at the same time it lets less current pass through the emmitter so it dims my LED. I hope you understand what my problem is!

Is there a formula to calculate the total resistance? or is there a way around this problem?

03-09-2005, 11:16 PM
Yes, there is an easy solution.

The resistor value in series with the LED needs to be increased.

ˇDo the following:
Reconnect your circuit Measure the voltage on the LED anode Take that voltage reading, and divide the voltage reading by the current you want to pass through the LED. The result of the division is the resistorˇvalue needed

Basically, and LED uses a 'fixed' amount of voltage. ( In reality, the voltage across the LED will vary a little with the current through it, but lets not get into that now)

What ever voltage is 'left over' in your circuit, is used to select a resistor value that will set the current to some desired value.

Ex. There is a 5 volt circuit (4.3 in your case)

The LED uses 2.0 volts.

5 volts - 2 volts gives you 'left over' 3 volts.

3 volts divided by the desired current = the needed resistor value

03-17-2005, 03:00 AM
Hi again,

I managed to solve all the problems I had thanks to you guys!!

I had oneˇother question: what is the maximum current can I draw from Vdd and Vss? I am using the BS2 educational board. Will the limit be the maximum current the transformer can output?

Thanks again!


03-17-2005, 03:19 AM
We recommend no more than 40mA for Homework board and no more than 1amp for the REV C Board of Education.


Dave Andreae

Tech Support
dandreae@parallax.com (mailto:dandreae@parallax.com)
www.parallax.com (http://www.parallax.com)


03-17-2005, 04:13 AM
The linear regulator on the BOE board will limit you to 1 Amp. You can try to pull more than that, but I believe you may be doing damage if you do.