Capacitance measurement with the Arduino Uno

You can buy meters, like this one for instance:

Peak_LCR40

that measure capacitance, but where’s the fun in that?!

If you’ve got an Arduino, why not build one instead?

Update: Thank you for all the interest in this article! I have managed to increase the range, so it can measure capacitances of less than 1pF to over 1000uF , still with no external components! I’ve called this the Capacitance Meter Mk II.

There are lots of examples of how to do this on the internet, but I’m going to suggest an incredibly simple way to it. Let’s start with the theory.

Consider the following circuit:

SimpleCapacitanceTester

CT is the capacitor under test. We start with both capacitors discharged and A2 at 0 volts. When we raise A2 to 5 volts a current will flow through both capacitors. The voltage on A0 will settle to within 1% of it’s final value within 30ns. The value that A0 will settle to is proportional to the ratio of CT divided by the total capacitance, C1 + CT. The formulas that we are going to need are:

CapTestFormulas3

We will use the ADC to measure VA0. VA2 is actually 5 volts,  so VA0 will vary from 0 to 5 volts, but we can use the ADC value instead, to make the calculations easier. Obviously we should then use the maximum ADC value (1023) for VA2. The ADC readings we can expect will range from about 33 for CT = 1pF to about 993 for CT = 1nF (1000pF).

So now we are ready to build the circuit and write the code. Right? Well, not quite. In the circuit above we have specified C1 as 30pF. But the Arduino will have some stray capacitance on the board and in the micro-controller. About 30pF of stray capacitance. We’re going to use this to make our circuit simpler – we are going to remove C1. But we need to work out how much stray capacitance there is, so we can plug it into the formulas above. But first, we’ll build the circuit and write the code.

The circuit is surprisingly easy. In fact there isn’t a circuit now. We’re going to use the Arduino as follows:

CapTestBoard1

The capacitor is the capacitor under test between A0 and A2. The capacitors on the right are the capacitors that I am going to test a bit later on.

The code looks like this:

const int OUT_PIN = A2;
const int IN_PIN = A0;

//Capacitance between IN_PIN and Ground
//Stray capacitance is always present. Extra capacitance can be added to
//allow higher capacitance to be measured.
const float IN_STRAY_CAP_TO_GND = 24.48; //initially this was 30.00
const float IN_EXTRA_CAP_TO_GND = 0.0;
const float IN_CAP_TO_GND  = IN_STRAY_CAP_TO_GND + IN_EXTRA_CAP_TO_GND;
const int MAX_ADC_VALUE = 1023;

void setup()
{
  pinMode(OUT_PIN, OUTPUT);
  //digitalWrite(OUT_PIN, LOW);  //This is the default state for outputs
  pinMode(IN_PIN, OUTPUT);
  //digitalWrite(IN_PIN, LOW);

  Serial.begin(115200);
}

void loop()
{
  //Capacitor under test between OUT_PIN and IN_PIN
  //Rising high edge on OUT_PIN
  pinMode(IN_PIN, INPUT);
  digitalWrite(OUT_PIN, HIGH);
  int val = analogRead(IN_PIN);

  //Clear everything for next measurement
  digitalWrite(OUT_PIN, LOW);
  pinMode(IN_PIN, OUTPUT);

  //Calculate and print result

  float capacitance = (float)val * IN_CAP_TO_GND / (float)(MAX_ADC_VALUE - val);

  Serial.print(F("Capacitance Value = "));
  Serial.print(capacitance, 3);
  Serial.print(F(" pF ("));
  Serial.print(val);
  Serial.println(F(") "));

  while (millis() % 500 != 0)
    ;    
}

The code above loops round every half second, applying a 5V pulse to the capacitor, and measuring the voltage the other side. It then prints out the calculated capacitance (and raw ADC value).

If we try this out it won’t be very accurate. That is because the stray capacitance isn’t exactly 30pF. So we need to calibrate it. I did this with a 100pF capacitor. My multimeter reckons that it actually has a value of 102pF. The reading I get on my Arduino board is 125pF (raw ADC value 825). So if we put VA0 = 825, VA2 = 1023 and CT = 102 into the second equation, this tells us that C1 is 24.48pF. So I changed IN_STRAY_CAP_TO_GND to 24.48 and uploaded this to the Arduino. This time the value displayed on the serial monitor is 102pF (most of the time!).

CapTestSerialMon1

I tried it out with the following capacitors:

MiscCap1

This was the results of my tests:

Capacitor Reading
- 0.072 pF (3)
15J 15.461 pF (396)
22J 23.403 pF (500)
47J 48.532 pF (680)
101J 102.000 pF (825)
471K 448.030 pF (970)
102K 1064.348 pF (1000)
392K 3553.097 pF (1016)
472K 4984.128 pF (1018)

I was curious to know how accurate this might be, so I worked it out. You can see the results of my analysis here. My conclusion was that you could expect a resolution of about 1% between 3.5pF and 225pF, and about 5% between 0.5pF and 1300pF. I haven’t worked the accuracy and linearity yet – I need to get hold of some very accurate reference capacitors (or a very accurate meter) for that.

You can build your own capacitor, and do experiments, which I have demonstrated here.

If you want to build on this or make a complete LCR meter, I have some ideas here.

31 thoughts on “Capacitance measurement with the Arduino Uno

  1. Pingback: hackaday.com/2014/01/22/capacitance-measurement-with-the-arduino-uno

  2. Pingback: 5jcodelabs.com/howtomrj/capacitance-measurement-with-the-arduino-uno

  3. nice post
    In the photo I can see you’re using USB power supply. With no doubt that accounts for the “error” in your measurements, since mostly never you’ll get the precise 5V reference on your USB port.
    To compensate this you could use some software sorcery: https://code.google.com/p/tinkerit/wiki/SecretVoltmeter, or better the hardware hack – add MCP1541 to vRef, or some even more accurate (thus 10x more expensive) device, like LT1021.
    I did personally added LT1021 (free samples available from linear.com) – now I can calibrate expensive voltmeters with my Arduino.

    • I don’t see anything in the code that assumes that MAX_ADC_VALUE corresponds to 5V. This code should work equally well over a range of +5V supply voltages, so long as the +5V supply is stable over the measurement period.

    • Well spotted! I could say that was a “deliberate mistake” – but it wasn’t, it was just a mistake. Thanks again for spotting that.

    • You are absolutely correct. The while(1) loop is completely unnecessary since loop() will be called continuously – so I have removed it. Thanks.

  4. Nice, simple and by that merit elegant, whats not to like – well done.

    I am right in thinking there must be a limit to the largest capacitor you can measure. The maximum current these outputs can source is 20mA?

    • It shouldn’t matter how much current can be sourced (or the rise time) – the result should be the same. The only problem may be damaging the microcontroller, but it should be fine up to about 100uF. To be on the safe side you could include series resistors, but this might mean longer delays are required to reach a steady state.

      • A current-limiting resistor is a good idea. A cap looks like a dead short to ground when you first apply the 5V to it. Long term, this could damage an output pin. To limit 5V to 20mA requires a 250 Ohm resistor – make it 270 to be safe. An RC circuit will reach 90% of its final value in 2.2RC. So a 100pF capacitor in series with a 270 Ohm resistor will reach 4.5V in 59 nanoseconds. This can be a source of error as the cap being tested gets larger. One way to test these is to put them into an oscillator and measure the resulting frequency.

        • this was my thinking. say someone building a supercap project saw this and, without thought, just hooked it up straight as show – the cap will look like a dead S/C and would damage the arduino. Maybe and extension of this project could be employing some circuitry to accommodate large caps accurately? Or you done on this one Jon?

          • As long as the capacitor is discharged it should be ok… because we only apply a very short pulse (~100us – the time it takes to do the ADC conversion using analogRead()), and then discharge (which presumably would take another 100us). After that we wait ~499,800us before we have another go.
            The only problem would be if someone hooked up a charged capacitor, but that could destroy the Arduino even if the I/O pins were set as inputs. I guess you could add an external resistor, but how big should that be? Someone could still hook up a capacitor charged up to hundreds of volts…

  5. How to measure the power factor along with the capacitance. I have a very large capacitor. I don’t know its value. So I wanted to know the powerfactor so I can purchase in large.

  6. Pingback: Arduino Uno rigged to measure capacitance

 | Bits & Pieces from the Embedded Design World

  7. Great method! Seems to work very well. I made a capacitive water level sensor for my sump pump long ago but this is the first cap measuring application I have found that is stable enough to use. The water level sensor capacitance is between 40 and 450 pf, I added 100 pf to ground (IN_EXTRA_CAP_TO_GND) to make this app more accurate on the high end. added delay(1); right after the digitalWrite in the main loop to allow the measurement to settle, with these cap values it should settle in way less than 1 millisecond. Now if I can port it to a Tiny85 for low power… should work A2 is not used as an analog pin here, can use a digital pin, right?

    • I have been working on an updated version of the code that will cover the range from 1nF up to over 1000uF using the internal pullup on one of the output pins. So altogether it will be able to measure capacitors from less than 1pF to over 1000uF (nine orders of magnitude). I’m quite pleased with the result. :) I’ll post that as another article on here today.

  8. Nice job. I must be doing something entirely wrong though. When I upload the code and try to take a reading, I always get 1023 from A0 when a capacitor is hooked up and I get 0 to 3 when nothing is hooked up. There is no in between. I’m not sure what’s gone wrong.

  9. I am trying this on MSP430g2252.
    Draining a capacitor isn’t working:
    I am putting a 100uF cap (charged to 3V) across 2 GPIO pins that are set to output LOW.
    With a voltmeter, it is taking 30minutes to drain down to 0 V.

    Any suggestions? Maybe msp430 can’t drain thru the IO pins?
    I’m doing this for the LOW pins:

    P1DIR |= pin;
    P1SEL &= ~pin;
    P1SEL2 &= ~pin;
    P1OUT &= ~pin;
    P1REN &= ~pin;

    Any suggestions?

  10. I figured it out. My analogRead was doing this:
    ADC10AE0 = (1 << pin);
    I put this in because I saw Energia used that. When I took this out, my capacitor drains quite quickly!

    This is a great project! Isn't electronics fun?!

  11. Pingback: The Skinny: Balloons, Bikes, and Breathers | Skinny Research and Development

  12. Pingback: Cum masori capacitatea unui condensator cu o placa Arduino | Robofun Blog

Leave a Reply

Your email address will not be published. Required fields are marked *


+ nine = 18

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>