# Charge discharge curve of a capacitor, Arduino

I'm trying to visualize the charge discharge curve of a capacitor with Arduino. This is my curcuit:

simulate this circuit – Schematic created using CircuitLab

13 is pin 13 of the Arduino and A0 is the analog pin 0 that I use to read voltages at that point.

What I do is to set 13 to HIGH/LOW with time spans of around 4.7 seconds, which is the loading time for that capacitor with the 10k resistor.

I expect that the charging should be slow, so I should get, when 13 is set to HIGH, a growing curve that doesn't really make it to the asymptotic value. When 13 is set to LOW, the discharging is going to be done though the 220 resistor, so it should be faster, and while voltage at that point decreases to 0, I should see the curve get down really fast.

Am I right or wrong in something (clearly wrong). The thing is that after getting the data through Serial and plotting it, I get just the opposite of what I expected:

So... what is going on?

What's the purpose of the diode? If that's an LED, then it's clamping the voltage at around 1.9V, hiding most of the exponential curve that you are looking for.

In other words, the rising edge of your waveform is just the first part of an exponential curve that is asymptotically headed toward 5V, but gets stopped at 1.9V by the clamping action of the diode. The falling edge is the entire view of an exponential curve that starts at the diode clamp voltage, and is headed toward 0V — but gets interrupted by the next charge cycle.

• The purpose of that was just to watch a LED fade in and out. I am aware that the diode falsifies the curve, but even with that, I still don't understand how the curve is like that... if the discharge is across the diode, then the discharge should be really fast (only 220 ohms), and I should see the asymptotic value of the exponential curve that goes down, shouldn't I? The semiperiod of the signal is about 5 seconds. – MyUserIsThis May 3 '13 at 12:14
• No, the current through the diode is negligible, limited by the 10K resistor during the time that it is clamping the voltage. As soon as the discharge starts, the diode stops conducting, and the shape of the curve is dictated by the 10K resistor. – Dave Tweed May 3 '13 at 12:34
• Musn't the C1 discharge through the diode when both 13 and GND are set to voltage 0? – MyUserIsThis May 3 '13 at 15:47
• No, all diodes have a forward voltage drop that prevents current from flowing whenever the applied voltage is less than that value. For a silicon diode, this is around 0.7V. For your LED, it's more like 1.9V. – Dave Tweed May 3 '13 at 16:01
• Didn't know that. I will try without the diode and see what I get. Thanks. – MyUserIsThis May 3 '13 at 16:03

I don't have a lot of experience in electronics, so this may be wrong, but here is my guess:

Your capacitor is actually across a voltage divider. (see http://en.wikipedia.org/wiki/Voltage_divider). The voltage across him will thus be:

(R2 / (R1 + R2)) * Vin


In your case, that would be about 0.11v. Not sure how the diode will react in that case, it might lower the voltage even more.

As you said, the time constant Ï„ of your capacitor in this case is 4.7s. This means that C1 will get 63% of its remaining charge every 4.7s. I don't see the units on your chart sadly, so I can't confirm this. But it seems fair that the capacitor will charge to 0.11v in around 5*4.7s. (which doesn't look to match the chart, btw...)

From there, as soon as you set 13 to LOW, C1 discharges through R2 and D1. As it discharges, the voltage drops, and therefore the current as well.

I = U/R
as C1 discharges, U drops, therefore I drops as well


That explains why C1 discharges slower and slower.

As far as I know how things work, this seems quite possible to me. Hope it's right and it helps :) I'd be interested to know the units on your chart axes however.

• The units of the vertical axis is in volts when multiplied by 5/1023, because the reading is done from 0-1023, meaning 0-5 volts. The horizontal axis... unfortunately the board took too much time in some functions and I don't have a precise value... but approximately 5 seconds is the semiperiod of the signal that you see. – MyUserIsThis May 3 '13 at 12:18
• When you say "From there, as soon as you set 13 to LOW, C1 discharges through R2 and D1. As it discharges, the voltage drops", I understand that. The thing is that this should be faster than the charging, because the resistor of the diode is smaller, so the curve that goes don should get really close to the asymptotyc value, while it doesn't, that's what my doubt is. – MyUserIsThis May 3 '13 at 12:19
• Not sure about that last one. Since the initial voltage is 5v across 10kÎ©, it makes a current of 0.5mA. And 0.11v across 220Î© makes 0.5mA as well. If that is correct, it means there is the same current in and out C1. However, the 5v is constant (since it is provided by the arduino), whereas the 0.11v decreases over time (since it is provided by C1). Therefore discharging takes more time in the end. (And that is, if my reasoning is correct...) – Antoine_935 May 3 '13 at 12:31
• Note that according to the graph, your voltage rises to ~1.9v. As Dave highlights, the diode may play a specific role here, forcing the tension to rise to 1.9v before letting the current through. – Antoine_935 May 3 '13 at 12:40
• I think I still don't understand how you reason that the discharge once 13 is set to voltage 0 should take more time than charging... when you say that the charging time doesn't match the chart, that's exactly what I mean. – MyUserIsThis May 3 '13 at 15:49

R2 and D1 interfere with the theoretical charging process of C1 via R1. Remove R2 and D1 and look what you get on your ADC input when pin 13 drives high. It will be as you expect. When you drive pin 13 low you'll get the same type of curve but from a high level to a low level.

If you want to discharge C1 a lot quicker you can use another pin (call it pinX) on your MCU controlling a grounded transistor that connects via the 220R to the top of the capacitor.

Sequence is pinX is low (off) and pin13 goes high - you'll see a nice charging curve that is asymptotic with the positive supply on the MCU.

Next switch pin X high and pin 13 low. This will discharge the cap pretty quickly.