Capacitive Sensing in Teensy 3.0

Has anyone managed to get capacitive sensing working with the Teensy 3.0?
I have tried many configurations without success, I get a return of -2 for each input; using an oscilloscope I can see that the RX pins have an oscillation on them, although I would kind of expected that to be on the TX pin and not the RX pin.

This is the code I am currently running although I have varied it a lot and tried other TX RX pins. I am using a 2M resistor from the TX to the RX pin, I am sure these are correctly oriented.
I also have a pot at the moment controlling a servo, but intend to replace that with a 3 position output based on the last instance of the capacitive sensor inputs.


Controlling a servo position using a potentiometer (variable resistor) 

#include <Servo.h> 
#include <CapacitiveSensor.h>
Servo myservo;  // create servo object to control a servo 

// FIRST pin Sends energy SECOND PIN senses a change
CapacitiveSensor CS_open   = CapacitiveSensor(16,17);
CapacitiveSensor CS_mid    = CapacitiveSensor(16,18);
CapacitiveSensor CS_closed = CapacitiveSensor(16,19);
int threshold = 1000;

int LED = 13;               // flashing red led
const int redPin =    21; //variable leds
const int greenPin =  20; //variable leds
int potpin = 10;            // analog pin used to connect the potentiometer
int val;                        // variable to read the value from the analog pin 
int valr;                      // variable red led 
int valg;                      // variable green led 
int vals;                      // variable servo 
int n=0;

void setup() 
  pinMode(LED, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object 
  digitalWrite(LED, HIGH);
void loop() 
  digitalWrite(LED, HIGH);
  long SV_open    = CS_open.capacitiveSensor(30);
  long SV_mid     = CS_mid.capacitiveSensor(30);
  long SV_closed  = CS_closed.capacitiveSensor(30);
  // serial Print
  //Serial.print(millis() - start);        // check on performance in milliseconds
  Serial.print("\t");                    // tab character for debug windown spacing
  Serial.print(SV_open);                  // print sensor output 1
  Serial.print(SV_mid);                  // print sensor output 2
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  valr = map(val, 0,1023, 0, 255);     // scale it to use it with the red green leds
  valg = map(val, 0, 1023, 255, 0);     // scale it to use it with the red green leds
  // analogWrite(redPin, valr);
  // analogWrite(greenPin, valg);
  vals = map(val, 0, 1023, 500, 2300);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.writeMicroseconds (vals);  // set servo to mid-point
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  if(SV_open > threshold) 
    // turn the RED LED on and Green OFF
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    if(SV_closed > threshold) 
    // turn the GREEN LED on and RED OFF
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, LOW);
    if(SV_mid > threshold) 
    // turn the GREEN LED on and RED on
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);  

You’d be a lot better off asking this in the PJRC forum, which is for Teensy:


That library does not use the touch sensing capability of the Teensy 3. If I read it correctly, it was written to work on a Nano or Uno etc. It measures the capacitance between two pins by grounding one of them (in your case pin 16) and timing how long it takes for the other pin to drop below a threshold value.

You don't need to do this on Teensy 3. The touchRead function works just like the analogRead function except that it reads capacitance instead of voltage.

Try this demo sketch I've just tried out on my T3. It just prints the value received by touchRead on Teensy3 pin 23 (A9). When the wire is touched, the sketch prints a number above 2000. When the wire isn't being touched the number is around 800.

void setup(void)

void loop(void)