# How can I measure capacitance with a microcontroller?

I have a capacitor ranging from 10pF to 1000pF that should be used as a water level sensor. I have tried using the FDC1004 chip, but it seems like this is only ranging from 0 to 15pF. Does anyone here have a solution or ideas to measure this capacitance?

• In addition, some micros have a CTMU or charge-time-measurement-unit (or variation thereof.) Typically this is used for tasks like capacitively sensing a "button" press, but nothing is preventing this from being a fluid instead of a finger. Commented Apr 14, 2023 at 16:25

One simple idea would be to use this capacitor in an RC oscillator built around a TLC555 and use the micro-controller to measure frequency, which is relatively easy.

Frequency is   F = 1.44/(RA+2RB)/C Hz   and things get markedly non-linear when that's over 700kHz. So perhaps you might try RB=100kΩ, RA=10kΩ.

Try using the FDC2x1x series chips. I am using that quite successfully to detect fluid levels down to 10uL. Just a heads up, designing an algorithm to actually determine when fluid is detected is the main challenge. As your sensor approaches fluid, the capacitance will increase gradually. Once your sensor touches fluid, the capacitance will increase quickly.

A microcontroller pin can be configured to produce a square wave output, that feeds one input of a single XOR gate. The same signal feeds an RC low pass filter of R1 (10 kohm) and the unknown capacitor. The delayed signal enters the second input of the XOR gate.

The output of the gate is high during times, where the two inputs have different level.

The larger the unknown capacitor value, the larger is the duty cycle of the XOR output. The useful duty cycle range is about 3..30 %.

The low pass filter R3 an C3 integrates this to a DC value, that can be read by the microcontroller.

The square wave frequency must match the capacitor range. If the selected frequency is too high, the delayed signal amplitude is too low to be seen by the XOR gate input.

simulate this circuit – Schematic created using CircuitLab

The capacitor C1 separates DC components from the sensor, R2 in combination with the integrated clamping diodes of the gate provide a rudimentary ESD protection and R4 eliminates static charges collected by the sensor area.

The relation between the measured voltage and the sensor capacitance is not perfect linear, but probably sufficient for a water level sensor.