# Pulse-width modulation: how to set the duty cycle using two variable sources?

For a hobby project, I am trying to build an electrical circuit in which the brightness of two LEDs can be determined using two sensors: one for light (an LDR), and one for sound (a microphone, electret is preferred). After the construction of the circuit, I would like to still be able to change the maximum brightness of the LED, as well as how much the brightness is determined by light or sound. In the supplied image, the characteristics of the system are shown.

The least trivial questions for building this circuit is how to combine the two "inputs" (sound and light) to determine a duty cycle, as well as how to set the percentage of influence of both sources. Given the following:

• max = the maximum duty cycle sent to the LEDs
• l = the amount of light observed
• s = the amount of sound observed
• p = the percentage of influence of light on the total duty cycle

I would like to know which circuit would satisfy the following conditions:

• $$\text{Duty Cycle} = \text{max}( (p(1−l)+(1−p)s )$$

Furthermore, the power source should be as small as possible (e.g. 2 AA batteries) and be able to last for 50 hours. The simplest circuit is to separately feed the sound and light sensor signals into a microcontroller with a A/D, compute the desired LED brightnesses, then output the results as PWM.

There are many micros that have at least two A/D inputs and two PWM outputs. If this were a volume product, I'd probably start by looking at the PIC 16 line. If this is a one-off, use whatver you have lying around or are comfortable with. This would be very easy to do on one of the 16 bit PICs or dsPICs, like a 24F. You'll have a hard time finding one that can't run from its internal oscillator, has two or more A/D inputs, and at least two basic PWM outputs (called Output Compare on these parts).

The parameters of your algorithm that you mention about would be build-time constants in the main include file. Changing them would as simple as changing the values in the include file, then ree-building and re-flashing the code into the micro. If you want to get more sophisticated, you could implement a UART protocol that allows changing the parameters on the fly.

• Yeah, probably the way to go. I'd love to see this implemented out of analog circuitry though – Dzarda Feb 10 '14 at 15:10

For 5-10% type accuracy, this circuit would be easy enough to play with. It has a 15V supply, so two cells are not going to work. Gain multiplication (p and (1-p)) on the pot is only approximate. Output is 0..1V (and beyond) for 0..100% PWM (I think you want it to saturate at 100%? Often no more circuitry would be required.

All in all, a $\mu$C solution would be cleaner and probably lower power, but this would be easy to play with without programming.