### DIY Touch Sensor (Capacitive Sensor)

Capacitive Sensors is a technology which detects proximity or touch (by a hand/skin, or any conductive object). The sensor measures the capacitance between the input and output nodes to detect a touch. The sensor detects anything that is conductive, so these sensors can be used to replace any normal switches to make them touch sensitive or even be utilized in making touch screens for monitors, touch-pads and touch sensitive buttons in phones, laptops or other devices.

The sensor setup in the example below is a simple DIY setup without using a commercial sensor chip.

Setup:
Attach a high value resistor (1-10M Ohm) between an input and an output pin. Also connect a short bare copper or aluminum wire/foil to the input pin. If the wire is to be a longer one, make sure it isn't touching any other wires along the way, or just use a covered wire with a small uncovered area at its tip. This will be the touch sensor for the capacitive sensor (i.e. activates at touch).

An LED is also connected to a separate output pin and GND. This LED turns on when someone touches the sensor with a conductive object (e.g. capacitive sensors are most commonly used to sense touch with skin/fingers etc.)

It is also possible to vary the capacitance reading of this setup to detect even when one's hand is 3 to 4 inches from the sensor, or make it activate just on absolute touch. One can use lower values of R (e.g. 1 M Ohm or less) for absolute touch to activate the sensor. With a 10 M resistor the sensor should start to respond 1-2 inches away.

Code:
When the value at the output pin is changed from LOW to HIGH, it changes the state of input pin to LOW(or 0) for a very short time interval. This time interval is defined by:

T  =  R  x  C,

where
T  =  time interval,
R  =  resistance,
C  =  capacitance of the sensor + capacitance of any conductive object in contact with the sensor pin

So, this time interval increases if the sensor on input pin (the bare copper/aluminum wire) is touched with a conductive object. And the interval reduces again when the conductive
object is removed from the sensor.  So, we measure the length of the time interval to get a measure of capacitance on the touch sensor.

Threshold:
The value of the threshold here depends on how sensitive the user wants the sensor to be.  The lower bound of the threshold would be the value of R (the resistance) itself, since that remains constant in when measuring T = R x C. But, the upper bound can be changed depending on the requirements of the system.

Smoothing:
However, there might be a lot of jitter as well as environmental conditions that might make the
capacitance value jump around a lot. This can be overcome by using a smoothing function. For example, this can be done by reading the capacitance measure for a number of times and then averaging the values overall.

Circuit:

Schematic:
Sample Arduino Program:
When the output at pin4 transitions from LOW to HIGH, it changes the state of input pin5 to LOW(or 0) for a very short time interval. This time interval increases if the sensor on input pin5 is touched with a conductive object and vice versa.

At the start of each main loop cycle in this program, we set the value of a variable 'capX' to 0. Then for the time interval the value at input pin5 returns LOW, we increment 'capX'. This results in 'capX' being barely incremented if the sensor is not in contact with a conductive object. But, as soon as someone holds/touches the sensor the value of capX quickly increments because of the longer time interval. So, if the capX value is bigger than a given threshold, it means the sensor just detected a touch.

The value of the threshold here depends on how sensitive the user wants the sensor to be and/or the environmental affect the initial value at the sensor itself.

 /* This code turns the LED on while the sensor is in contact with a conductive material (e.g. when someone touches it with their bare skin/fingers) Setup:Attach a high value resistor (1-10M Ohm) between an output pin 4 and input pin 5. Also connect a short bare copper or aluminum wire/foil to the input pin5. Connect an LED to output pin13 and GND. By: Naureen Mahmood. */ #define LED        13#define THRESHOLD   5 int capI;      // interval when sensor pin 5 returns LOW void setup() {  Serial.begin(9600);  pinMode(LED, OUTPUT);  pinMode(4, OUTPUT);     // output pin  pinMode(5, INPUT);      // input pin} void loop() {  capI = 0;      // clear out capacitance measure at each loop   // transition output pin4 LOW-to-HIGH  to 'activate' sensor pin5  digitalWrite(4, HIGH);        // On activation, value of pin 5 stays LOW for a time interval T = R*C.   // C is big if the sensor is touched with a conductive object.  // Increment capI for the interval while pin5 is LOW  int val = digitalRead(5);  // read the input to be checked  while (val != HIGH){        capI++;        val = digitalRead(5);    // re-read the input to be checked   }  delay(1);    // transition output pin4 HIGH-to-LOW to 'deactivate' sensor pin5  digitalWrite(4, LOW);        Serial.println(capI, DEC);  // print out interval   if (capI > THRESHOLD)       // Turn LED on if capI is above threshold    digitalWrite(LED, HIGH);  else      digitalWrite(LED,  LOW);}

Sample Arduino Code (with smoothing filter):