Example code for capacitive touch on Trinket M0. Note: make sure to install the Adafruit FreeTouch library.
// touch - Capacitive touch demo using FreeTouch. Trinket M0 version.
// Note: the pin silkscreened 1 is Analog Pin 0.
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
// Initialize A0 as a touch sensor
if (! qt_1.begin())
Serial.println("Failed to begin qt on pin A0");
int counter, result = 0;
counter = millis();
result = qt_1.measure();
Serial.print("QT 1: "); Serial.print(result);
Serial.print(" ("); Serial.print(millis() - counter); Serial.println(" ms)");
Information about the parameters for the constructor can be found at: https://forums.adafruit.com/viewtopic.php?f=8&p=610501 and are paraphrased below incase the link goes dead.
The parameters for the constructor are:
p - The input pin connected to the sensor.
t - How much to oversample the input. One of the following:
Oversampling is a process of taking several measurements, finding the average, and reporting that instead of the actual readings. It's way of reducing noise, but also improves the accuracy of the measurement. The average value of noise is zero (if it wasn't, it would be a source of free energy), so the more readings you add together, the closer the noise error in the measurements approaches zero.
There's another side-effect of adding noise to measurements though: it dithers the sensor output around the real input value. If I have a window comparator that only measures "below 1v" and "above 2v", and feed it a signal at 1.1v plus a couple of volts of noise, I can expect to see about ten "below 1v" outputs for every one "above 2v" output. The noise pushes the signals above and below the thresholds, but vanishes if I take the average of a large enough sample. Statistically, getting a 10:1 improvement in resolution like that would take about 64 readings to make the noise error small enough to ignore.
Oversampling takes longer though, so there's always a tradeoff between how much you can do and how long the signal will remain stable.
r - The value of the resistor that will discharge the touch pad. One of the following:
f - Frequency mode. One of the following:
For systems that take repeated measurements, there's one kind of noise that doesn't average to zero: noise that occurs at the same frequency as the sampling rate. There are all sorts of videos that show the effect of a camera being in sync with the thing it's recording.. this one is especially nice: https://www.youtube.com/watch?v=uENITui5_jU
For capacitive sensors, things like induced signal from fluorescent lights can create noise, and if the lamp frequency is just slightly off of your sampling frequency, the touch system will think it looks like a series of touches.
(If you have an oscilloscope, hold the tip of a probe between your fingers and put your hand within a foot or so of a fluorescent light.)
One way to prevent that is to change the sampling frequency. You can shift it up and down in a predictable pattern, hop from one frequency to another, or add some random jitter. Atmel's QTouch library supports all those options, but 'NONE' (fixed frequency readings) is the simplest.