One Pin Capacitive Sensor Library

I have created a one pin capacitive sensor library that supports multi-touch for the Leonardo (and UNO). You can download the library at: GitHub - MrYsLab/OnePinCapSense: A one pin capacitive sensor library for Ardino that supports multi-touch

You might consider changing the name. Sooner or later, you'll hear from Cypress Semicondutor who registered the trademark "CapSense" for their own capacitive sensing hardware+firmware. I'm suspect Paul Badger's library pre-dates their trademark, but the reality is they're a big company with a well funded legal department, and Paul B (not me) wrote that library a long time ago and hasn't maintained it since.

If you do change the name, it might be worthwhile to begin with "CapacitiveSensor", perhaps "CapacitiveSensorOnePin"? That way, when anyone makes an alphabetically ordered list of Arduino libraries, yours will appear right next to the old (but renamed) CapacitiveSensor library.

Also, in the code, you might consider disabling interrupts. If an interrupt occurs anywhere between the instant you make the pin an input and the moment it reads high, your count will be less than it should be, because the CPU spent time updating the millis() count, or transmitting or received serial data, or updated servo motor pulses, or any number of other activities.

Of course, keeping interrupts disabled for a long time might cause those other activities to fail.

Another approach might involve using the 16 bit timer with input capture mode. You'd configure it up to capture the rising edge while the pin is still driven low. That way, you'd only need to disable interrupts for a very brief time, only while you change the pin to input mode and read the value of the timer (or set it to zero). Then you could allow interrupts while you wait for the capture event. The capture could even happen while an interrupt has the CPU. The timer's rapidly increasing value gets captured to another register, which you can read at your leisure and use to report a highly accurate measure of the elapsed low->high time.

Then again, the input capture feature only exists on a single pin, so it's probably not so useful for more than just a single touch electrode. :frowning:

Thanks for your comments. I am not sure what you mean by "Then again, the input capture feature only exists on a single pin, so it's probably not so useful for more than just a single touch electrode. :-(". You can create as many sensors as you have input pins available, and if you touch more than one sensor, you will get readings for all sensors touched.

I know I'm posting to an old thread, but the final comment above needs correction/clarification.

The "input capture" Paul describes is a feature of the microcontroller hardware associated with a hardware timer, and thus it exists for only the one pin/timer as he describes. It has nothing to do with the method you are discussing, but can't be done on other pins.

You method seems so obvious. I was thinking of this and searched to see if it had been done and found your post. I haven't tried it, but why not one pin???

Suggestion: place a small resistor in series with the Arduino pin input (about 100 ohms). It will have trivial effect on the timing, but in case of a charged-up human touch it may make your Arduino have a longer life... An insulated touch-sensor will also help with the discharge issue.