// Digital capacitor tester // (C) 2013 Hackerstore, see https://www.hackerstore.nl/BlogArtikel/4 // update 2018 alexP // See diagram cap-esr-freq.jpg for hardware connections. #include #include #define ANALOG_PIN A5 // Analog pin for measuring. The value can be between 0 and 1023. #define P1_PIN 12 // Pin for charging the capacitor via R9 & P1. #define R8_PIN 7 // Pin for discharging the capacitor via R8. float MicroFarad =0; long tStart =0; float nDuur =0; int wait =2000; LiquidCrystal lcd(13,11,6,4,3,2); void setup() { pinMode(A4, INPUT_PULLUP); // button S3 connected for reset. pinMode(P1_PIN, OUTPUT); // connection for charging the capacitor. digitalWrite(P1_PIN, LOW); lcd.begin(16, 2); // lcd screen with 2 rows with each 16 characters. } void loop() { if(digitalRead(A4)== LOW) // reset the software via button S3. {softReset();} Discharge(); // discharge the capacitor until zero voltage. /* Measuring the value of the capacitor: Capacitance is a measure of the ability of a capacitor to store electrical charge. The Arduino capacitance meter relies on the same basic property of capacitors: the time constant. The time constant of a capacitor is defined as the time it takes for the voltage across the capacitor to reach 63.2% of its voltage when fully charged. An Arduino can measure capacitance because the time a capacitor takes to charge is directly related to its capacitance by the next equation: TC = R x C. TC is the time constant of the capacitor (in seconds). R is the resistance of the circuit (in Ohms). C is the capacitance of the capacitor (in Farads). The formula to get the capacitance value in Farads is C = TC/R. In this meter the R value can be set for calibration between 15kOhm and 25 kOhm via potmeter P1. In the formula for the calculation a value of 22 kOhm is used. the full analog value on the ANALOG_PIN is 1023, so 63.2% is a value of 647. */ lcd.setCursor(0,1); lcd.blink(); // cursor blinks during measuring. digitalWrite(P1_PIN, HIGH); // charge the capacitor. tStart = millis(); while(analogRead(ANALOG_PIN)<647){}; nDuur = abs(millis()-tStart); MicroFarad = (nDuur/22000.0) * 1000; // calculate the value of the capacitor. delay(wait); lcd.noBlink(); // show the calculated value on the lcd screen: if(MicroFarad >= 1.0){ // show the value in microFarad: lcd.clear(); lcd.print(" "); lcd.print(MicroFarad,1); lcd.print(" "); lcd.print((char)228); // print the greek character mu. lcd.print("F "); delay(wait); } else{ // show if no capacitor is connected or the value is too low to be accurate: if (MicroFarad < 0.07) {lcd.clear(); lcd.print(" no capacitor");} // show the value in nanoFarad: else{ lcd.clear(); lcd.print(" "); lcd.print(MicroFarad*1000,1); lcd.print(" "); lcd.print("nF "); delay(wait); } } } // discharge the capacitor for the next measuring. void Discharge() { digitalWrite(P1_PIN, LOW); pinMode(R8_PIN, OUTPUT); digitalWrite(R8_PIN, LOW); while(analogRead(ANALOG_PIN)>0){} ; pinMode(R8_PIN, INPUT); delay(wait); } // reset procedure void softReset(){ lcd.clear(); lcd.setCursor(0,0); lcd.print("reset..."); delay(wait); asm volatile (" jmp 0"); }