Capacitance Measurement using Arduino

Definition

Capacitance is defined as the ability of a body to store an electric charge. The SI unit of capacitance is the farad (symbol: F), named after the English physicist Michael Faraday. A 1 farad capacitor, when charged with 1 coulomb of electrical charge, has a potential difference of 1 volt between its plates.

Capacitance Measurement

                Each 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. Larger capacitors take longer to charge, and therefore have larger time constants. An Arduino can measure capacitance because the time a capacitor takes to charge is directly related to its capacitance by the 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).

Algorithm for capacitance measurement code

  1. Set discharge pin to INPUT (so it can’t discharge the capacitor)
  2. Set charge pin to OUTPUT and make it HIGH
  3. Record the start time with millis()
  4. Check the voltage repeatedly in a loop until it gets to 63.2% of total voltage.
  5. After the cap is charged, subtract the current time from the start time to find out how long the capacitor took to charge.
  6. Divide the Time in seconds by the charging Resistance in ohms to find the Capacitance.
  7. Report the value with print on Serial monitor.
  8. Discharge the capacitor. To do this:
  9. Set the charge pin to Input
  10. Set the discharge pin to OUTPUT and make it LOW
  11. Read the voltage to make sure the capacitor is fully discharged
  12. Loop and do it again

Capacitance Measurement Circuit

Capacitance Measurement using RC timing
Capacitance Measurement using RC timing
Arduino Capacitance Measurement Circuit
Capacitance Measurement Circuit

Arduino Code for Capacitance Measurement

/*  Capacitance Measurement
* Theory   A capacitor will charge, through a resistor, in one time constant, defined as T seconds where
 *    TC = R * C
 *    TC = time constant period in seconds
 *    R = resistance in ohms
 *    C = capacitance in farads (1 microfarad (ufd) = .0000001 farad = 10^-6 farads) 
 *
 *    The capacitor's voltage at one time constant is defined as 63.2% of the charging voltage.
*/

#define analogPin      0          // analog pin for measuring capacitor voltage
#define chargePin      13         // pin to charge the capacitor - connected to one end of the charging resistor
#define dischargePin   11         // pin to discharge the capacitor
#define resistorValue  10000.0F   // 10K change this to whatever resistor value you are using
                                  // F formatter tells compiler it's a floating point value

unsigned long startTime;
unsigned long elapsedTime;
float microFarads;                // floating point variable to preserve precision, make calculations
float nanoFarads;

void setup(){
  pinMode(chargePin, OUTPUT);     // set chargePin to output
  digitalWrite(chargePin, LOW);  

  Serial.begin(9600);             // initialize serial transmission for debugging
}

void loop(){
  digitalWrite(chargePin, HIGH);  // set chargePin HIGH and capacitor charging
  startTime = millis();

  while(analogRead(analogPin) < 648){    // 647 is 63.2% of 1023, which corresponds to full-scale voltage 
  }

  elapsedTime= millis() - startTime;
 // convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ),  net 10^3 (1000)  
  microFarads = ((float)elapsedTime / resistorValue) * 1000;   
  Serial.print(elapsedTime);       // print the value to serial port
  Serial.print(" mS    ");         // print units and carriage return


  if (microFarads > 1){
    Serial.print((long)microFarads);       // print the value to serial port
    Serial.println(" microFarads");         // print units and carriage return
  }
  else
  {
    // if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad). 
    // This is a workaround because Serial.print will not print floats

    nanoFarads = microFarads * 1000.0;      // multiply by 1000 to convert to nanoFarads (10^-9 Farads)
    Serial.print((long)nanoFarads);         // print the value to serial port
    Serial.println(" nanoFarads");          // print units and carriage return
  }

  /* dicharge the capacitor  */
  digitalWrite(chargePin, LOW);             // set charge pin to  LOW 
  pinMode(dischargePin, OUTPUT);            // set discharge pin to output 
  digitalWrite(dischargePin, LOW);          // set discharge pin LOW 
  while(analogRead(analogPin) > 0){         // wait until capacitor is completely discharged
  }

  pinMode(dischargePin, INPUT);            // set discharge pin back to input
}

Result of Capacitance Measurement

                The code will wait on this line If there is no capacitance Connected

“ while(analogRead(analogPin) < 648)    // 647 is 63.2% of 1023, which corresponds to full-scale voltage”

Connect the capacitor between test terminals and observe the serial monitor will show the result. If no capacitance is connected serial terminal will not show anything.

Result of capacitance measurement
Result of capacitance measurement

Leave a Reply