# Any PWM gurus out there?



## irksomeremnant (Jun 8, 2011)

So I want to run two white LEDs off a 9V battery....(presently dabbling with LEDS/PICS and a breadboard) - but I don't want a resistor to 'waste' 25% of the overall battery useage.

So my thinking goes along the lines of using a PIC & PWM....

1. First, establish what the minimum voltage the battery can fade to before the LED brightness can be allowed to wane. Since this is a 9V battery ...I'm saying 6.8V (yes I know a 9V battery still has a fair bit of life below 6.8V but I need to get a current limiting resistor in series & must allow for the voltage voltage drop across it)

2. Ok, two white leds in series, with a forward voltage of 6.2V.

so working on my minimum voltage of 6.8V, I need to lose about 0.6V across a current limiting resistor.

I want about 18mA running through the LEDS, so this would be a resistor of about 24 Ohms.

Ok, I hear you say....but what about a fresh battery?!

Well a fresh PP3 battery (under load) _is_ probably about 9V....yet above, I've calculated above for a supply of 6.8V ....so, how about I PWM the LED circuit, so instead of getting 100% of the supply (which for a fresh battery would be 9V), I use a duty cycle to only to allow 9V for 75% of the time (which averages to bean 'effective' voltage of about 6.8V), as the battery level falls (I monitor it), I increase the PWM duty cycle.....up to 100% when the battery wanes to 6.8V.

Now yes I know there'll be increased current through the leds when the 9V supply is 75% on), but just wondering what the impact will be (it actually works on a breadboard, but I haven't quite squared away in my head the ramifications & calculations!)


Will this save battery life, is the thinking flawed? Should I take up floral pottery?


----------



## bbawkon (Jun 8, 2011)

Thinking is good, but I would add a few things. Inductor in series and cap in parallel. This will REALLY smooth/average out the voltage (less spikey) and thus will be MUCH kinder to your emitter. Right now, those LEDs are getting slammed with 9V and are probably conducting hundreds of percent of their rated current. Also, are you switching the load directly with the PWM output, or are you PWMing a FET that switches the load?


----------



## irksomeremnant (Jun 8, 2011)

bbawkon said:


> Thinking is good, but I would add a few things. Inductor in series and cap in parallel. This will REALLY smooth/average out the voltage (less spikey) and thus will be MUCH kinder to your emitter. Right now, those LEDs are getting slammed with 9V and are probably conducting hundreds of percent of their rated current. Also, are you switching the load directly with the PWM output, or are you PWMing a FET that switches the load?


 
Thanks....I mentioned in another thread, that inductors have proved to be a bit of a no-no for the intended final location (a bass guitar)....but no harm in a cap I suppose, I'm switching the 'ground' to the LEDs via a FET.


----------



## bbawkon (Jun 8, 2011)

Yeah, at least put a big cap in parallel with the two LEDS - 16V or higher rated. Your theory is fine, and it will work well - but you need to lower the spikes if you want to LEDS to have any significant life span.

An inductor would be best (You can really tolerate the EMI from this solution but not from a shielded inductor?), but a nice big cap is a million times better than nothing.

Ben


----------



## Steve K (Jun 8, 2011)

irksomeremnant said:


> Thanks....I mentioned in another thread, that inductors have proved to be a bit of a no-no for the intended final location (a bass guitar)....but no harm in a cap I suppose, I'm switching the 'ground' to the LEDs via a FET.


 
When it was proved that inductors were a problem, how close was the inductor to the pick-up? 
What if you located the switching power supply some distance from the pick-ups, and used twisted-pair wiring to bring power to the LEDs? and probably want to have 0.1uF across the LEDs too, just for good practice.

I'm still a bit concerned about pwm'ing the current through the LEDs, since this produces very fast changes in current and voltage at the LEDs, which is what usually causes problems. Using a processor means that you've added another source of noise.

Anything that can be done to slow the changes in the current and voltage will reduce the harmonics that are generated. The selection of the switching frequency might be important too. You may be able to chose a switching frequency that is high enough to place it out of the range that the pick-ups are sensitive to. The risk is that you might find out what other parts of the guitar or amp are sensitive to high frequencies. 

regards,
Steve K.


----------



## irksomeremnant (Jun 8, 2011)

Steve K said:


> When it was proved that inductors were a problem, how close was the inductor to the pick-up?
> What if you located the switching power supply some distance from the pick-ups, and used twisted-pair wiring to bring power to the LEDs? and probably want to have 0.1uF across the LEDs too, just for good practice.
> 
> I'm still a bit concerned about pwm'ing the current through the LEDs, since this produces very fast changes in current and voltage at the LEDs, which is what usually causes problems. Using a processor means that you've added another source of noise....the buck boost I used was an adaptive type...so matbe I'd be better of with a fixed frequency boost (as I suspect it switching frequencies might be a cause of some problems)
> ...


 
Thanks guys.

re the inductors being a source of problem...that was with a buck boost circuit...I've no space in my guitar for a circuit board (without taking a chisel to it...that's not gonna happen!) so the circuit board is actually going under the pickup (so ahem, rather *close*....not a lot of room for big caps either - probably need to fit some tantalum caps in there).

I've actually now got everything running reasonably quiet ...my main worry now is battery consumption (not to mention the worry about a shortened LED life due to me whacking the LEDs with current they weren't anticpating...albeit only for 70% of the time)


----------



## irksomeremnant (Jun 8, 2011)

Actually, I think I've found a flaw in my logic....given the above numbers in my first post & a 24R series resistor, when the voltage is 9V (ie a fresh battery), there'll be about 106mA running through the two white leds....now I'd figured on something like a 75% duty cycle would adress this (9V -> 6.8V is a 25% reduction), but clearly, if I applied a 25% reduction in the duty cycle, the LEDS would still have an 'average' current of 80mA through them (not good....and the LEDS are likely to be a little stressed!)

It's late here....maybe I'm now just confusing myself?! should it be the 'effective' (PWM'ed) voltage I work out first & then use this to calculate the current draw (in which case a 75% duty cycle at 9V is an 'effective' voltage of 6.8V ....& 6.8V into a 24R resistor & two white leds is about 18mA....which is what I want)


----------



## bbawkon (Jun 8, 2011)

You are correct - average current would be 80mA - not good 

A better solution would be to not even measure battery voltage - it's really irrelevant anyway since you want an average CURRENT, not an average voltage. Instead, put a small current sense resistor in series with the LED(s) and used the uC A/D to measure current during your on time - then use this to calculate your duty cycle.. Turn PWM ON, Measure Current (Lets say 100ma), 100/20 = 5, 1/5 = 20%, Set PWM Duty cycle to 20%..


----------



## irksomeremnant (Jun 9, 2011)

All good points well made...that said, I want to learn so I'll stick with this setup for a little while.

Ok, the plot thickens (bear with me here), since I thought the LED would be getting an average of 80mA (see my last post- post #7), I therefore have just trialled what the maximum PWM duty cycle should be to supply 18mA.

There are 100 'steps' of PWM up for grabs (normally there'd be 255 steps but because my HPWM is running at a high frequency, there are less steps available, so in this case a PWM value of 100 means 100% duty cycle)

Ok, 9V through a 24R resistor & two white LEDS (forward voltage of 3.175V) , yields a current of about 110mA - but I need 18mA, which is approximately 84% less 

So, taking the PWM down to a value of about '16' (ie if 100 is the max PWM value, then 84% less means a PWM value of 16) _should_ mean the LEDS get an average of 18mA.

So I tried it...the LEDS are very dim....so maybe it really is the voltage I should be working on (as per my first post) & not the current (as per post #7)?

I guess this could be all squared away if I had a true RMS meter!


Edit:Ok, just splashed out for a 'True RMS' DVM (my old one was showing its age anyway!) ...I will do some tests & post back!


----------



## bbawkon (Jun 9, 2011)

How high of a frequency? Is your FET good enough to turn on/off that fast? With a low duty cycle, a slow fet may never even turn on all the way. Even at 16% duty cycle - with 110mA hitting the LED, it should not be visibly dim.. RMS Meter will be nice.. Oscilloscope with a current probe would be far nicer.


----------



## irksomeremnant (Jun 9, 2011)

Ok, now I'm confused.....

I just went out & bought a 'true RMS meter' (a Uni-T UT61E) ....but I'm not getting the readings I'd imagined. (I've taken the PWM frequency down to 1khz while taking these measurements - as that's the highest pwm the the Uni-T meter can measure)

Re my readings, well at 50% duty cycle....I see a DC drop of about 3.6V DC across the 40 Ohm resistor.....but bearing in mind I'm using a PWM 50% duty cycle - should I actually be measuring the AC voltage drop across the resistor (in other words is modulated DC considered AC?!) ...if so the reading I get using my (supposedly) 'true rms' meter is just 1.5V AC across the 40 ohm resistor?

Or should I dispense with voltage readings all together....in which case I'm reading 21mA DC through the circuit ...if I switch to AC....it reads 21mA too!

If I take the duty cycle down to 25%, I get an AC current through the circuit of 17mA (& a DC current of 9.88mA)

Aaargh....not understanding!


----------



## nickelflipper (Jun 9, 2011)

irksomeremnant said:


> So I tried it...the LEDS are very dim....so maybe it really is the voltage I should be working on (as per my first post) & not the current (as per post #7)?


At 100kHz PWM and 16% duty cycle, the on time of the led is only 1.6 us. The on time needs to be balanced between flickering (slow) and smooth (fast) updates for best brightness. The PWM period/frequency needs to be slowed down substantially for the eye to perceive the brightness.

Questions:
1) What PIC is being used, or is that flexible?
2) How is the PIC supply power going to be handled?
3) Are the leds going be either full on/off, global PWM/dimmable, or individual PWM/dimmable?
4) How many leds in total, there were up to 12 in another post, is the end objective,?

Ooops too slow with the reply!
5) Is the brightness at 1 kHz much better?


----------



## irksomeremnant (Jun 9, 2011)

The brightness (in general) has been fine all along (1khz, 50khz, 100khz)....it was just that one test I did (post #9) where the led was dim.

The reason I'm using such ridiculously high PWM frequency is to put it *well* outside the audio band.....the fact that there are less number of PWM steps available when I go higher in pWM frequency is actually a bonus (eg at 15khz, I have about 255 steps of PWM available, but at 100khz only about 100 steps - less is more for my simple aplication)

I'm using a 16lf1824
The power supply is a 9V battery
The leds are going to be on/off & dimmable.
There will eventually be up to 12 (or maybe 98 - I haven't decided yet), but to keep this all nice & simple while learing, I'm focusing on a simple array of two (in series)...oince I've squared this all away in my head, I'll scale up to suit.

Now as it goes, I'm inclined to think that my new meter reading is sort of correct (since the LED brightness seems to mate with it's AC current meter readings) but it'd be nice to align them with some formula or other!

*Edit:* Doh, first problem....I still had my program's 'duty' variable set as a byte (until these tests I'd no need for it to be set for a word...max steps I'd needed was always less than 255)...this means for all I thought I was setting my duty to be 500 ....it was still just 255! Schoolboy error...ok, now for some more testing!


----------



## nickelflipper (Jun 9, 2011)

Huh, the new enhanced 14bit PIC's have a muxed comparator like the MSP430, very nice.

Furthering upon bbawkon's constant current suggestion:
Take a read of the PIC16f1824 data sheet paragraph at chapter 19.9, for creating a feedback loop to the ECCP module. Set FVR to 1.024V, DAC to 1/32 of FVR, and send it along to the comparator for a reference voltage of 0.032V. Using a sense resistor of 1R8 or 1R6 gives a 18-20ma led current. Make a constant PWM freq. and d.c. for the minimum battery voltage conditions, and let the comparator feedback loop handle/follow the max voltage down to the minimum. That's if I read everything right?

If a large number of leds, say more than eight?, then sampling a chip like the TLC5947 could be the way to go. I have been working with a couple of TLC59401's and an 8x8 rgb muxed array, and it's been a tussle.


----------



## irksomeremnant (Jun 9, 2011)

Excellent...you're obviously well versed in all this *and* PICs!

I was following you until this bit...

_"1R8 or 1R6 gives a 18-20ma led current" _

_??? _

Presumably what you're angling towards is a very small fixed voltage set within the PIC to have the PIC compare against a 'sensed' incoming voltage from a low value resistor in series with the LEDs? (not sure that would work becuase these LEDs aren't gojn g to be simply on or off....but going to be dimmable, which if dimmed then knock the 'sensed' feedback voltage out of the whack, sending the feedback loop into a tizz (or have I not grasped your idea properly?)


----------



## nickelflipper (Jun 9, 2011)

irksomeremnant said:


> Excellent...you're obviously well versed in all this *and* PICs!
> 
> I was following you until this bit...
> 
> ...


In the end, I think a purpose built led driver is the solution. Having the micro and driver under the guiter pickup has got to be a challenge. Get a donor guitar, and get the chisel out.


----------



## Steve K (Jun 9, 2011)

I'm still trying to understand why the 'tronics need to be located close to the pickup. why not locate the 'tronics on the back of the guitar body or neck, and run some thin 30 ga wire (twisted pair, please) to the leds? That would allow a conventional switching power supply to be used. 

.... or use a simple current regulator, which could be located anywhere, and might be only slightly less efficient (if at all) than other schemes.....

regards,
Steve K.


----------



## irksomeremnant (Jun 9, 2011)

I mentioned earlier why...



irksomeremnant said:


> Thanks guys.
> 
> ...I've no space in my guitar for a circuit board (without taking a chisel to it...that's not gonna happen!)


 
I want this to go inside my guitar...not be an external solution.

I've now got this squared away all this LED current melarkey in my head now...I was simply getting RMS current muddled with average current ....it's average current I need to know (along with peak current), but I'm now happy with calculating that.

Many thanks for bearing with me & all your input...it's greatly appreciated.


----------



## 2xTrinity (Jun 17, 2011)

If you're concerned about PWM causing EMI, why not make a linear regulator. You already have a fairly complicated setup with microcontroller, FET, caps etc. 

You could actually do this with a simple OpAmp. Have the output of the opAmp drive an LED in series with a sense resistor. Set the inverting input (-) to measuring the voltage drop across a sense resistor, essentially feeding back a voltage proportional to the current through the LED. Have the inverting (+) input measuring a fixed voltage reference -- such as zener diode with a voltage-divider network in parallel (these resistors can be in the high meg-ohm range to keep parasitic drain low).

I_LED = V_ref/R_sense 

If the OpAmp alone doesn't have enough headroom to properly drive the LED as the battery drains, you can use the OpAmp to drive the gate of FET. So long as you still feedback the voltage across the sense resistor, the circuit should still work.


----------



## MikeAusC (Jun 18, 2011)

irksomeremnant said:


> So I want to run two white LEDs off a 9V battery....(presently dabbling with LEDS/PICS and a breadboard) - but I don't want a resistor to 'waste' 25% of the overall battery useage.
> 
> So my thinking goes along the lines of using a PIC & PWM. . . . .


 
Using PWM will NOT improve efficiency one percent, because you still need to transform the battery voltage down to the LED voltage.

Using Switchmode with an Inductor will improve efficiency. It's like PWM - but energy is stored in the inductor rather than dissipated.

Using a toroidal inductor will stop radiated interference. My 200 watt Amplified Mixer has the transformer a few centimetres from the millivolt Mic level input - it's a toroidal transformer.


----------



## irksomeremnant (Jun 19, 2011)

Thanks.

Just as an aside....meter readings.

Imagine this setup...

Two white LEDS (forward voltage of about 3.2V, therefore 6.4V in total)...with a series resistor of about 130R - this will mean about 20mA through the leds (so a 2.6V drop across the resistor)

Ok, now if we say make the PWM duty cycle 50% ....as measured with a True RMS DVM (a Uni-T 61-E .... http://www.uni-trend.com/UT61E.html )...what voltage would you expect to see such a meter display across the resistor (in other words at 100% duty, the meter will obviously measure the 2.6V drop across the resistor, but what do you think will be displayed on the meter at 50% duty?)...I'm still trying to wrap my head around being able to measure/confirm the effect of PWM changes....and what the limitations a True RMS DVM may have when trying to make PWM measurements


----------



## MikeAusC (Jun 19, 2011)

50% of the time the resistor has 2.6 volt across, 50% of the time the resistor has zero volts across it, so the average voltage is 1.3 volts.

A basic multimeter would give an unknown reading here - it depends on how it samples the input voltage. For confident reading of the average of of a varying voltage you can use a smoothing filter - a 100kohm series resistor to a 1000microfarad cap.

For square waves you don't need an RMS multimeter - averaging will give you the same result.


----------



## irksomeremnant (Jun 20, 2011)

Well, I've just tried measuring with my DVM....the DVM seems to be measuring the PWM 'average" just fine. For example, the 50% duty measured 'voltage drop' across the series resistor is half that of the measured 100% voltage drop (as seen on the DVM display) ...and that's at a rather high PWM frequency of 48khz!

re efficiency...let's see if I can square this one away in my head - I'm using a 22R resistor, which has been chosen for a supply voltage 6.8V - even though I then use a 9V supply, I PWM the supply voltage to LEDS for an average current of about 22mA through the resistor (& therefore LEDs)

Surely (& this is the bit I'm not 100% clear about) about 6% is being 'wasted' across the resistor by using PWM for an average current of 22mA (ie 6.4V across the two LEDS & 0.4V across the series resistor) ....vs 29% being wasted across the resistor if I hadn't used PWM & just went with a series limiting resistor (9v-6.4 =2.6V across the resistor) ...have I got this bit right?

the other bonus here is that I can maintain constant brightness of the leds all the way down to 6.8V (by monitoring the battery & as it fades I increase the PWM).


----------

