Hi everyone, I have a problem : I have to measure the time constant of RC circuit. In the theory, time period between Vmin and Vmax is equal 5T , T=RC is the time constant. So, I try to measure time between Vmax and Vmin, but I can't understand why it not work. Anyone please help me!
My code here:
float Vmax=0;
int wait_time=5000;
float Vmin=0.2;//Vin
float preval=0;
long period,current,t;
float V, Vmin_val,Vmax_val,Vnew;
void setup() {
Serial.begin(115200);
}
void loop() {
t=millis();
if(t>wait_time){ //to ensure that the voltage value (max,min) is stable
analogReadResolution(12);
V=(float)analogRead(A1)*3.3/4095;
if(V<Vmin) Vmin=V;
if(V>Vmax) Vmax=V;
Serial.print("----Vmax = ");
Serial.println(Vmax);
Serial.print("----Vmin = ");
Serial.println(Vmin);
Serial.print("----V = ");
Serial.println(V);
Vnew=(float)analogRead(A1)*3.3/4095;
Serial.print("----Vnew = ");
Serial.println(Vnew);
if (Vnew==Vmin) {
preval=millis();//start count
if (Vnew==Vmax) {
current = millis();
period =(current-preval)/5;
preval=current;}
}
Serial.print("----period = ");
Serial.println(period);
}
}
Please indent your code correctly, that's important to be readable.
From what I can see the key piece of logic is (after formatting readably):
if (Vnew==Vmin)
{
preval = millis();//start count
if (Vnew==Vmax)
{
current = millis();
period =(current-preval)/5;
preval = current;
}
}
Which makes no sense - how can Vnew both be equal to Vmin and Vmax at the same time?
Why are you setting preval and then current to millis() without any intervening time passing?
I also note that analogResolution call belongs in setup(), not loop().
One thing that's definitely wrong is testing for equality between analogRead values. ADCs have error,
you cannot rely on repeatability to the least significant bit.
What you are trying to do, measure an RC time constant, requires
triggering something to start charging or discharging the capacitor.
waiting for the voltage to fall/rise past a threshold
Waiting in loop() should be done with an if clause - loop is always cycling round, so all you
need is an 'if' to detect the threshold is reached/passed. That can then call the function to
calculate and report the results.
There needs to be some event to record the starting time/voltage and trigger the discharge.
I think thats what your wait_time thing is?
I use a square wave to supply a RC circuit in order to modulate charge and discharge process of capacitor, I aim to know the value of R and C, so firstly I have to determine the time constant T of this circuit. The voltage I read in the program is the capacitor voltage.
I will try again with your suggestions. Thank you for your responses.
Thank you.
There does not seem to be anywhere on that circuit that connects the square wave to the Arduino, therefore I can't see how it would know that the capacitor has started charging.
Rather than use a square wave I think it would make more sense to connect the resistor to a digital output, which would initially be at 0, then set it high and start timing from that point.
I use the square wave to supply the circuit and use Arduino to read the capacitor voltage value, this image is the signal I display in the oscilloscope. My program is working, thank for all your supports.