Download the CapacitiveSensor library from Arduino written by Paul Badger, unzip the file and move the entire folder into your Arduino libraries folder.
Here's a video overview of how the circuit and code work, thanks to Paul Stoffregen:
Fire up the Adafruit version of the Arduino IDE (get it here) and open the sample sketch for the CapacitiveSensor library.

Find this first section of code:
CapacitiveSensor   cs_9_10 = CapacitiveSensor(4,2);       
CapacitiveSensor   cs_9_2 = CapacitiveSensor(4,5);       
CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);
And change to reflect your current wiring, commenting out all but one sensor configuration:
CapacitiveSensor   cs_9_10 = CapacitiveSensor(9,10);       
//CapacitiveSensor   cs_9_2 = CapacitiveSensor(9,2);       
//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);
Also find this corresponding bit of code:
And update to read:
void loop()                    
{
    long start = millis();
    long total1 =  cs_9_10.capacitiveSensor(30);
    //long total2 =  cs_4_5.capacitiveSensor(30);
    //long total3 =  cs_4_8.capacitiveSensor(30);

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

    Serial.println(total1);                  // print sensor output 1
    //Serial.print("\t");
    //Serial.print(total2);                  // print sensor output 2
    //Serial.print("\t");
    //Serial.println(total3);                // print sensor output 3

    delay(10);                             // arbitrary delay to limit data to serial port 
}
Upload this code to your Flora board and open Arduino's serial monitor. Touch the conductive fabric and watch the numbers change on screen. Take note of the low end of the spike-- for me the reading always went above 4000 when I touched the fabric sensor.
Add an IF statement that sets Flora's onboard LED (D7) to light up if the sensor reading exceeds the "I touched it" threshold:
    if (total1 > 4000){
      digitalWrite(7, HIGH);
    }else{
      digitalWrite(7, LOW);
After uploading this code you should see Flora's red LED light up every time you touch the fabric. Now onto a more interesting output, the RGB pixel!
Here's the entire sketch, updated to drive a single RGB Neo Pixel. Copy it and paste it into a new sketch or replace your current sketch:
#include <CapacitiveSensor.h>
#include "Adafruit_FloraPixel.h"


/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 * Modified by Becky Stern 2013 to change the color of one RGB Neo Pixel based on touch input
 */


CapacitiveSensor   cs_9_10 = CapacitiveSensor(9,10);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
//CapacitiveSensor   cs_9_2 = CapacitiveSensor(9,2);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
Adafruit_FloraPixel strip = Adafruit_FloraPixel(1);

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

}

void loop()                    
{
    long start = millis();
    long total1 =  cs_9_10.capacitiveSensor(30);
    //long total2 =  cs_9_2.capacitiveSensor(30);
    //long total3 =  cs_4_8.capacitiveSensor(30);

if (total1 > 4000){
  digitalWrite(7, HIGH);
  strip.setPixelColor(0, Color(0,255,0));  
  strip.show();
} else {
  strip.setPixelColor(0, Color(0,0,0)); 
  strip.show();
}
  
    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.println(total1);                  // print sensor output 1
    //Serial.print("\t");
    //Serial.println(total2);                  // print sensor output 2
    //Serial.print("\t");
    //Serial.println(total3);                // print sensor output 3

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

// Create a 24 bit color value from R,G,B
RGBPixel Color(byte r, byte g, byte b)
{
  RGBPixel p;
  
  p.red = r;
  p.green = g;
  p.blue = b;
  
  return p;
}
Test your sensor by touching the conductive fabric. The pixel should light up when you are in contact with the sensor!

This guide was first published on Jan 16, 2013. It was last updated on Jan 16, 2013.

This page (Code) was last updated on Jan 16, 2013.

Text editor powered by tinymce.