ATtiny85 PWM outputs causing supply voltage sag

Discussion in 'The Projects Forum' started by ROTiree, Feb 4, 2018.

  1. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    Evenin' all... Here's the thing - I'm trying to use an ATtiny85 to input (at pin7) a voltage, use the native ADC to then light up an RGB LED (20mA per channel, but that's way too bright, so I'm limiting that to about 10mA each). The circuit uses pins 3, 5 and 6 in PWM mode to control the brightness of the LEDs to cycle through the colours of the rainbow as voltage increases, starting at purple (0v), through shades of indigo to blue, through cyan to green, then yellow, orange and ending on red (4.5V).

    So far, so good and it has been great fun learning the programming language (similar to C, but a lot easier), re-learning some old maths techniques, etc, but there are times when the light output glitches between colours as it reaches a "cusp", as it were. Most of the time it's really smooth, but when a new colour is about to make its presence felt (transition from yellow to red, for example), it kind of flickers between 2 states.

    I put the scope on it, to watch the PWM signals, and noticed that the 0V to +5V PWM traces have little cut-outs in them, down to about 4.7V. I then put the scope onto the Vcc line and it's all over the place! It's definitely an artifact of the power supply's inability to cope with the current surges as the LEDs are rapidly switched on and off. Admittedly, I'm using a piddly little TO-92 format 7805, which has a limit of 100mA - I'm nowhere near that limit, but it can't help matters, so I've got some TO-220 format ones on order, capable of pushing out 500mA, which may solve the problem, to an extent.

    So, the question boils down to this... is there a (relatively) simple circuit that will drive these LEDs? I'm already thinking along the lines of (a) a separate power supply for the LEDs, so the ATtiny's 7805 doesn't have to provide the current surges, and therefore its Vcc will stay much steadier, (b) transistor buffers for the LEDs, so the ATtiny's output pins see a much larger impedance (again, limiting the current that the ATtiny will be using). Maybe also a LP filter to "smooth out the edges" on the PWM spikes, but that's almost certainly going to lead to colours cutting off prematurely on low duty-cycle portions of my spectrum.

    It struck me, though, that someone else might have already solved this problem, and I'd be grateful for any insights they might have.

    Cheers.
     
  2. LesJones

    Well-Known Member

    Jan 8, 2017
    1,389
    332
    Have a look at the input to the 7805 with your scope If it is dropping below about 7.5 volts at any point you need a higher input voltage to it. Do you hav at least the recomended values of input and output capacitors on the 7805 ?

    Les.
     
  3. BobaMosfet

    Senior Member

    Jul 1, 2009
    331
    70
    What does your schematic look like, it may just need a little tweaking. What kind of noise level are you seeing on your postive rail peak-to-peak? Noise will always be there, and also on ground in digital circuits, and some is simply background noise from your 60Hz house power if in the USA.

    All your outputs to your LEDs need resistors to limit the max current do the LED. Beyond that, you might post code for how you set up the ADC and your PWM logic.
     
  4. philba

    Active Member

    Aug 17, 2017
    959
    302
    Yes, a schematic is needed here. What caps do you have in your circuit? What are your current limiting resistors on the LEDs? (the way to bright comment makes me think there is a problem there) Also, what is your input voltage source? With a proper 7805 circuit and a couple hundred mA, >7V supply to the 7805 you shouldn't have any problems.
     
  5. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    Thanks, Les - The input voltage is +9V and the trace is fine, there. And, yes, fairly large caps (10u and 4u7, and a protection diode strapped across Vin and Vout on the 7805 as well - been there, got that t-shirt :D )

    Thanks, Boba. Noise level almost disappears when I take the current-limiting resistors out (presenting the PWM outputs with infinite resistance)... also, the PWM outputs rise from about a 4.3V peak to 4.9V, so I think I might be on the right track with buffers.

    Noise is an issue, with spikes in the frequency plot at 1MHz (ATtiny's clock freq) and harmonics thereof. Taking the CLRs out has, again, reduced this massively.

    As to the code, it's a simple bastardisation of some stuff I found on the Arduino site; the maths works, no glitches there. I haven't tried to do anything clever with fast PWM, or altering the scaling factors on the various channels (because that has the potential to mess with program timing interrupts). Pin3 is a fast PWM in any case, but even that channel drags Vcc down somewhat when it triggers, so making the other 2 operate in their faster modes would not solve the issue.

    So, it looks like presenting the PWM outputs with a high impedance and powering the LEDs and their buffers with a separate supply is the way forwards. Anyone got a quick and dirty (but cleaner :D ) one-transistor buffer to hand, please?
     
  6. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    Thanks for the reply, philba. CLRs were 470R, initially, but I found ones that "balanced" the output as per the attached schem:

    [​IMG]

    The power supply is rated at 250mA - Admittedly, I had been using one of the 100mA outputs, but going to the higher-rated one has made no difference at all. It was worth a try, though.
     
  7. philba

    Active Member

    Aug 17, 2017
    959
    302
    Hmmm, I have almost that exact same set up and don't see what you are seeing. The LED is a Cree RGB that pulls 20 mA per channel. I also use PB3, 4 and 0. Do you always have the programmer attached?
     
  8. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    My LED is a 5mm dia, 20/20/20mA KingBright one - might have a look to see if I can source a Cree one instead. It's got to be through-hole, though.

    The programmer is only used when uploading a program to the ATtiny85 (it's a little USB AVR and I don't want to put any undue mechanical strain on either it, or the USB port on my computer). The rest of the time the ATtiny sits in an 8-pin dip socket on a breadboard (for now), so it's very easy to whip it in and out as I develop the program.

    I forgot to add in the 100nF cap between Vcc and DGND on the ATtiny85 on my schem... bashed out the schem real quick and but I did put that conditioning cap in when it came to breadboarding it.
     
  9. philba

    Active Member

    Aug 17, 2017
    959
    302
    ICSP is a good thing... I can't imagine swapping chips. What a PITA, besides I use SMDs. So that not the issue.

    What is the 9V source? A 9V alkaline battery? Maybe it's running down? They don't really have that much capacity (500 mAH or so) to start with.
     
  10. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,689
    331
  11. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    Source is a Voodoo Labs Pedal Power2 (the go-to power supply for guitarists' FX pedals, because it's stable, very little noise, well-protected, etc). It has 4 x 100mA sockets, 2 x 250mA and 2 that are deliberately designed to "sag" under load (simulates brown-out). Putting a scope on the +9V line from that power supply shows no drop out at all and virtually no noise - it's only observed downstream of the 7805.

    I did try a (brand new) battery - no difference; not better or worse in any way.

    @be80be , not precisely the same manf part no, but the specs are identical to the one I have.
     
  12. philba

    Active Member

    Aug 17, 2017
    959
    302
    It's troublesome that it does that. It's indicative of something pulling too much current. Do any of the components heat up after prolonged use (30+ mins)? If not, then I'd just leave it be.

    If you want to use transistors to drive the LEDs, here's an example. Size R2 as you would driving directly from the 85. You can omit R3, it's to prevent startup glitches. R1 value isn't particularly critical -1K to 4.7K will work. Any small signal NPN transistor will work OK - 2N2222, 2N4401, BC337, ...
    led driver.png
     
  13. BobaMosfet

    Senior Member

    Jul 1, 2009
    331
    70
    Is the input to the ADC a trimpot (that would make sense to me for adjusting color)? If so, you may need to control the range a little bit as reaching the 'stop' in one direction will provide zero resistance whatsoever, which may cause a short or overcurrent condition.

    A couple of things-
    D15 isn't necessary, remove that. It prevents the regulator from separating the load from input and winds up feeding output from C36 back to the input. Change your C35 and C36 caps to non-polarized, and their values should be 0.1uF, not what you have.

    IMHO it's never wise to drive something at it's maximum brightness. I strongly recommend you drive your LED at only 8-10mA. I pulled a datasheet for a Kingbright RGB LED (WP154A4SEJ3VBDZGW/CA) since you didn't provide a link or part number :) - Here are the resistor values I feel you should use:

    Code (Text):
    1.  
    2. COLOR    F(v)    R-OHMS
    3. ------------------------
    4. RED    2.2    360
    5. GREEN    3.3    200
    6. BLUE    3.3    200
    7.  
    Hopefully the table above looks OK.

    Your resistor values don't make sense.
    1K on RED? (5-2.2)/1000 = 2.8mA.
    2.4K on GREEN? (5-3.3)/2400 = 0.708mA.
    470 on BLUE? (5-3.3)/470 = 3.6mA

    The values I provide run them all at around 8mA give or take a little because resistor values can only get close to the math result.

    I would not use onboard PWM and would drive the pins directly based on what it takes to fool the eye and how frequently I have to push a specific waveform at an LED in its entirety at 24fps.
     
    Last edited: Feb 5, 2018
  14. philba

    Active Member

    Aug 17, 2017
    959
    302
    The diode is a standard technique and is shown in a number of 78xx datasheets. It's to prevent a problem where the circuit is powered from somewhere else and Vin = 0.

    What are your reasons for discouraging PWM (I assume hardware PWM)? I use it just fine with RGB LEDs.
     
  15. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    Thanks, philba. No heating issues at all, that I've observed (and I looked for it, given my exact same thought that something was drawing too much current). It must be going somewhere... maybe the ATtiny85 itself, as I set it to 8MHz a day or so ago... That didn't make any difference to the voltage sag, though, so I'm kinda stumped on that.

    That schem you provided would give me the desired effect were it not for the fact that this LED is common cathode, so they'll have to go "emitter-side". I cobbled something together in a SPICE program and came up with an emitter-follower that seems to do the trick (see below). But thanks very much indeed for digging it out. The 'net is littered with thousands of MOSFET and "bespoke" IC versions designed for powering arrays/strings of LEDs and it was difficult to find a simple one.

    Input was via a trimpot, but I scrapped it because it got "scratchy" and replaced it with an ordinary pot (rated for thousands of turns, not 17...). The ATtiny's inputs are rated at Vcc+0.5V and GND-0.5V, and are designed to take Vcc at the input, so no worries there. I also limited the max level to 4V7 with a Zener and a small resistor, to prevent users from breaking it when they turn the knob to 11 to see what happens (guitarists are like that :D ), given that the rest of the circuit operates off a bi-polar +/-9V supply and the input to this bit of the circuit could, when rectified, get to a diode-drop or so below +9V, which is way over the limit. The ATtiny85 is a remarkably robust little beast, as I proved in the wee small hours this morning, when I accidentally connected Vcc to the 9V rail... DOH! All is well, though... for now, but there's no use tempting a guitarist by saying "Stop when the LED goes red! Bad things will happen... Now back off until the LED turns orange.", hence that Zener to clamp the max input to the ATtiny85 to well within limits, while still getting sufficiently fine resolution out of the ADC.

    OK, on to the power supply part. I initially started with 100n caps, as you suggest, but the voltage sags led me to think that bigger "buckets" might help... they didn't. With those caps currently in my set up, though, D15 is recommended. It's buried down in the weeds in all the datasheets... if your input cap fries itself for some reason, when you next switch the power off, the charge in C36 fries the 7805 as it discharges. The same thing happens if you deliberately make Cout > Cin... That diode stops that happening. With small caps and low power, it is not an issue, so I'll take D15 out once I replace the caps with 100n ones.

    I had a look at the datasheet for my Kingbright RGB LED and worked out R values for 15mA per channel, initially, as it's my intent to scale the PWM for each channel in software to balance the colours out. Whichever one is the dimmest of the 3 will get max chat (255) and the others will be scaled back to some other max value so they have matched brightness and the colours "right". Thanks for your work extracting those values, though - it's appreciated.

    The resistor values do not make sense, agreed, but that thing is blindingly bright when you have to get in close to fish something out of the breadboard and replace it with another value. Those (silly) values matched the outputs, to an extent, while not giving me spots before my eyes. They are going to be replaced with more sensible values later on, when it comes to the prototype stage.

    Here's a new schem with emitter-follower drivers:

    [​IMG]

    The values look a bit weird, but this is the result from a SPICE simulator, not RL... I'll test each channel in turn (without the other 2 connected) to check the voltage at the top and bottom of each emitter resistor, work out if the current is correct, then adjust values to taste. From the sim, Ib for each transistor is 90-100uA, Ie for each one is 15mA +/- 0.5mA (using "standard" resistor values), so I should be in the ball-park. Input and output are also in phase, so I won't have to invert all the numbers in my look-up arrays.

    I'd be most grateful for your thoughts on my driver circuit, though?

    Cheers,

    Mike
     
  16. ROTiree

    Thread Starter New Member

    Jan 27, 2018
    11
    0
    UPDATE.

    The noise on the traces has been cleaned up immensely by attaching a pull-up resistor (1k) to Vcc on pin1 (/RST) and a pull-down resistor to pin2 (I/O3). Leaving them floating is, clearly, a bit of a bad thing... but it hasn't solved the voltage sag problem:

    [​IMG]
     
  17. philba

    Active Member

    Aug 17, 2017
    959
    302
    why is R53 so big? You just need to drive the transistor into saturation.

    And by the way, PNPs are generally a better choice for high side driving things. Though, your emitter follower will still have enough V to be above the Vf so it's not the end of the world.
     
    ROTiree likes this.
  18. BobaMosfet

    Senior Member

    Jul 1, 2009
    331
    70
    Not discouraging it. Just doing it a different way allowing the ability to drive all 3 pins in parallel. I use this technique on all kinds of RGB LED PWM projects. For me, it's because the PWM in the ATtiny85 can miscount of the OCR fails to latch during counts.

    I would still remove the diode. In the datasheet I looked at, that diode is only to be there if output exceeds 6V and there is a risk of short-circuit on the inputs.

    I think, also per the datasheet, you can only output up to 60mA on all pins. If you output more current than this, this could explain why your voltage is sagging.
     
    ROTiree likes this.
  19. philba

    Active Member

    Aug 17, 2017
    959
    302
    If the resistors are as in the schematic, there was a lot less that 60 mA being drawn.

    I haven't seen miscounting of of the PWM timer.
     
    ROTiree likes this.
  20. BobaMosfet

    Senior Member

    Jul 1, 2009
    331
    70
    Well, something's up somewhere because voltage doesn't sag unless current draw somewhere is too great.
     
Loading...