Shop OBEX P1 Docs P2 Docs Learn Events
LED at different level of intensity — Parallax Forums

LED at different level of intensity

CarloCarlo Posts: 23
edited 2005-03-16 21:13 in BASIC Stamp
Hi,

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!

Thanks,

Carlo

Comments

  • BeanBean Posts: 8,129
    edited 2005-03-03 03:31
    How many levels of brightness do you require ?
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.sxvm.com
    ·
  • EvanEvan Posts: 7
    edited 2005-03-03 03:55
    how do you want to control the brightness? you could use a potentiometer to manually set it
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-03-03 06:10
    Carlo -

    The PWMPAL offered by Parallax is one solution, for up to 4 channels of PWM:
    http://www.parallax.com/detail.asp?product_id=28020

    The PAK-5 co-processor is another solution, for up to 8 channels of PWM:
    http://www.awce.com/pak5.htm

    Regards,

    Bruce Bates
  • CarloCarlo Posts: 23
    edited 2005-03-03 08:13
    hi,

    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.

    Carlo
  • alphamike27alphamike27 Posts: 10
    edited 2005-03-03 11:37
    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.



    Regards



    Alan
  • BeanBean Posts: 8,129
    edited 2005-03-03 12:17
    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.
    ---edit---
    By "off" I mean make the pin an input NOT low.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.sxvm.com
    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-03 13:49
    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.
  • ErnieErnie Posts: 20
    edited 2005-03-03 17:35
    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-03 19:03
    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.
  • StarManStarMan Posts: 306
    edited 2005-03-04 03:15
    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
  • BeanBean Posts: 8,129
    edited 2005-03-04 03:18
    Off the top of my head. The brightness controls ALL LEDs and you must supply the PWM signal.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.sxvm.com
    ·
  • KenMKenM Posts: 657
    edited 2005-03-04 06:03
    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.



    ;
    [noparse][[/noparse]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
    ;Options
    ;· $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:

    DecodeMode:
    · LOW Load
    · SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$09FF\16]
    · HIGH Load
    · RETURN

    ; next you need to set the shutdown mode to active


    ;SHUTDOWN MODE - Address $XC
    ;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:

    ShutDownMode:
    · LOW Load
    · SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0C01\16]
    · HIGH Load
    · RETURN

    ;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

    ScanLimit:
    · LOW Load
    · SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0B07\16]
    · HIGH Load
    · RETURN

    ;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


    Intensity:
    · LOW Load
    · SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0A07\16]
    · HIGH Load
    · RETURN

    Write back if you have questions.

    Post Edited (KenM) : 3/4/2005 6:07:19 AM GMT
  • BeanBean Posts: 8,129
    edited 2005-03-04 12:14
    Opps, I was thinking of a different driver chip.
    Sorry about that.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out· the "SX-Video Display Module"

    www.sxvm.com
    ·
  • CarloCarlo Posts: 23
    edited 2005-03-04 19:58
    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?

    Carlo
  • KenMKenM Posts: 657
    edited 2005-03-04 20:37
    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.

    ken

    Post Edited (KenM) : 3/4/2005 8:44:08 PM GMT
    667 x 544 - 29K
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-04 21:00
    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.
  • CarloCarlo Posts: 23
    edited 2005-03-04 21:11
    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-04 22:11
    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.
  • StarManStarMan Posts: 306
    edited 2005-03-05 00:02
    Pardon my digression, but what do you mean when you say they "latch"?·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-03-05 01:07
    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
  • davecdavec Posts: 10
    edited 2005-03-06 22:17
    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:
    http://ourworld.compuserve.com/homepages/Bill_Bowden/led.htm

    cheers
  • CarloCarlo Posts: 23
    edited 2005-03-08 15:43
    hi

    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?

    Thanx

    Carlo
  • KenMKenM Posts: 657
    edited 2005-03-08 15:48
    Carlo,

    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.

    k
  • CarloCarlo Posts: 23
    edited 2005-03-08 17:18
    It works, I didn't have the transistor on the same ground.

    Thank you
  • CarloCarlo Posts: 23
    edited 2005-03-09 16:04
    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?
  • KenMKenM Posts: 657
    edited 2005-03-09 16:16
    Yes, there is an easy solution.

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

    ·Do the following:
    1. Reconnect your circuit
    2. Measure the voltage on the LED anode
    3. Take that voltage reading, and divide the voltage reading by the current you want to pass through the LED.
    4. 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
  • CarloCarlo Posts: 23
    edited 2005-03-16 20:00
    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!

    Carlo
  • dandreaedandreae Posts: 1,375
    edited 2005-03-16 20:19
    We recommend no more than 40mA for Homework board and no more than 1amp for the REV C Board of Education.

    Dave



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Dave Andreae

    Tech Support
    dandreae@parallax.com
    www.parallax.com

    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-16 21:13
    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.
Sign In or Register to comment.