The CapTouch library for the Arduino allows you to use large resistors (1M-10M) to create simple capacitance sensors that can be used like buttons or to create more complex sensors like wheels and stroke sensors. Official documentation of the library can be found here. While the library works quite well, capacitance sensing is dependent on a number of factors that can make it unpredictable, for example, change in power supply from USB to battery, weather conditions and different people. For more stable solutions I would recommend the MPR121 which does some regular calibrations and has additional circuits to ensure consistency. That being said, if you want maximum proximity this approach works best, as you can easily add 10M+ resistors to increase sensitivity.
The following influences the distance at which a hand can be detected:
- Surface area of the touch pad
- Size of the resistor (1M – 10M works best)
- In the code below, the number in lines 27-29 will influence the sensitivity of the reading
When you run the code and open the serial monitor you will see three large numbers that will increase when you touch the pad. For simplicity, this is the amount of capacitance. If the amount of capacitance gets too high everything will slow to a halt. Reduce the numbers in lines 27-29 and the capacitance value will fall. If it holds up really badly, check that everything is connected properly and there is definitely a resistor connected to the pins.
Capacitance sensing, in general, requires that you play around with different resistors and sizes of touch pads depending on what you are trying to achieve.
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10 megohm between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
CapacitiveSensor cs_2_8 = CapacitiveSensor(2,8); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapacitiveSensor cs_3_7 = CapacitiveSensor(3,7); // 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
long start = millis();
long total1 = cs_2_8.capacitiveSensor(5);
long total2 = cs_3_7.capacitiveSensor(5);
long total3 = cs_4_6.capacitiveSensor(5);
Serial.print(millis() - start); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug window spacing
Serial.print(total1); // print sensor output 1
Serial.print(total2); // print sensor output 2
Serial.println(total3); // print sensor output 3
delay(10); // arbitrary delay to limit data to serial port