Shop OBEX P1 Docs P2 Docs Learn Events
Is this PWM reaction normal? — Parallax Forums

Is this PWM reaction normal?

ERMERM Posts: 34
edited 2006-06-12 10:26 in BASIC Stamp
Hi everybody,

Does anyone have an answer to this glitch I've noticed. Using the program below, I've noticed that when the led's I have on each pin fade on and off back and forth, the led that should be out lights dimly. This happens even when you take out the second half of the program to flash 1 by itself. Led 2 will light dimly and go out.

Any thoughts or remedies?


fade:

FOR fade = 255 TO 1 STEP 5
PWM 1, 255 - fade, 1
NEXT
FOR fade = 1 TO 255 STEP 5
PWM 1, 255 - fade, 1
NEXT
FOR fade = 255 TO 1 STEP 5
PWM 2, 255 - fade, 1
NEXT
FOR fade = 1 TO 255 STEP 5
PWM 2, 255 - fade, 1
NEXT

goto fade

Comments

  • BeanBean Posts: 8,129
    edited 2006-06-08 11:06
    Tony,
    When the PWM command finishes it puts the pin into an INPUT state.
    I'm not sure why that would make your LEDs stay on dimmly though ???
    Can you post a schematic ?
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "I reject your reality, and substitute my own." Mythbusters
    ·
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-06-08 12:02
    Folks -

    Just for the record, the program listed can not be the operational program, since it contains syntactical errors. The problem may be more self-evident if we can see the actual, operational program.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • ERMERM Posts: 34
    edited 2006-06-08 15:54
    About the error, I didn't mean to label the routine fade: and then GOTO fade. It was late when I posted the message. The correct code is below.

    The setup consists of both pins going to their own transistor through a resistor to control the leds.
    The scenario, as led 1 lights up, led 2 lights up dimly and diminishes when led 1 dims out and vice versa when led 2 lights.
    The same happens with the other outputs if led's are connected to pins 3, 4, and so on. As led 1 or 2 lights, the other leds on the other outputs light dimly all together.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    fade VAR Word
    BtnWk VAR Byte

    singlefade:

    FOR fade = 255 TO 1 STEP 5
    PWM 1, 255 - fade, 1
    NEXT

    FOR fade = 1 TO 255 STEP 5
    PWM 1, 255 - fade, 1
    NEXT

    FOR fade = 255 TO 1 STEP 5
    PWM 2, 255 - fade, 1
    NEXT

    FOR fade = 1 TO 255 STEP 5
    PWM 2, 255 - fade, 1
    NEXT

    GOTO singlefade
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-06-08 18:16
    Try making the for loops from 255 to 0, and 0 to 255. Also, at the end of each PWM loop, directly set the output either high or low. What do you mean by "going to their own transistor through a resistor"? There are many ways to hook up a transistor, and if you have a common emitter circuit that might not be the best performer.

    FOR fade=0 TO 255 STEP 5
    PWM 1,fade,1
    HIGH 1 '<---definite HIGH
    NEXT
    FOR fade=255 TO 0 STEP 5
    PWM 1,fade,1
    LOW 1 '<-- definiite LOW
    NEXT[noparse][[/noparse]/code]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ERMERM Posts: 34
    edited 2006-06-11 05:05
    OK, can anyone explain this? I have revised my code to a simple one to see the results. This is what I have programmed:

    singlefade:

    FOR fade = 0 TO 255
    PWM 1, fade, 9
    NEXT

    FOR fade = 255 TO 0
    PWM 1, fade, 9
    NEXT

    GOTO singlefade

    and the result is that led 1 slowly lights up, and here's the twist, no matter what output pin I use 0-15 minus number one which I am already using, the led on that pin will light dimly increasing in intensity and then extinguish as pin 1 reaches its maximum intensity and then the other led's will light dimly again as led 1 begins its "fall" and the others will dim out.

    Can anyone else reproduce this problem? I chose 9 for the rate so I can see what happens in "slow motion". The attached picture is an example of how I have my setup coming off of the output to a transistor that controls a higher load. When I connect a second one of these arrays to any other output, I get the above result. Can anybody figure this out?

    Thanks,
    Tony
    177 x 241 - 10K
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-06-11 07:00
    Tony -

    See what happens when you add a blocking diode, as in the modified version of your schematic, which is attached. My first inclination is that there·may be a +VDC back-feed through the ground plane. If so, the blocking diode·should prevent it from affecting your circuitry.

    If I'm wrong, stop taking advice from software guys like me :-)

    Regards,

    Bruce Bates




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->

    Post Edited (Bruce Bates) : 6/11/2006 7:20:53 AM GMT
    177 x 241 - 8K
  • grimshadygrimshady Posts: 80
    edited 2006-06-11 12:06
    When these pins go into an INPUT state then are they are at an "unknown" potential?

    Maybe he should pull those lines low (through a resistor) to ensure the transister for the other banks of LED's stays OFF.


    Bean (Hitt Consulting) said...
    Tony,
    When the PWM command finishes it puts the pin into an INPUT state.
    I'm not sure why that would make your LEDs stay on dimmly though ???
    Can you post a schematic ?
    Bean.

    Post Edited (grimshady) : 6/11/2006 12:26:44 PM GMT
  • BeanBean Posts: 8,129
    edited 2006-06-11 12:12
    What value is the resistor going to the base of the transistor ?
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "I reject your reality, and substitute my own." Mythbusters
    ·
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-06-11 15:50
    I've run across a similar problem when using PWM to generate several channels of voltage on neighboring Stamp pins. What is happens is that there is a small capacitance between the pins, and the rapid transitions on the active PWM pin couple into the neighboring pin, which is an input. When generating voltages, the effect is that the PWM activity on one pin causes the other voltages to change a little bit. In your case, the current coupled by the parasitic capacitor goes into the base circuit of your transistor next door and turns it on a little bit. The solution is to put a small capacitor (33pf - 100 pf) either from each stamp pin to ground, or from the base to ground on each of your transistors.

    I have a web page writeup on this at www.emesys.com/BS2PWM.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • grimshadygrimshady Posts: 80
    edited 2006-06-11 17:30
    Interesting....what I find astonishing is the fact that the base can get enough current to drive the base of the transistor from parasitic capacitance.· If it was a mosfet then it would seem easier to accomplish.·

    Also, the base has to at least reach 0.7v to overcome the base,emmiter junction so the pin not only has to rise up more than 0.7V but also source enough current to accomdate the hfe of the transistor.· He's running 60ma I guess through the transistor so what does he need to get the transistor to turn on a little...like maybe 3ma?



    I'll go read your link, maybe all my questions are already answered.
    Tracy Allen said...
    I've run across a similar problem when using PWM to generate several channels of voltage on neighboring Stamp pins. What is happens is that there is a small capacitance between the pins, and the rapid transitions on the active PWM pin couple into the neighboring pin, which is an input. When generating voltages, the effect is that the PWM activity on one pin causes the other voltages to change a little bit. In your case, the current coupled by the parasitic capacitor goes into the base circuit of your transistor next door and turns it on a little bit. The solution is to put a small capacitor (33pf - 100 pf) either from each stamp pin to ground, or from the base to ground on each of your transistors.

    I have a web page writeup on this at www.emesys.com/BS2PWM.htm

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-06-11 17:52
    The interpin parasitic capacitance is only about 10 pf, so it is surprising to me, too, that it turns on the bipolar transistor enough to make the leds glow. The current through the parasitic capacitance has to go somewhere, and the only path available is through the base of the transistor. You expect the amount of current coupled to be maximum when the PWM frequency is at its maximum (=111 kilohertz with the BS2 when the PWM is 50%, fade=128 in Tony's program).

    There might also be leakage current from the Stamp input pin, but that current would only be femtoamps. A 100pf capacitor in parallel with the pin or from base to emitter should easily shunt the high frequency current spike away from the base. Or, for the solution with no additional components in Tony's application, simply make the pin a definite LOW or HIGH output after completion of the PWM command, instead of leaving it as an input. That is not an option when you want to use PWM as a DAC.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-11 21:15
    Tony Mour, you have:

    singlefade:
    ·FOR fade = 0 TO 255
    ·PWM 1, fade, 9
    ·NEXT
    ·FOR fade = 255 TO 0
    ·PWM 1, fade, 9
    ·NEXT
    ·GOTO singlefade

    Maybe that's a "typo"?· I believe that it should be:

    singlefade:
    ·FOR fade = 0 TO 255
    ·PWM 1, fade, 9
    ·NEXT
    ·FOR fade = 255 TO 0 STEP -1
    ·PWM 1, fade, 9
    ·NEXT
    ·GOTO singlefade
  • SSteveSSteve Posts: 808
    edited 2006-06-11 22:09
    PJ Allen said...
    Maybe that's a "typo"? I believe that it should be:
    ...
    FOR fade = 255 TO 0 STEP -1
    No, PBASIC automatically supplies the "STEP -1" if the second value is less than the first. Perhaps that was new in PBASIC 2.5. That's the only version I've used, so I don't know which features came when.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-06-11 22:17
    OK -- it's a BS1 thang.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-06-12 04:55
    grimshady -

    Use the IF...THEN...ELSE form of the IF statement rather than the IF...THEN format, and that THEN....GO TO restriction will disapper.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • grimshadygrimshady Posts: 80
    edited 2006-06-12 10:26
    Unfortuanatly, BS1 here not bs2 or better...shakehead.gif
    I think only bs2 has the good stuff.
    I didn't mean to threadjack (actually did but I dont want to pull this thread away from its original topic so please no more posts on my IF then problem).· PM me if you want to add to this.·· I've deleted my original message.

    To get this back on topic, I'm using the PWM command also and getting unexpected results..maybe due to impropper byte variable use.

    PWM is the topic. ...I"ll go back and read that page more carefully...wish I was using a bs2!!!
    Bruce Bates said...
    grimshady -

    Use the IF...THEN...ELSE form of the IF statement rather than the IF...THEN format, and that THEN....GO TO restriction will disapper.

    Regards,

    Bruce Bates

    Post Edited (grimshady) : 6/12/2006 10:33:42 AM GMT
Sign In or Register to comment.