Electronic – arduino – How does the Arduino CapSense library work


Can anyone give me some help in understanding how the Arduino CapSense library is able to sense proximity? From reading the source code, it seems that it toggles a sending pin and then waits for the receiving pin to change to the same state. The time taken (if I understand this correctly) depends on the proximty of a finger, for example, to the sensor electrode. However, there are several things I don't understand, such as:

  • Why does the proximity of e.g. a human hand change the time taken for the receive pin to change state?

  • Why is a resistor needed? I can understand a small resistor, but the example seems to recommend a very large (10 Mohm or greater) resistor for better sensitivity.

  • The site recommends capacitors for better performance. How do these enhance performance?

  • Does the frequency with which the send pin toggles play into the detectors performance at all?

  • And finally: Why does it use a digital pin as the receiver? Why not just pipe the output of the whole thing into an analog pin and measure the voltage via ADC?

Best Answer

It's a simple RC circuit (Resistor-Capacitor). By measuring the time it takes to go form one state to the other, you get an approximation of distance (we know the general capacitance of the human body, the high value resistor, and the general capacitance of the sensor material) . The high value resistor is to provide a significantly low drain time.

The reason it uses a Digital pin is because it does not matter what the intermediate voltage is, just how long it takes to change from State 1 to State 2. And Digital Pins are more plentiful, and have fixed points where the state changes, either directly or through Schmitt triggers. The ADC is also slow or blocking, taking setup and a few cycles or interrupts, while a digital pin change is immediate, and only takes 3 or 4 cycles to check at most.

From the Capsense Library page:

The capacitiveSensor method toggles a microcontroller send pin to a new state and then waits for the receive pin to change to the same state as the send pin. A variable is incremented inside a while loop to time the receive pin's state change. The method then reports the variable's value, which is in arbitrary units.

When the send pin changes state, it will eventually change the state of the receive pin. The delay between the send pin changing and the receive pin changing is determined by an RC time constant, defined by R * C, where R is the value of the resistor and C is the capacitance at the receive pin, plus any other capacitance (e.g. human body interaction) present at the sensor (receive) pin. Adding small capacitor (20 - 400 pF) in parallel with the body capacitance, is highly desirable too, as it stabilizes the sensed readings.