# Combining two PWM outputs for finer granularity

I plan to use two PWMs to very slowly (exponentially) turn on an LED strip, from 0.002% to 0.004%... to 100% power over twenty minutes. 0.002% seems to be a safe brightness that doesn't seem jarring when suddenly turned on, but I can't push much higher than that. The formula I chose (for its very slow start) is: power=(2^(t*15)-1) / 2^15 where t is the normalized time, from 0 to 1. I don't care about the number of steps, so long as none is perceptually large. (So 97%→100% is fine because it's unnoticeable, but 0.002%→0.5% would be quite jarring.) 255 steps would be a great plenty if I could take a step every few seconds, but if the steps represent linear brightness increments, I need more like 51000 steps.

Both PWMs are 8-bits. The high power part PWM1 will take over when the low power part PWM2 reaches a certain level. In fact, the "low power" part of the circuit could drive the light from 0 to 100%, but that half of the circuit (left) is analog, and I can't easily model the brightness. Hence, at the level where I no longer need extremely small gradations, I'll switch over to driving the light with PWM1, which is mathematically nice but incapable of the extremely low power needed for the early steps.

Is this a reasonable circuit to drive a transistor Q2 with both a PWM signal (PWM1) and an analog voltage (after a low-pass filter from PWM2), but not at the same time?

Note: I will probably not be combining PWM1 and PWM2 dynamically in a "most significant digit / least significant digit" configuration, because the voltage->brightness response from PWM2 is way too nonlinear.

Potential problem:

The low-pass filter may not respond fast enough, so when PWM1 starts sending its signal and PWM2 turns off, the low pass filter may not step down fast enough to avoid a flash. (The flash would occur if the low-pass filter remains on during the early OFF portions of PWM1's signal.) This would manifest in a brief doubling in brightness.

Potential solution: maybe with a diode, capacitor, and a couple resistors, I can hold the base voltage of Q1 high when PWM1 becomes active, even during the off-portion the signal.

• Do you really need that resolution? – Harry Svensson Feb 24 '18 at 13:55
• @HarrySvensson At the lowest levels, I absolutely do, and more resolution would be even better. At the brighter levels, resolution becomes almost unimportant. If you try sleeping when a bright light suddenly turns on at 1/255 power, you'll find how serious this is. – piojo Feb 24 '18 at 13:56
• Look into linear-voltage-to-exponential-current converter circuits. They're common in audio synthesis applications, but might apply well here. – Russell Borogove Feb 24 '18 at 15:25
• If you don't need the frequency to be too high, you probably can use bit-banging PWM instead of timer based. What I did when building a lamp was using I2C DACs to drive LED drivers. So, a bit easier to get whatever resolution I needed. – Wesley Lee Feb 25 '18 at 18:21
• You can go for the lowest duty cycle for one period, skip the next and so on. You can’t develop electronics without an oscilloscope. – winny Feb 26 '18 at 8:10

The proposed circuitry solution is needlessly complex, and will not be economical to prototype or produce. The entire premise of the question is not fully convincing, but accepting it at face value, there are far better ways.

• As already proposed by user Dampmaskin, find a 16-bit timer. Many MCUs have these, including not only the ATmega328p in either traditional or "Arduino" forms and also very low cost Arm Cortex M0 parts.

• Alternately, implement PWM with larger count widths in software, either entirely or by using software to trigger a one-shot hardware PWM

• Use one 8-bit timer in repeating mode to trigger another in one-shot mode. This lets you effectively make much smaller fractions. For example, if the repeat time fires at an interval of 10 milliseconds, but the on-time timer counts from a timebase of microseconds, your maximum on time would be 255 microseconds, but your repeat is 10000 microseconds, yielding a range of 0.01% to 2.55%. When you need higher percentages, start reducing the repeat interval which functions as the divisor. This can easily let your create on time percentages too low to have any response from a typical LED at all.

The advantage all of these have is that the output to the PWM power modulator is a signal from a single MCU pin. At most, triggering one timer from another requires a single loopback wire or resistor from the output of one to the trigger input of another.

• Commercial dimmers are noticably jerky. He wants to emulate smooth sunrise now. This demands more attention to specs and thus a more complex solution. It could be done in software with the right specs. – Tony Stewart Sunnyskyguy EE75 Feb 25 '18 at 19:24
• As the above schemes are ratio-based, they are variable with extremely fine granularity. – Chris Stratton Feb 25 '18 at 20:09
• Eh, 0.01% isn't fine granularity with respect to perception. But your suggestions are workable, particularly if I buy a chip to drive the MOSFET. However, based on what I've recently read, MOSFETs just can't switch fast enough, even if they're driven with high current. I'm sure the perfect transistor exists, but I haven't found it. If I can smooth the current out (after the MOSFET), all your suggestions would work. And I should at least give that a try, but I can't predict what issues may arise. – piojo Feb 27 '18 at 14:37

This is not intended to be a complete DIY solution but background needed to design your specs for the desired transfer function at low levels, high levels and the entire range. You may want to test the actual current values from 9 to 14V and see what values you want for each PWM level. 10 bit resolution log resolution may be ok or not.

The eye theory http://www.telescope-optics.net/eye_intensity_response.htm Only need Photopic curve ( bright).

The psychophysics of human response gain and exponent. https://en.wikipedia.org/wiki/Stevens%27s_power_law

A 10-bit PWM software solution http://jared.geek.nz/2013/feb/linear-led-pwm

An analog lower current solution with some theory. http://www.presepioelettronico.it/Public/data/amministratore/20141214144447_AntiLogDrive.pdf

When implementing Op AMps, you must be concerned about Input & output common mode range limitations for BJT OpAmps as Rail to Rail are good but tend to have low output current.

The transfer function of a commercial Sunrise alarm clock with brightness & alertness vs alarm time. Brightness need not be full when waking up, to give time for eyes to respond. An hour may be too long for most workers.

This a modified S curve.

• Thanks! I just added details (like the formula and the initial acceptable step sizes) to the question. I'm keeping the formula, since I need something a bit different than linear brightness response--the goal is to not wake a person (me) up until near the end of the time interval--the brightness is supposed to sneak up on you, but not so much that your body hasn't started preparing to wake up. I don't know the theory of this, so I've been playing it by ear. I found a model of sunrise ramped up too quickly, so I wrote and tuned a formula. – piojo Feb 25 '18 at 18:50
• that transfer function must be included in your spec ( question) You may end up with an S curve. Point form for specs is best. Like a datasheet. – Tony Stewart Sunnyskyguy EE75 Feb 25 '18 at 19:16