0

I want to use CapacitiveSensor library in order to make my own aluminum keys piano. I have seen many videos online showing projects like the one I'm trying to do, but for some reason mine doesn't work.

Here's my cuircit: enter image description here

Here's my code:

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_7_6 = CapacitiveSensor(7,6);  // 10M resistor between pins 7 & 6, pin 6 is sensor pin

void setup()                    
{
   //cs_7_6.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
}

void loop()                    
{
    long start = millis();
    long total1 =  cs_7_6.capacitiveSensor(5);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.print(total1);                  // print sensor output 1
    Serial.println("\t");

    delay(10);                             // arbitrary delay to limit data to serial port 
}

This happens whenever I touch the aluminum foil:

enter image description here

First nothing happens (for ~1 sec), then it prints -2 as the capacity value, and when I release my finger from the foil it prints a very high value.

Anyone knows the reason for it? I'm searching for an answer for a long time and still can't find a solution...

  • The method that library uses is incredibly crude and shouldn't be considered reliable. It's not proper capacitative touch sensing. To do it properly you need four things that you don't have with that library: 1. a constant current source. 2. an analog input pin. 3. MOSFET switching for charge/discharge circuits. 4. a ground plane integrated with the touch sensor. While under some circumstances it may appear to give capacitative touch sensor-like results, I certainly wouldn't use it for anything more than making an LED light up. Certainly never anything that controlled any motors or similar. – Majenko Jun 29 '15 at 11:54
  • By the way, "-2" is "timeout" (something you'd never get with a proper CTMU). – Majenko Jun 29 '15 at 12:14
  • Oh, and have you read all the things on the Arduino Playground page for the Capacitive Sensing Library? – Majenko Jun 29 '15 at 12:15
1

The problem is that you're touching the foil.

The "capacitive sensing" used in that library is crude at best.

If you read the Arduino Playground page for the Capacitive Sensing Library it discusses a lot of the problems with the library, including grounding. Make sure that the Arduino is properly grounded, or provide your own ground plane under the foil sensors.

Also they talk about the value of resistor and the sensitivity. With a 10MΩ resistor you get sensing at (supposedly) 4-6cm from the foil. That's the value you're seeing as you remove your finger. While you're touching the foil it's just having a complete brain haemorrhage and can't work out what is going on (completely unable to charge the pin since you are leeching off all the current). For "absolute touch" it recommends no more than 1MΩ.

Of course, as I mentioned, that method is rather crude. To do it properly you need to:

  1. Discharge an analog input pin through a MOSFET to ground (low impedance).
  2. Apply a constant current (typically 50µA is used but varies depending on ADC characteristics) to the analog input for a fixed amount of time.
  3. Remove the current and measure the voltage on the ADC input.
  4. Repeat.

More modern MCUs have this kind of functionality built in, in the form of a "CTMU" - Charge Time Measurement Unit. If you want to have anything more than "there's someone vaguely in the vicinity of the sensor" then I would suggest switching to a more modern MCU and doing it properly, or employing a specific CTMU chip as an external device.

Also, the sensor should be comprised of three parts - a ground plane, a sensor plane (connected to the ADC) and a dielectric layer between them. The sensor should also be insulated from direct touch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.