Charge/discharge capacitor through diode and resistor

I'd like to charge and discharge a capacitor at different rates. The most straightforward way seems to be just providing two paths for the current, each with a resistor and diode in series.
In this situation, how do I calculate the capacitor's voltage at any given time?
My first thought was to use the standard RC formula, subtracting the diode forward voltage from the source voltage. I wasn't able to find a formula, but I was able to determine that that's incorrect.

Interesting. I guess these are pure DC tests. Are you measuring this effect with an oscilloscope and using a relatively large capacitor which you discharge between tests ?
You could also try a simulation with, say, LTspice.

No tests at all. I actually got the info from this question, but yes, I'm using 5VDC. I was hoping there'd be an easy answer, but maybe this situation is less common than I thought. I'll give the simulation a go. If that doesn't lead anywhere, I'll just use an N-channel and a P-channel FET instead of diodes.

If the issue is leakage through the diode or capacitor, as discussed in your linked article, then a simulation may not model those characteristics very accurately. However, these effects will not affect short term timing characteristics.
These may, however, have an impact if you are using a diode voltage drop to limit the maximum charging voltage on, say, a supercap.
What are you doing, incidentally ?

The voltage rises with an exponential function with time , if supplied from a voltage source . The constant fit the exponent is ~ RC in seconds . ( faraday and ohms) .
If you drove the capacitor from a constant current source the voltage would rise linearly. .

Don’t forget the voltage across a diode varies with current too .

If you charge (or discharge, but not both) at the time, you do not need the diodes; just two different value resistors.
The formulas for cap voltage vs. time are:
Vcap = Vin *(1-(2.718)^-(t/RC)) for charging,
Vcap = Vstart *(2.718)^-(t/RC)) for discharging.
Vin is the charging Voltage, Vstart is the Voltage on the cap when discharge begins, t is time in seconds,
R is in ohms and C is in farads.

This is what I'm trying to do. The circuit should output HIGH if it receives a continuous HIGH signal for >5us. Any interruption should reset this timer. I actually just need one diode to discharge the capacitor quickly. I can work out the voltage divider and hysteresis if I know how the capacitor will behave.

I made a mistake earlier - my VCC is 5V, but my MCU logic level is 3.3V, and the input will be an MCU pin.
So, VF for the 1N5817 is <0.25V at 20mA and 25C. VF decreases with decreasing current. A maximum instantaneous current of 16.5mA would flow without the diode. Therefore, I can assume VF to always be < 0.25V and the capacitor voltage during discharge to always be < (3.3-0.25) * e ^ (-t/RC). Correct?

Edit2: Never mind, that doesn't make sense. But if I instead charge through the diode, I should be able to assume VC > (3.3-0.25) * (1 - e ^ (-t/RC))...

Edit: forgot to label the diode on the schematic. pretend it says 1N5817 below it

Shazool, can you tell us what will be connected to the floating connection at the right of your diagram (traditionally the output!)

Oh, my apologies. I wasn't aware of the convention. It's connected directly to a microcontroller output.

Please refer to this tutorial (yes its by me) on schematic diagrams :slight_smile:

I'm assuming the triangle is a connection to some positive voltage to set a threshold. And presumably the op amp would be a comparator, so you would not need the feedback resisitor.

The output from a microcontroller is NOT the same as you would get from a switch. So your component values or even your circuit may need to change. You havent said what value the capacitor is, nor the unlabelled resistor.

As you want ba virtually instantaneous discharge the 220 ohm is redundant - the cap would not be storing enough energy to harm the mc output

Thank you John. I shall use Circuit Diagram in the future.

Yes, the triangle is 3.3V. I deliberately left the resistors and cap unlabelled because I was still searching for a way to figure out appropriate values. I ended up using a 30pF capacitor, 220kΩ to charge, 100kΩ/100kΩ divider to provide a reference voltage, and 220kΩ for feedback. This seems to do the job as far as I can tell. Still working out some other issues.

The op amp is a LM324. I initially had no feedback resistor, but I was getting multiple transitions.

I took out the discharge resistor as you suggested. Is this safe to do with any standard-size ceramic cap? And how does an MC output differ from a switch output?

Is this safe to do with any standard-size ceramic cap?

Uc = 0.5C V^2 Joules: so while I'd not state a boundary value, certainly if its less than 10nF yes, safe.

10,0003.33.3 = 100nJ

For comparison the capacitance of the human body is around 200pF.
However if you've walked over a nylon carpet your body could be at a few kV which is how static damage can occur Uc = 200pf * 5000^2 = 5mJ

how does an MC output differ from a switch output?

As you see from the 32U4 data sheet its not a zero impedance to ground or Vcc

Thanks again. You've cleared a lot of things up for me... always more complicated than it seems. I'll give you another karma in an hour.

My pleasure. You asked

In this situation, how do I calculate the capacitor's voltage at any given time?
My first thought was to use the standard RC formula, subtracting the diode forward voltage from the source voltage.

The exponential formula does work, but only if the values are constants. (as denoted by capital letters)

vcap = Vs (1-exp(-t/RC)

another approach is to use an iterative approach, based on one simple formula: Q = CV = It

Where: Q is the charge on the capacitor, C the capacitance (Farads), V the voltage across the cap (Volts), I the current (Amps) and t the time (seconds) the current has flowed.

In a simple circuit like this

ChargeCap.png

lets assume C is initially discharged.

vR1 = Vs - (vD +vC) ;

iC = vR/R

after a short time t

vC = iC t / Cap Where Cap is the value (in Farads) of the capacitor

we can now go back and repeat this performing the calculation after another period t.

This gives an iterative process which you can easily set up in EXCEL; however it gets interesting when you add in the diode equation to get vD. I'll look at this later - for fun

(the virus restrictions have a lot to answer for!)

ChargeCap.png