Tutorial.CapacitanceMeter History

Hide minor edits - Show changes to markup

June 06, 2009, at 06:00 AM by Paul Badger -
Changed line 43 from:
  • Set charge pin HIGH
to:
  • Set charge pin to OUTPUT and make it HIGH
Deleted line 51:
  • Set discharge pin back to INPUT
June 06, 2009, at 05:58 AM by Paul Badger -
Added line 47:
  • Report the value with serial.print
Changed lines 51-52 from:
  • Read the voltage to make sure the capacitor is fully discharged
to:
  • Read the voltage to make sure the capacitor is fully discharged
  • Set discharge pin back to INPUT
June 06, 2009, at 05:55 AM by Paul Badger -
Changed lines 138-144 from:
  • Modify the sketch to have the charging resistor also discharge the capacitor. Note that the value is not twice the value reported when it only charges this capacitor. Explain this. Hint - study the changing curve on the graph.

to:
  • 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.

June 06, 2009, at 05:54 AM by Paul Badger -
Added lines 133-134:
  • Substitute in larger resistors if the charging time is too short, smaller resistors if the charging time is too long.
Deleted line 136:
  • Fight with "Serial.print" to print out decimal point and fraction (see Stopwatch sketch)
Changed lines 138-143 from:
  • Substitute in larger resistors if the charging time is too short, smaller resistors if the charging time is too long.

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

October 20, 2008, at 05:44 AM by Paul Badger -
October 20, 2008, at 05:41 AM by Paul Badger -
Changed lines 36-41 from:

Millis function - good for timing events

  • Record the start time
  • Cause the event to occur
  • Check the time and subtract the start time to find the elapsed time of the event
  • For short events put the event in a loop and do it many times
to:
October 11, 2008, at 02:04 AM by David A. Mellis -
Changed line 141 from:
  • Swap out several charging resistors on different BBB (Arduino) pins to make an "auto-ranging" capacitance meter
to:
  • Swap out several charging resistors on different pins to make an "auto-ranging" capacitance meter
October 09, 2008, at 04:51 PM by Paul Badger -
Changed lines 141-148 from:
  • Swap out several charging resistors on different pins to make an "auto-ranging" capacitance meter
    • Substitute in larger resistors if the charging time is too short, smaller resistors if the charging time is too long. By using some extra Arduino pins, you could make the capacitance meter auto-ranging.

to:
  • Swap out several charging resistors on different BBB (Arduino) pins to make an "auto-ranging" capacitance meter
    • Substitute in larger resistors if the charging time is too short, smaller resistors if the charging time is too long.

October 09, 2008, at 04:49 PM by Paul Badger -
Changed lines 50-51 from:
  • After the cap is charged, subtract the current time from the start time
  • to find out how long the capacitor took to charge.
to:
  • After the cap is charged, subtract the current time from the start time to find out how long the capacitor took to charge.
October 01, 2008, at 05:42 AM by Paul Badger -
Changed lines 12-13 from:
  • C = capacitance in farads (1 microfarad (ufd) = .0000001 farad = 10^-6 farads )
to:
  • C = capacitance in farads (1 microfarad [ufd] = .0000001 farad = 10^-6 farads )
September 27, 2008, at 04:19 PM by David A. Mellis -
Changed lines 24-25 from:

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

to:

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

September 27, 2008, at 03:16 AM by Paul Badger -
Changed lines 6-7 from:

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

to:

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

September 27, 2008, at 03:13 AM by Paul Badger -
September 27, 2008, at 03:11 AM by Paul Badger -
Changed line 66 from:
 *    T = R * C
to:
 *    TC = R * C
Changed line 68 from:
 *    T = time in seconds
to:
 *    TC = time constant period in seconds
September 27, 2008, at 03:09 AM by Paul Badger -
Changed lines 8-10 from:
  • T = R * C

  • T = time in seconds
to:
  • TC = R * C

  • TC = time constant in seconds
Changed line 17 from:

Example: 1 megaohm * 1 microfarad = 1 second\\

to:

Example: 1 megohm * 1 microfarad = 1 second\\

Changed lines 24-25 from:

The BBB (Arduino) pins can be in one of two states, which are electrically very different.

to:

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

Changed lines 32-33 from:
  • Good for lighting LED's, driving other circuits - useless for reading sensors
to:
  • 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

Deleted lines 137-139:

September 27, 2008, at 03:05 AM by Paul Badger -
Changed lines 142-143 from:
  • Fight with "Serial.print" to print out decimal point and fractions
    (see Stopwatch sketch)
to:
  • Fight with "Serial.print" to print out decimal point and fraction (see Stopwatch sketch)
September 27, 2008, at 03:04 AM by Paul Badger -
Changed lines 140-141 from:
  • Average together a group of readings for more accuracy
  • Use several charging resistors on different pins to make an "auto-ranging" capacitance meter
to:
  • 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
  • Fight with "Serial.print" to print out decimal point and fractions
    (see Stopwatch sketch)
  • Swap out several charging resistors on different pins to make an "auto-ranging" capacitance meter
September 27, 2008, at 03:01 AM by Paul Badger -
Changed line 87 from:

float microFarads; // floating point variable to preserve precision, make calculations, printing easier

to:

float microFarads; // floating point variable to preserve precision, make calculations

September 27, 2008, at 03:00 AM by Paul Badger -
Changed lines 107-110 from:
    Serial.print(elapsedTime);       // print the value to serial port
    Serial.print(" mS    ");         // print units and carriage return

to:
  Serial.print(elapsedTime);       // print the value to serial port
  Serial.print(" mS    ");         // print units and carriage return

September 27, 2008, at 02:58 AM by Paul Badger -
Changed lines 105-106 from:
  microFarads = ((float)elapsedTime / resistorValue) * 1000;   // convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ),  net 10^3 (1000)
to:
 // convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ),  net 10^3 (1000)  
  microFarads = ((float)elapsedTime / resistorValue) * 1000;   
September 27, 2008, at 02:56 AM by Paul Badger -
Changed line 101 from:
  while(analogRead(analogPin) < 648){       // 647 is 63.2% of 1023. 1023 is the A/D reading that corresponds to the charging voltage 
to:
  while(analogRead(analogPin) < 648){       // 647 is 63.2% of 1023, which corresponds to full-scale voltage 
September 27, 2008, at 02:54 AM by Paul Badger -
Changed line 87 from:

float microFarads; // floating point variable to preserve precision and make calculations and printing easier

to:

float microFarads; // floating point variable to preserve precision, make calculations, printing easier

September 27, 2008, at 02:53 AM by Paul Badger -
Changed lines 48-49 from:
  • Subtract the time from the start time to find out how long the capacitor took to charge.
to:
  • After the cap is charged, subtract the current time from the start time
  • to find out how long the capacitor took to charge.
September 27, 2008, at 02:51 AM by Paul Badger -
Deleted lines 56-57:
Changed line 71 from:
 * Hardware setup:
to:
 *  Hardware setup:
September 27, 2008, at 02:50 AM by Paul Badger -
Changed lines 74-85 from:
 * Charging resistor (10k as set below) is connected at one end to chargepin, 
 * at other end to common point (blank breadboard column)
 * OK to change charging resistor value for testing small capacitors 
 * Don't forget to change the value in the #define too though!
 * Discharge resistor (220 ohm or 1k - value doesn't matter) connected at one end to discharge pin,
 * Other end to common point
 * Capacitor under test: One end(negative if polarized cap) to ground,
 *  other end to common point
 * Try lots of different values - also series and parallel arrangements. 
 * Wire from common point to analog pin
  • /
to:
 *  Test Capacitor between common point and ground (positive side of an electrolytic capacitor  to common)
 *  Test Resistor between chargePin and common point
 *  220 ohm resistor between dischargePin and common point
 *  Wire between common point and analogPin (A/D input)
 */
Changed line 81 from:
  1. define chargePin 13 // pin to charge the capacitor - this is connected to one end of the charging resistor
to:
  1. define chargePin 13 // pin to charge the capacitor - connected to one end of the charging resistor
Deleted lines 85-94:

/* Hardware setup

 *  Test Capacitor between common point and ground (positive side of an electrolytic capacitor  to common)
 *  Test Resistor between chargePin and common point
 *  220 ohm resistor between dischargePin and common point
 *  Wire between common point and analogPin (A/D input)
 */

September 27, 2008, at 02:47 AM by Paul Badger -
Changed line 78 from:
 * Dischage resistor (220 ohm or 1k - value doesn't matter) connnected at one end to discharge pin,
to:
 * Discharge resistor (220 ohm or 1k - value doesn't matter) connected at one end to discharge pin,
Changed lines 101-103 from:

Arduino Sketch

to:
September 27, 2008, at 02:45 AM by Paul Badger -
Changed line 106 from:

float microFarads; // floating point variable to preserve precison and make calculations and printing easier

to:

float microFarads; // floating point variable to preserve precision and make calculations and printing easier

Changed line 110 from:
  pinMode(chargePin, OUTPUT);               // set chargePin to output
to:
  pinMode(chargePin, OUTPUT);     // set chargePin to output
Changed line 113 from:
  Serial.begin(9600);                       // initialize serial transmission for debugging
to:
  Serial.begin(9600);             // initialize serial transmission for debugging
Changed line 117 from:
  digitalWrite(chargePin, HIGH);            // set chargePin HIGH and capacitor charging
to:
  digitalWrite(chargePin, HIGH);  // set chargePin HIGH and capacitor charging
September 27, 2008, at 02:41 AM by Paul Badger -
Changed lines 89-92 from:
  1. define resistorValue 10000.0F // change this to whatever resistor value you are using - F formatter tells compliler it's a floating point value

to:
  1. define resistorValue 10000.0F // change this to whatever resistor value you are using
                                  // F formatter tells compliler it's a floating point value

September 27, 2008, at 02:39 AM by Paul Badger -
Changed lines 80-81 from:
 * Capacitor under test - try different values - also series and parallel arrangements. One (negative if polarized) end to ground
 *     other end to common point
to:
 * Capacitor under test: One end(negative if polarized cap) to ground,
 *  other end to common point
 * Try lots of different values - also series and parallel arrangements. 
September 27, 2008, at 02:37 AM by Paul Badger -
Changed lines 74-76 from:
 * Charging resistor (10k as set below) is connected at one end to chargepin at other to common point (blank breadboard column)
 * OK to change charging resistor value for testing small capacitors - change the value in the #define too though!
 * Dischage resistor (220 ohm or 1k - value doesn't matter) connnected at one end to discharge pin, other end to common point
to:
 * Charging resistor (10k as set below) is connected at one end to chargepin, 
 * at other end to common point (blank breadboard column)
 * OK to change charging resistor value for testing small capacitors 
 * Don't forget to change the value in the #define too though!
 * Dischage resistor (220 ohm or 1k - value doesn't matter) connnected at one end to discharge pin,
 * Other end to common point
September 27, 2008, at 02:34 AM by Paul Badger -
Changed lines 4-5 from:
to:
Changed lines 40-41 from:
to:
September 27, 2008, at 02:33 AM by Paul Badger -
Changed lines 4-5 from:
to:
Changed lines 20-21 from:
to:
Changed lines 40-41 from:
to:
September 27, 2008, at 02:33 AM by Paul Badger -
Changed lines 4-5 from:
to:
Changed lines 20-21 from:
 
to:
Changed lines 40-41 from:
to:
September 26, 2008, at 11:34 PM by Paul Badger -
Changed lines 130-131 from:
    // if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad). This is because Serial.print will not print floats
to:
    // if value is smaller than one microFarad, convert to nanoFarads (10^-9 Farad). 
    // This is  a workaround because Serial.print will not print floats
September 26, 2008, at 11:32 PM by Paul Badger -
Added lines 1-160:

Capacitance Meter and RC Time Constants

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

  • T = R * C

  • T = time in seconds
  • R = resistance in ohms
  • C = capacitance in farads (1 microfarad (ufd) = .0000001 farad = 10^-6 farads )

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

Example: 1 megaohm * 1 microfarad = 1 second
Example: 10k ohms * 100 microfarad = 1 second

 

Experimental Setup

The BBB (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

Millis function - good for timing events

  • Record the start time
  • Cause the event to occur
  • Check the time and subtract the start time to find the elapsed time of the event
  • For short events put the event in a loop and do it many times

Alogrithm 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 HIGH
  • Check the voltage repeatedly in a loop until it gets to 63.2% of total voltage.
  • Subtract the 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.
  • 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

/*  RCTiming_capacitance_meter
 *   Paul Badger 2008
 *  Demonstrates use of RC time constants to measure the value of a capacitor 
 *
 * Theory   A capcitor will charge, through a resistor, in one time constant, defined as T seconds where
 *    T = R * C
 * 
 *    T = time 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.
 *
 * Hardware setup:
 * Charging resistor (10k as set below) is connected at one end to chargepin at other to common point (blank breadboard column)
 * OK to change charging resistor value for testing small capacitors - change the value in the #define too though!
 * Dischage resistor (220 ohm or 1k - value doesn't matter) connnected at one end to discharge pin, other end to common point
 * Capacitor under test - try different values - also series and parallel arrangements. One (negative if polarized) end to ground
 *     other end to common point
 * Wire from common point to analog pin
*/

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



/* Hardware setup
 *  Test Capacitor between common point and ground (positive side of an electrolytic capacitor  to common)
 *  Test Resistor between chargePin and common point
 *  220 ohm resistor between dischargePin and common point
 *  Wire between common point and analogPin (A/D input)
 */

!!!! Arduino Sketch


unsigned long startTime;
unsigned long elapsedTime;
float microFarads;                          // floating point variable to preserve precison and make calculations and printing easier
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. 1023 is the A/D reading that corresponds to the charging voltage 
  }

  elapsedTime= millis() - startTime;
  microFarads = ((float)elapsedTime / resistorValue) * 1000;   // convert milliseconds to seconds ( 10^-3 ) and Farads to microFarads ( 10^6 ),  net 10^3 (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 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
} 

Further things to try

  • Average together a group of readings for more accuracy
  • Use several charging resistors on different pins to make an "auto-ranging" capacitance meter
    • Substitute in larger resistors if the charging time is too short, smaller resistors if the charging time is too long. By using some extra Arduino pins, you could make the capacitance meter auto-ranging.

Share