Arduino Capacitance Meter: Wiring, Code and Simulator

To build this Arduino Capacitance Meter project you need an Arduino Uno and a 10 kΩ resistor, a 220 Ω resistor, a 10 µF electrolytic capacitor and an oscilloscope. Pin 8 charges an unknown capacitor through 10 kΩ; the sketch times how long A0 takes to reach 63.2 % — one time constant — and prints C = τ / R. Make the 9 connections in the wiring table, upload the Arduino C++ code, and watch the serial monitor print about 10 µF, then change the capacitor to 47 µ or 1 µ and see the measurement follow. The scope shows each charge curve.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
110 kΩ resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
110 µF electrolytic capacitorA large, polarised capacitor for smoothing supplies: the stripe marks − . Reverse it, or exceed its voltage rating, and it fails.
1OscilloscopeTwo channels against earth (GND). Turn TIME/DIV and VOLTS/DIV to fit the wave, set the trigger level to steady it, and read Vpp, RMS and frequency. X-Y mode draws Lissajous figures. Choose a digital (DSO) or phosphor (CRO) screen.

Wiring

#FromTo
1Arduino Uno pin 810 kΩ resistor leg 1
210 kΩ resistor leg 210 µF electrolytic capacitor +
3Arduino Uno pin 7220 Ω resistor leg 1
4220 Ω resistor leg 210 µF electrolytic capacitor +
5Arduino Uno GND10 µF electrolytic capacitor -
6Arduino Uno A010 µF electrolytic capacitor +
7Arduino Uno pin 8Oscilloscope CH1
810 µF electrolytic capacitor +Oscilloscope CH2
9Arduino Uno GNDOscilloscope GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a 10 kΩ resistor, a 220 Ω resistor, a 10 µF electrolytic capacitor and an oscilloscope from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 8 to 10 kΩ resistor leg 1.
  4. Wire 2. Connect 10 kΩ resistor leg 2 to 10 µF electrolytic capacitor +.
  5. Wire 3. Connect Arduino Uno pin 7 to 220 Ω resistor leg 1.
  6. Wire 4. Connect 220 Ω resistor leg 2 to 10 µF electrolytic capacitor +.
  7. Wire 5. Connect Arduino Uno GND to 10 µF electrolytic capacitor -.
  8. Wire 6. Connect Arduino Uno A0 to 10 µF electrolytic capacitor +.
  9. Wire 7. Connect Arduino Uno pin 8 to Oscilloscope CH1.
  10. Wire 8. Connect 10 µF electrolytic capacitor + to Oscilloscope CH2.
  11. Wire 9. Connect Arduino Uno GND to Oscilloscope GND.
  12. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  13. Run it. Press Run. Watch the serial monitor print about 10 µF, then change the capacitor to 47 µ or 1 µ and see the measurement follow. The scope shows each charge curve.

Arduino C++ code

// Capacitance meter: charge a capacitor through a known resistor and time it.
//   pin 8 -- 10 kOhm --+-- A0
//                      |
//                      C  (the unknown)
//                      |
//                     GND
// After one time constant (tau = R x C) the capacitor reaches 63.2% of 5 V,
// so C = t / R. Pin 7 discharges it through 220 Ohm between measurements.

const float R = 10000.0;

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
}

void loop() {
  // Discharge: pin 7 pulls the capacitor down through 220 ohm.
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  while (analogRead(A0) > 0) {}
  pinMode(7, INPUT);                  // let go of it

  unsigned long start = micros();
  digitalWrite(8, HIGH);              // start charging
  while (analogRead(A0) < 647) {}     // 63.2% of 1023
  unsigned long t = micros() - start;
  digitalWrite(8, LOW);

  float microfarads = (float)t / R;   // t in us / R in ohm = C in uF
  Serial.print("tau = ");
  Serial.print(t);
  Serial.print(" us  ->  C = ");
  Serial.print(microfarads, 2);
  Serial.println(" uF");
  delay(500);
}

What to try

Watch the serial monitor print about 10 µF, then change the capacitor to 47 µ or 1 µ and see the measurement follow. The scope shows each charge curve.

Frequently asked questions

How do I wire the Capacitance Meter project on an Arduino Uno?

Make 9 connections: Arduino Uno pin 8 to 10 kΩ resistor leg 1; 10 kΩ resistor leg 2 to 10 µF electrolytic capacitor +; Arduino Uno pin 7 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to 10 µF electrolytic capacitor +; Arduino Uno GND to 10 µF electrolytic capacitor -; Arduino Uno A0 to 10 µF electrolytic capacitor +; Arduino Uno pin 8 to Oscilloscope CH1; 10 µF electrolytic capacitor + to Oscilloscope CH2; Arduino Uno GND to Oscilloscope GND.

What parts do I need for this Arduino project?

An Arduino Uno and a 10 kΩ resistor, a 220 Ω resistor, a 10 µF electrolytic capacitor and an oscilloscope.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

Pin 8 charges an unknown capacitor through 10 kΩ; the sketch times how long A0 takes to reach 63.2 % — one time constant — and prints C = τ / R.

More Arduino Uno projects