Hi there,
I'm a beginner, and I'm trying to simulate a Capacitor charge-discharge cycle, without any external Capacitor, but just a R.
I've been trying for long, but I have some problem with the software (I tried something like nested loop or similar). I tried searching on the web but I didn't find anything like this. Anyone could help me?
Any advice is welcome.
Thank you very much. ![]()
Let me be the first to ask: what have you tried so far? Are you testing with an LED? Post the code you've written so far.
No, I'm not testing with an LED. My hw is just a R(power and gnd obviously) on which depend the time costant. I need to simulate the behavior of the voltage across the capacitor, not having an external capacitor.
The code I wrote is this (I know the definition of the data is missing, my problem it's the flow chart, as nested loop or whatever I need to realize my purpose):
void setup()
{
Serial.begin(9600);
Vrload = A*(3.3/4096);
Rsens = Rl*(Vheater/Vrload-1);
tau = Rsens*C;
}
void loop()
{
analogReadResolution(12);
A = analogRead(analogPin);
double Vc=0;
while (Vc<Vrh)
{
Vc=Voh-(Voh-Vrl)exp(-t/tau);
Th=taulog((Voh-Vrl)/(Vol-Vrh));
Serial.print("Vc = ");
Serial.println(Vc);
Serial.print("Thigh =");
Serial.println(Th);
}
while (Vc>=Vrh)
{
Vc=Vol-(Vol-Vrh)exp(-t/tau);
Tl=taulog((Vol-Vrh)/(Voh-Vrl));
Serial.print("Vc = ");
Serial.println(Vc);
Serial.print("Thigh =");
Serial.println(Th);
if (Vc<Vrl)
{
Vc=Voh-(Voh-Vrl)*exp(-t/tau);
};
}
Show all the code. C is used before it is defined/declared.
Can you please explain what the code should be doing? What is not working with it? Also don't forget to use code tags to make it easier to read ![]()
To simulate a capacitor charging, you need 3 things. Supply voltage, a resistor, and of course a capacitor.
With the resistor and capacitor you can calculate Tau, the time it takes to charge and discharge, which is R*C.
Then you can plug every thing into this formula.
Vc = E*(1 - e^( -Time / Tau) ).
Vc = capacitor Voltage @ time.
E = supply Voltage.
Time is usually in milliseconds, but for better results, use microseconds.
Discharge:
Vc = E*e^(-Time / Tau) .
You will need to convert the formula to C language.