Capacitance Meter Example

Learn about capacitance meters and RC time constants

RCSchem

Overview: A resistor will charge a capacitor in TC seconds, where:

  • TC = R * C

  • TC = time constant in seconds

  • R = resistance in ohms

  • C = capacitance in farads (1 microfarad [ufd] = .000001 farad = 10^-6 farads )

The voltage at 1 Time Constant equals 63.2% of the charging voltage.

  • Example: 1 megohm * 1 microfarad = 1 second
  • Example: 10k ohms * 100 microfarad = 1 second

Experimental Setup

This sketch works because the Arduino pins can be in one of two states, which are electrically very different.

  • Input State (set with pinMode(pin, INPUT);)

  • High Impedance (resistance) - Makes very little demand on the circuit that it is sampling

  • Good for reading sensors but not lighting LED's

  • Output State (set with pinMode(pin, OUTPUT);)

  • Low Impedance - Can provide 40 mA source (positive voltage), or sink (negative voltage)

  • Good for lighting LED's, driving other circuits - useless for reading sensors.

Additionally the pins can be HIGH (+5 volts), to charge the capacitor; or LOW (ground) to discharge the capacitor

CapacitanceMeterSchem

Algorithm for capacitance meter sketch

  • Set discharge pin to INPUT (so it can't discharge the capacitor)

  • Record the start time with millis()

  • Set charge pin to OUTPUT and make it HIGH

  • Check the voltage repeatedly in a loop until it gets to 63.2% of total voltage.

  • After the cap is charged, subtract the current time from the start time to find out how long the capacitor took to charge.

  • Divide the Time in seconds by the charging Resistance in ohms to find the Capacitance.

  • Report the value with serial.print

  • Discharge the capacitor. To do this:

  • Set the charge pin to Input

  • Set the discharge pin to OUTPUT and make it LOW

  • Read the voltage to make sure the capacitor is fully discharged

  • Loop and do it again

Arduino Sketch

1/* RCTiming_capacitance_meter
2
3 * Paul Badger 2008
4
5 * Demonstrates use of RC time constants to measure the value of a capacitor
6
7 *
8
9 * Theory A capcitor will charge, through a resistor, in one time constant, defined as T seconds where
10
11 * TC = R * C
12
13 *
14
15 * TC = time constant period in seconds
16
17 * R = resistance in ohms
18
19 * C = capacitance in farads (1 microfarad (ufd) = .000001 farad = 10^-6 farads )
20
21 *
22
23 * The capacitor's voltage at one time constant is defined as 63.2% of the charging voltage.
24
25 *
26
27 * Hardware setup:
28
29 * Test Capacitor between common point and ground (positive side of an electrolytic capacitor to common)
30
31 * Test Resistor between chargePin and common point
32
33 * 220 ohm resistor between dischargePin and common point
34
35 * Wire between common point and analogPin (A/D input)
36
37 */
38
39#define analogPin 0 // analog pin for measuring capacitor voltage
40#define chargePin 13 // pin to charge the capacitor - connected to one end of the charging resistor
41#define dischargePin 11 // pin to discharge the capacitor
42#define resistorValue 10000.0F // change this to whatever resistor value you are using
43
44 // F formatter tells compliler it's a floating point value
45
46unsigned long startTime;
47unsigned long elapsedTime;
48float microFarads; // floating point variable to preserve precision, make calculations
49float nanoFarads;
50
51void setup(){
52
53 pinMode(chargePin, OUTPUT); // set chargePin to output
54
55 digitalWrite(chargePin, LOW);
56
57 Serial.begin(9600); // initialize serial transmission for debugging
58}
59
60void loop(){
61
62 digitalWrite(chargePin, HIGH); // set chargePin HIGH and capacitor charging
63
64 startTime = millis();
65
66 while(analogRead(analogPin) < 648){ // 647 is 63.2% of 1023, which corresponds to full-scale voltage
67
68 }
69
70 elapsedTime= millis() - startTime;
71
72 // convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ), net 10^3 (1000)
73
74 microFarads = ((float)elapsedTime / resistorValue) * 1000;
75
76 Serial.print(elapsedTime); // print the value to serial port
77
78 Serial.print(" mS "); // print units and carriage return
79
80 if (microFarads > 1){
81
82 Serial.print((long)microFarads); // print the value to serial port
83
84 Serial.println(" microFarads"); // print units and carriage return
85
86 }
87
88 else
89
90 {
91
92 // if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad).
93
94 // This is a workaround because Serial.print will not print floats
95
96 nanoFarads = microFarads * 1000.0; // multiply by 1000 to convert to nanoFarads (10^-9 Farads)
97
98 Serial.print((long)nanoFarads); // print the value to serial port
99
100 Serial.println(" nanoFarads"); // print units and carriage return
101
102 }
103
104 /* dicharge the capacitor */
105
106 digitalWrite(chargePin, LOW); // set charge pin to LOW
107
108 pinMode(dischargePin, OUTPUT); // set discharge pin to output
109
110 digitalWrite(dischargePin, LOW); // set discharge pin LOW
111
112 while(analogRead(analogPin) > 0){ // wait until capacitor is completely discharged
113
114 }
115
116 pinMode(dischargePin, INPUT); // set discharge pin back to input
117}

Further things to try

  • Substitute in larger resistors if the charging time is too short, smaller resistors if the charging time is too long.

  • Measure capacitors in parallel and in series, check to see if your observations agree with electronic theory

  • Average together a group of readings for more accuracy

  • Swap out several charging resistors on different pins to make an "auto-ranging" capacitance meter

  • Modify the sketch to have the charging resistor also discharge the capacitor. Note that the reported value is not twice the value reported when it only charges this capacitor. Explain this. Hint - study the changing curve on the graph.

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.