Making your own capacitive touch sensor

The very minimum you need to make a capacitive touch sensor is 3 jumper wires, a mini breadboard, and a 1M OHM resistor.

In this example I connected the 1st jumper wire to pin 4 to act as the sender. I then connected the 2nd jumper wire to pin 2 to act as the receiver (sensor). Then I placed the 1M OHM resistor between the jumper wire connection. I then used the jumper wire to connect to the receiver, allowing me to touch the metal pin with my fingers.

Using the CapacitiveSensor library

  1. `git clone[github.com]
  2. In the Arduino IDE, select Sketch > Import Library, Add Library...
  3. select the CapactiveSensor library and then click OK
  4. Copy and paste the following code into the Arduino IDE
#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch

 * Uses a high value resistor e.g. 1M between send pin and receive pin
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 */


CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 1M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int led = 13;

void setup()                    
{
  pinMode(led, OUTPUT);   
  cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
}

void loop()                    
{

    long total1 =  cs_4_2.capacitiveSensor(30);
    
    if (total1 > 80) {

      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(10);               // wait for 10 milliseconds
      digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW    
    }

}

Compile and upload the code. You should now see pin 13 blink whenever you touch the contact pin of the 3rd jumper wire (which connects to the receiver on pin 2).

To make it more fun I placed the jumper wire contact on the stem of a screwdriver and touched that to make the LED blink.

Resources

Tags:
Source:
2057hrs.txt
Published:
29-02-2016 20:57