Using PWM to turn LEDs ON or OFF

AnalogKid

Joined Aug 1, 2013
12,174
Charging a capacitor starts with high current and quickly takes a charge.
Not if the source of the charging current is a constant-current source. (D1-D2-R2-Q2) In that case the voltage ramp across the capacitor is linear. I think this is unnecessarily complex for a simple missing-pulse detector, but that's just me.

ak
 
Since your schematic is generated in LTspice, why not show its simulation of how it works?
IMG_1661.jpeg
For 500us pulse the C1 peak voltage (green) is 1V and the C2 storage voltage (blue) 0.8V:
IMG_1660.jpeg


For 2500us pulse the C1 peak voltage (green) is 4.5 V and the C2 storage voltage (blue) 4V:
IMG_1659.jpeg
Note: By inserting a schottky (Vf=0.3V) in series with D1 the error between C1 voltage and C2 voltage can be reduced. This error is caused by too low D1 forward voltage since it is biased with too low current through 10k resistor.
 
Last edited:

crutschow

Joined Mar 14, 2008
38,569
Below is the LTspice sim of my take on a circuit to discriminate between the pulse widths using a CD4013 FF and a CD4049 inverting buffer package;

The circuit at the D and CLK FF inputs triggers the Q output high (LED OFF, bottom trace) only if the pulse width is less that about 700µs (first and last pulses).
The circuit at the FF CLR input resets the Q output to zero (LED ON) only if the pulse width is longer than about 2300µs (middle pulse).

The sim also shows that pulses of 700µs, 1500µs, and 2200µs (second and fourth set of pulses), has no effect on the FF state (nor will any pulse width between the minimum and maximum detected).

R6 (minimum width) and R8 (maximum width)may need to be tweaked to get the desired pulse-width discrimination.

1740715286192.png
 
Last edited:

Thread Starter

mindless

Joined Feb 27, 2025
16
I like this simple approach but how do I calculate R3 and R4 from the voltage readings?

My suggestion and version using a LM393 comparator.
Initial voltage readings are taken at the junction of C1 and D1 when the RC controller is set at 2.5,1.5 and 0.5ms.
From these voltage readings R3 and R4 can be calculated.
The 2.5ms PWM generated voltage will turn the output from the LM393 High, turning ON Q1 and the LEDs.
At this time the output from the second half of the LM393 pin7 goes Low reducing the voltage at pin2.
This will keep the LEDs ON when the PWM signal changes to 1.5ms but drop out at 0.5ms.
View attachment 343379
 

AnalogKid

Joined Aug 1, 2013
12,174
OK, but as the cap nears its peak voltage wouldn't it take less current?
Nope.

The circuit produces a *constant* current into the load. In this case the load is a simple capacitor, but that doesn't matter to the circuit. If the load were an incandescent light bulb, notorious for having a very non-constant resistance, the voltage across the bulb would start out increasing very slowly, and increase more rapidly as the bulb warms up. This is the opposite of a capacitor charged by a voltage source, a case you are more familiar with. Bonus, constant-current drive extends the life of the bulb by 10x - 100x.

The classic voltage equation for a capacitor (integral of i dt) reduces to a simple ramp when i is a constant. Thus, the slope of the ramp is independent of cap voltage. The voltage ramp across the cap remains constant until the current source runs out of voltage compliance; basically, it saturates against the positive rail. At that voltage there is an abrupt change in the cap voltage waveform as it goes from constant-slope to zero-slope. Switching constant currents into and out of a capacitor is a common method for producing a triangle waveform in a function generator.

This corner has / produces / whatever harmonics, which is why a trapezoid waveform may look like a decent approximation of a sine wave, but still has some of the squarewave buzz sound quality. BTW the harmonic field is less than that of a triangle wave. A common way to produce a sine wave without all of that Wein-bridge stuff is to produce a triangle wave and lowpass filter it. The harmonic field of an equivalent trapezoid waveform has less energy, so the result after the same lowpass filter will be cleaner. (I did some work with a psycho-acoustics research lab back when.)

ak
 
Last edited:

BobTPH

Joined Jun 5, 2013
11,568
Do you really need the two widely separated thresholds?

Would it work for you if the light is on above 1 ms and off below 1.5 ms? That would be far simpler. It needs only a single comparator.

Edited: Changed 1ms to 1.5ms which is the middle of the range.
 
Last edited:

Tonyr1084

Joined Sep 24, 2015
9,744
OK, but as the cap nears its peak voltage wouldn't it take less current?
Nope.

The circuit produces a *constant* current into the load. In this case the load is a simple capacitor, but that doesn't matter to the circuit. If the load were an incandescent light bulb, notorious for having a very non-constant resistance, the voltage across the bulb would start out increasing very slowly, and increase more rapidly as the bulb warms up. This is the opposite of a capacitor charged by a voltage source, a case you are more familiar with. Bonus, constant-current drive extends the life of the bulb by 10x - 100x.
This is where I'm showing my gross lack of knowledge regarding this subject. Perhaps I should start my own thread, but for now please understand I'm not trying to hijack this thread.

Here's just about all I know about a capacitor: When the charge is zero volts and a voltage (represented in red) is applied the current (represented in blue) is at its max. As the voltage rises toward the set voltage the current drops. As we get near to 4 Tau and 5 Tau the current drops to almost nothing. For the sake of argument let's assume at 4T the current is 10mA (and I have no reason for choosing that number other than the need to choose something). Even if the constant current is set at 50mA the cap isn't going to take 50mA. I'm basing ALL this on DC theory (fancy word for "All I know is DC electricity").

Screenshot 2025-02-28 at 8.26.36 AM.png

I'm not above learning something but I can't see how a nearly fully charged cap is going to continue to draw current - except for the ESR. (yeah, I know that one too). As we approach full charge, never getting to absolute full charge, the current is going to be approaching zero current. Regardless of CC. No ? ? ? I'll admit I could be wrong. That happens more often than I'd like to admit.
 

Tonyr1084

Joined Sep 24, 2015
9,744
As I'm pondering this subject, a CC circuit will raise the voltage higher and higher until 50mA is maintained. So maybe that's what I'm not fully understanding. Also, are we charging the cap to 5T? I believe I understand that a cap in a timing circuit will oscillate between 2T & 4T, changing state when one reaches other.
 

AnalogKid

Joined Aug 1, 2013
12,174
Here's just about all I know about a capacitor:
Note that for this discussion we are talking about theoretically perfect components. Capacitors do not have an internal resistance, power supplies have zero output impedance, etc.

That chart is derived from a specific situation: A constant voltage source (the output voltage does not change no matter how high or low the current) a fixed resistor from the power supply to the capacitor, and a fixed capacitor from the resistor to GND. The resistor and cap form a two-element voltage divider, and the chart is the voltage at the centerpoint, between the R and the C. In this case, as the voltage across the cap increases, the voltage across the resistor decreases. Because there are only two elements in the circuit, the cap voltage Vc and the resistor voltage Vr *must* add up to Vcc at all times because Vcc is constant. Per Ohm's Law, as the voltage across the resistor decreases, so does the current through it and into the cap. But there is much more to the universe than that one specific situation.

A constant voltage source has an output that is constant as the output current varies from 0 to infinity. This is why the voltage across the resistor decreases with charging - the voltage on one end is constant, and the voltage on the other end is changing. OTOH, a constant current source has an output that attempts to push a current that is constant as the load impedance varies from 0 to infinity. In the circuit above, this is 1.8 mA. Within the capabilities of the power supply, the voltage at the Q2 collector will do *anything* necessary to maintain 1.8 mA of collector current. Vcc is 6 V and the circuit needs a minimum of 1.2 V of operating headroom. Thus, as long as the voltage across the cap is between 0 V and 4.8 V, the charging curve of the capacitor voltage will be a straight line, not an exponential curve. Once the cap voltage reaches 4.8 V, it will sit there until something discharges it. Note that in the constant current situation there is no resistor in series with the capacitor. The charging current is controlled/limited only by the circuit.

The chart is derived from the basic capacitor equation.

Formula for calculating voltage across a capacitor


That equation can handle any current waveform: sinewave, squarewave, triangle, whatever. If you can describe it mathematically, you can integrate it. But if the current is a constant, (and you ignore the initial, pre-charge condition Vo) the integral equation reduces to this:

ec=it or e = (it)/c

e - the voltage change across the capacitor, in volts
c - capacitor value in farads
i - the constant current in amperes
t - time in seconds

For a specified constant current and capacitor value, the equation will tell you how much time it takes for the cap voltage to *change* by how many volts.

In our case, the current is 0.0018 A and the cap is 0.000001 F. If we set the time to 1 second, then the cap voltage will try to *increase* 1800 V in one second. Clearly it cannot do that, but that is equivalent to 1.8 V in one millisecond. The current source is enabled when the input PWM signal is high, so the cap voltage now is directly proportional to input pulse width. It can be buffered, filtered, and run through a comparator to determine if the pulse width is greater than or less than a target value.

Note - as I said before, I think this is an overly-complex solution to a relatively simple problem. I think a simple R-C exponential ramp is accurate enough if the component values are selected such that the trip point voltage is the 0.5RC to 1.0RC range.

ak
 
Last edited:
Top