Unexpected lingering capacitance

Discussion in 'The Projects Forum' started by ebeowulf17, Mar 4, 2015.

  1. ebeowulf17

    Thread Starter Active Member

    Aug 12, 2014
    I recently finished a project (included in the Completed Projects Collection) which uses the Arduino Capacitive Sensor Library (linked here) to drive the three colors of an RGB LED based on proximity to one or more of three capacitive sensors. I had some troubles early on and managed to hack my way through them with a lot of guesses, but I'd really love to better understand what's going on and was hoping someone here can enlighten me.

    Basically the capacitive sensor in this case is the bottom of a soda can wired to an Arduino input, with a 5Mohm resistor tying that line to an Arduino output. The output changes states and then waits to see how long it takes for the input to change states. That amount of time varies based on the RC constant of the resistor and the sensor's capacitance, which changes in response to certain nearby objects (mostly my hands.) The fact that a hand several inches away from a soda can alters its electrical properties sounds like black magic to me, but otherwise I mostly feel comfortable with my conceptual understanding of the basic operation.

    What's weird is that when I first started testing this thing, it had a tendency for one or occasionally two colors to sort of runaway on me. They would act normal on a fresh boot, but gradually get more and more sensitive until eventually that color was lit at full brightness no matter what. Sometimes a quick power cycle would deliver a fresh start, but at other times the "runaway" channel would just pick right back up where it left off. Oddly, it wasn't always the same channel.

    Eventually I had the thought that whatever charge is building up to change from low to high states might not have time to dissipate all the way, since presumably draining that charge would also be subject to the same RC time constant. First I tried just adding significant (relative to the test cycle time) delays of 20-50 ms between samples. At first I thought I saw improvement, but then it got pretty bad again. I didn't test rigorously enough to discern whether it made a small difference or none at all.

    Finally I went with a more drastic option: After each cycle of sampling the three sensors, all three of the input channels (the ones directly tied to the soda can "sensors") are switched to output channels and then forced "low" (tied to ground.) I figured this should guarantee that they have no residual charge lingering before the next test cycle. Immediately before the next cycle of tests, they're all switched back to inputs so they'll behave as intended for capacitive sensing.

    This last arrangement seems to work pretty well. I still sometimes have a channel start getting noisy if I leave the unit on long enough, but it's very rare now and feels much more like something that could be explained by actual environmental changes altering the capacitance (changes in temperature, humidity, etc.)

    So I have the problem at least somewhat controlled now, but I don't feel confident in my understanding of the solution. It feels like my idea of grounding them out and discharging any remaining charge could be right, but it also seems like just putting the delay in between test cycles also should've made it reliable, which it didn't. So I don't trust my instincts on this.

    Can anyone explain what's going on? Does my solution make sense? Does my description of the mechanisms involved fit? Also, do you think there's a significant risk that whatever charge builds up in the test cycles could actually do harm by discharging straight to ground (through the Arduino) without a current limiting resistor? Could the soda can capacitors hold enough charge to damage anything?

    Sorry that (as usual for me) this is so long-winded. Thanks in advance to anyone who has the patience to get this far!
  2. AnalogKid

    Distinguished Member

    Aug 1, 2013
    The problem makes perfect sense. Air capacitors have extremely low loss, and what you have is an integrator collecting bits of charge from ions in the air and stray electromagnetic fields. And like with any precision integrator, you have to keep the capacitor in a known state before an integration cycle. This can be done either by placing a short across the cap (usually with a J-fet or MOSFET) or by controlling the potential at each end as you are.

    For a later-generation uC like an arduino, it probably has ESD protection built into its inputs. If so, it probably can handle whatever your Coke can can deliver. Check the datasheet.