Re: CapSense Library

Thanks, Paul. I tried the other code you posted earlier for doing the same thing and it worked like a charm. This is cool stuff!

OK it's packaged up in a library, thanks to some help from David Mellis. It really wasn't that big a deal and I'm sure I would have finished up around April of 09 on my own. jk

The library is accessible both at its old location http://www.arduino.cc/playground/Main/CapSense and on the reference/library page.

I think it is possible to build a scroll wheel with this function. Also I sensed through a ream of paper when testing, so it should be possible to sense through more than an inch or so of plastic, ceramic or other insulating material. I especially got a nice (somewhat linear) response to squeezing a sensor covered in fabric and think it would work great inside a stuffed animal.

A covered (insulated) sensor seems to work best and yields a surprisingly responsive pressure sensor. Much better than a force sensing resistor in many applications, I would think.

Please post bugs, suggestions, or any amazing feats on this thread.

Enjoy,

PB

I tried this yesterday but the results were not too promising. My dev environment is a Macbook not connected to ground. The sensor is a 3"x3" piece of aluminum foil that I stuck under the cardboard of a hardcover book, R=10MOhm. The sensing range for my hand was around 0.5" and for a single finger I could just make out a tiny increase of values upon touching the insulator. Touching the foil itself yielded a nice huge delta. Replacing the 10meg with 20meg resistors hardly changed the results. Moving my hand closer to the laptop however, DID create a large change in values.

The docs say to use a grounded connector which means that you could only build tethered projects. Kind of limiting for an embedded device such as the arduino- it would be nice if both battery operation AND power supplies would yield similar results.

Freescale has some nice application notes on their touch sensing ICs. To summarize: for single touch sensors they recommend using a spiral pattern where Gnd and Sensor wire traces run parallel to each other. Good insulator material is neoprene rubber, and to avoid stray capacitance there should be a Gnd plane behind the sensor. However, for reliable results they suggest a distance of only a few millimeters which makes me wonder how your docs can claim a sensing distance of > 10".

Most commercial capsensing ICs (Omron, Freescale, AD) seem to employ an oscillator and then measure phase/frequency shifts induced by touching the sensor. I wonder if this is any more reliable than measuring the charging rate as done in the CapSense library. This library depends on an arbitrary switching point of the atmega's CMOS inputs. Wouldn't the analog pin readouts be more robust ?

I tried this yesterday but the results were not too promising. My dev environment is a Macbook not connected to ground. The sensor is a 3"x3" piece of aluminum foil that I stuck under the cardboard of a hardcover book, R=10MOhm. The sensing range for my hand was around 0.5" and for a single finger

For 10" I was using a whole hand and somewhat larger sensor - it just depends on what you are trying to accomplish.

I could just make out a tiny increase of values upon touching the insulator. Touching the foil itself yielded a nice huge delta. Replacing the 10meg with 20meg resistors hardly changed the results. Moving my hand closer to the laptop however, DID create a large change in values.

This probably had to do with the wire connecting the sensor, which is live (sensing - unless you use shielded cable). The Theremin sensor though reported a similar effect.

The docs say to use a grounded connector which means that you could only build tethered projects. Kind of limiting for an embedded device such as the arduino- it would be nice if both battery operation AND power supplies would yield similar results.

The Qprox datasheets suggest taping a ground to the wall - The sensor doesn't need to be connected to a laptop - a power adapter works equally well (no need to be grounded). Unfortunately - the calibration is going to change a bit. As far as I can see there's no way to avoid this without adding an earth ground.

The approach I am using with subtracting the baseline should add some kind of rough calibration, but there's a lot I don't know about this yet. If you've used the Qprox linear sensors, you know how jittery and glitchy they are too.

The frequency approach isn't really that different - think of the whole thing as a big oscillator and you'll see that the frequency is changing. The period of the oscillation is being measured.

See the theremin approach in the playground (and posted on this forum too).

PaulB

I have begun testing out the CapSense library this afternoon, and noticed that if I left the sensor on for a few minutes there would be a slow build up (higher numbers coming out of the capsense function) over time. I am using a Macbook connected to ground and the arduino is powered by USB. I haven't tested it with foil yet, just a 2 foot wire with a 1/2" by 2" piece of aluminum at one end. Also their seems to be a significant charge after a while because its giving me small shocks. Is there a way to clear this build up?

I have begun testing out the CapSense library this afternoon, and noticed that if I left the sensor on for a few minutes there would be a slow build up (higher numbers coming out of the capsense function) over time.

I think what is going on is that the output of capsense is compared to the lowest reading. Over time noise (and maybe other factors) could produce a lower reading (especially true if your sensor wire is loose). Then the function is comparing with a lower number so will yield a higher output. There is an autocalibrate routine that runs every so often. You can change that in the header file for more frequent calibration. This should counter the drift (at the cost of a glitch where the sensor will fail to read a sensed hand until the hand is removed from the sensor). I'm going to implement a reset function to make this easier.

As to the charge, are you sure that it's just not plain static charge from dry air in the winter? The sensor probably is giving off lots of RF at some frequency but I don't really see that building up a charge. Then again I don't really know that much about RF.

PaulB

Thanks. Yeah I am not sure about the charge at all. It very well could be the dry winter weather. I will tinker with the autocalibrate.

For testing purposes use the RawSense function instead and see if it still keeps increasing.

I was going to mention another tip. If you reset your Arduino (or clone :)) the function autocalibrates on the way in, so you could compare the drift then reset.

Or as Cross mentioned, just use capSenseRaw - then you'll really know what's going on, and you can write your own subtraction function - maybe better than mine too, and we can improve the library!

PaulB

Before it was in a library I have used some of your earlier code and used it in an application that measures fuel tank level by measuring the capacitance of the probe. The probe is basically an aluminum rod inside of an aluminum tube. Anyways while bench testing I discovered by accident a way to use your code with one pin:

I keep a 10meg 5V pullup on the receiving pin. Turn receive pin to output and drive low (overcomes pullup). Turn receive pin to input and count until pin reads high.

I thought this might be useful to some. Btw thanks for this code as it is extremely useful for my purposes. Joel

One more thing to consider is pulling the input pin low for a longer time in between measurements to make sure that all charge on the capacitor has dissipated.

jrhall,

If it works with one pin that way then go for it. The new library drives the send pin both low and high (charging the cap each way) so don't know if this would work. Maybe because it's only looking for a CHANGE in values it doesn't really matter.

It might actually end up being more consistent if it only charges the capacitance one way. Someone could do some testing if interested. I'm on 16 other missions right now.

Electricity, fuel - yikes! Better have a good idea what you're doing. I've always wondered about electric fuel pumps and fuel gauges in cars and how they insure they're safe. It can't be a huge issue or there would be some literature of urban myth about fuel pumps blowing up cars.

Paul

The design for the fuel probe and the the method of reading it has been in use for decades. The modernizing part I am doing with the Arduino is getting rid of the analog/555 approach for determining capacitance in the sensor box.

I'm interested in using Arduino along with a relay to sense water level in a small tank. I have a tank under my window air conditioner unit to catch the water drips (for use in a flower bed and to prevent the water from soaking my porch). I want to turn on a pump when the tank is nearly full then turn the pump back off when it's nearly empty (don't want the water to go too low allowing the pump to suck air). I figure two capacitive sensors would be necessary. One for the high water level and one for the lower level. I know it's a bit overkill to use the arduino for this but I like the idea of building it myself instead of purchasing an off the shelf pump and/or float switch.

I'll bet the sensing part of this is fairly easy. I'd predict you'll get a really clear reading from the water in the tank and can just threshold against a hard-wired value. Maybe post something here with your results if you get a chance.

PaulB

I'll be sure to post about how it works out when I get beyond the planning stage and actually start hooking up wires.

You don't really need the capacitive sensing here. Submerge two wire pairs for the high and low points. One wire is at +5 and the other is your input. Connect the inputs to whichever pins correspond to Interrupt 0 and Interrupt 1. Then send your arduino to sleep and make it wake up on interrupt 0/1. Easy, reliable, very low power consumption.

I like the probe idea too. The only disadvantage is that one needs to put the probes in which could be a bit of fuss on a (mostly) sealed system. also some fluids, chemicals etc might not be so nice to the probes or behave so well electrically. I think that’s why cap sensing gets used in industriy sometimes.

I have a question about multiple inputs.

The CapSense Library page says that to use multiple inputs and have it autocalibrate just copy the "scheme" in CapacitiveSense.c with a different set of variables for each different set of pins you wish to sample.

Could you be more specific?

Thanks

The problem with making multiple calls from different input pins is that the autocalibrate scheme has a built-in variable. A call to capSense() with two different inputs is still going to reference the same (on the second call - incorrect) variable I believe.

This has to do with my limited ability with C++ so I need a bit of pointer on the right way to implement this - and then I need to put up a more elaborate example. The library is actually written in C instead of C++ and implementing it in C++ would solve most of this problem I think.

In the meantime, capSenseRaw() doesn't have any problems, so just use that one with as many inputs as you want. You can look in the library to see how I implemented the subtraction scheme, and make your own autocalibrate for each set of input pins.

Hope that helps,

Maybe I'll get an offer of help from a C++ coder on this, and get it fixed. Barring that I'll try to carve out some time to get to it this week.

paulb