Please tell me I'm dumb. I've setup a simple capacitor charging circuit where my Arduino supplies the circuit power from a digital pin, and measures the capacitor voltage as it charges.
My problem is my sensor results aren't showing a smooth voltage curve, there is a short period in the first 10-15 sensor samples after I turn the supply pin on (digitalwrite HIGH) that show a much slower charge rate before changing to its actual charge rate.
I attached 2 sets of results, the red graph shows the capacitor charging with the first 10-15 samples different to the actual charging rate.
The green graph shows the same setup but I altered the code slightly to run twice, you can see that the same 10-15 non ideal samples are there again, but in the second charge/discharge the response is completely ideal.
I was reckoning that there's some current limiting magic happening from the digital pin where it doesn't like the initial current rush to charge the capacitor, but i tried a variety of resistor/capacitor setups and they all have the same 10-15 sample period - besides, the green graph results show that in the second charging/discharging the pin is able to provide for an ideal response.
I guess I should clarify that I need an ideal response for a system identification project. If possible I need someway of disabling/bypassing whatever is causing this non ideal period, or if I'm doing something else that is completely messing with it please tell me I'm dumb.
Here is my beginner level code that produces the red graph results:
int Sensor = A0; //Sensor pin
float SenVal = 0; //Float for sensor readings
int Supply = 8; //Supply Pin
void setup() {
Serial.begin(9600);
pinMode(Supply, OUTPUT); //Supply pin is an output
digitalWrite(Supply, LOW); //Supply is initially off
}
void loop() {
SenVal = analogRead(Sensor); //Sample and print (will be 0.0 as supply is off)
Serial.println(SenVal);
digitalWrite(Supply, HIGH); //Supply 5V step
while(1){
SenVal = analogRead(Sensor); //Sample and print until Capacitor charged to 5V
Serial.println(SenVal);
if (SenVal > 1020){ //Stop and switch off supply (capacitor will charge down)
while(1){
digitalWrite(Supply, LOW);
}
}
}
}
Sorry for the long post, I tried to be as concise as possible. Also sorry if this isn't the best place for this post, i wasn't sure.
-----------------EDIT: SOLUTION-----------------
MrMark:
Mook, I think...
Yes! This was the problem, and as usual it all makes so much sense now
Thanks everyone for helping (karma++), I went through all solutions and am grateful for finding one so quickly.
I have attached new test results that I just produced as proof of the solution for anyone who might be interested. It compares the old code from the OP (top) with the new code included in this post (bottom)
Here is the new code exactly as MrMark suggested:
Changes from OP code
-115200 baud instead of 9600.
-Inclusion of delay(1) to dominate the sample time.
int Sensor = A0; //Sensor pin
float SenVal = 0; //Float for sensor readings
int Supply = 8; //Supply Pin
void setup() {
Serial.begin(115200);
pinMode(Supply, OUTPUT); //Supply pin is an output
digitalWrite(Supply, LOW); //Supply is initially off
}
void loop() {
SenVal = analogRead(Sensor); //Sample and print (will be 0.0 as supply is off)
Serial.println(SenVal);
digitalWrite(Supply, HIGH); //Supply 5V step
while(1){
SenVal = analogRead(Sensor); //Sample and print until Capacitor charged to 5V
Serial.println(SenVal);
delay(1);
if (SenVal > 1020){ //Stop and switch off supply (capacitor will charge down)
while(1){
digitalWrite(Supply, LOW);
}
}
}
}
Also thank you TomGeorge for posting my attachments :). I didn't know you could, and have now included them here.