#include <stdint.h>
#define CPIN 4
void setup() {
pinMode(CPIN, INPUT);
Serial.begin(115200);
delay(10);
Serial.println("start");
}
void loop() {
unsigned long cycleStart, cycleStop, chargeTime;
// drain cap first
pinMode(CPIN, OUTPUT);
digitalWrite(CPIN, LOW);
Serial.print(micros()); Serial.print(" - ");
Serial.println("Discharging...");
delay(5000);
pinMode(CPIN, INPUT_PULLUP); // now start charging via pullup
Serial.print(micros()); Serial.println(" - Charging!!!");
cycleStart = micros();
while (digitalRead(CPIN) == 0) {yield();};
cycleStop = micros();
chargeTime = cycleStop - cycleStart;
Serial.print(cycleStop); Serial.print(" - Done...");
Serial.print(chargeTime);
Serial.println("ms");
delay(1000);
}
I got your sketch working with 100k resistor and this code:
#include <stdint.h>
#define CPIN 4
#define DPIN 5
void setup() {
pinMode(CPIN, INPUT);
pinMode(DPIN, INPUT);
Serial.begin(115200);
delay(10);
Serial.println("start");
}
void loop() {
unsigned long cycleStart, cycleStop, chargeTime;
// drain cap first best via both pins
pinMode(DPIN, OUTPUT);
pinMode(CPIN, OUTPUT);
digitalWrite(DPIN, LOW);
digitalWrite(CPIN, LOW);
Serial.print(micros()); Serial.print(" - ");
Serial.println("Discharging...");
delay(5000);
digitalWrite(CPIN, HIGH); // prepare charging yet not possible because drainPin still low
pinMode(DPIN, INPUT); // now start really by tristating drainPin
Serial.print(micros()); Serial.println(" - Charging!!!");
cycleStart = micros();
while (digitalRead(DPIN) == 0) {yield();};
cycleStop = micros();
chargeTime = cycleStop - cycleStart;
Serial.print(cycleStop); Serial.print(" - Done...");
Serial.print(chargeTime);
Serial.println("ms");
delay(1000);
}
pick whatever you like. if you are after small C's you'll need the one with the charging resistor