touche-capacitive.ino
touchRead() is the signature Teensy function: it measures pin capacitance and detects a finger on a plain wire, with no 555 circuit or dedicated capacitive pad. Ideal for improvised touch interfaces.
Hardware
A Teensy 3.2 (NXP Kinetis MK20DX256, Cortex-M4 @ 72 MHz, 3.3 V logic) powered over USB. One bare wire (10-20 cm) or a small metal plate on pin 0; LED on 13.
Wiring
Sensor wire on pin 0 (nothing else), LED + 330 Ω on 13 to GND (3.3 V).
How it works
touchRead() returns near zero with no contact and several hundred with a finger. Hysteresis comes from the threshold plus the remembered state.
Running it
Install the Teensyduino plugin in the Arduino IDE, select the « Teensy 3.2 » board, click Upload then the Teensy Loader button. Note: 3.3 V pins.
Variants
Several sensor pins (a value array) for a capacitive keypad, or drive sound through the Audio library.
touche-capacitive.ino
// touchRead() : la broche devient un capteur capacitif sans circuit.
// Fonctionne sur une grande partie des broches Teensy (voir doc).
const int TOUCHE = 0; // broche capteur (fil nu ou plaque metallique)
const int LED = 13;
const int SEUIL = 500; // a calibrer avec la sortie serie
unsigned long derniereMesure = 0;
bool touche = false;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (millis() - derniereMesure >= 50) {
derniereMesure = millis();
int valeur = touchRead(TOUCHE); // sans contact ~0, doigt ~500-1000
Serial.print(F("touch = "));
Serial.print(valeur);
if (valeur > SEUIL && !touche) {
touche = true;
digitalWrite(LED, HIGH);
Serial.print(F(" -> TOUCHE"));
} else if (valeur < SEUIL && touche) {
touche = false;
digitalWrite(LED, LOW);
}
Serial.println();
}
}