The SAMD21 processor that is used on the QT Py has a built in Peripheral Touch Controller (PTC) that can detect touch on external capacitive sensors. The PTC has a mutual-capacitance mode to sense touch on an x-y matrix and a self-capacitance mode to detect touch on a single point (pin). The QT Py is in the 32 pin package so there are 6 capacitive touch pins available in self-capacitance mode (A0-A3,A6,A7).
Here is the PTC Block Diagram from the SAMD21 spec
Adafruit has a QTouch compatible Arduino library for the SAMD21. The library is FreeTouch: https://github.com/adafruit/Adafruit_FreeTouch
QTouch is Microchip's library for the PTC.
I decided that I would add 2 touch buttons to the project - one to change the NeoPixel pattern and one to play a sound (haven't implemented that yet).
I am going to use thumb tacks as my buttons and I printed a button holder that I could add to my ornament base.
Setting up the PTC with the FreeTouch library is reasonably straightforward. You initialize the button objects with 4 parameters:
- Input pin number
- Number of oversamples (for averaging)
- Discharge resistor size
- Frequency mode - set frequency mode to reject synchronous noise (I haven't found a good description of these modes)
So, I configured two available pins A0 and A1 (no external components required other than the wire to the thumbtack):
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Adafruit_FreeTouch qt_2 = Adafruit_FreeTouch(A1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Then set variables to store the measured values:
long newState = qt_1.measure();
long switchState = qt_2.measure();
I observed that the measured value averaged around 500 +/-50 with no touch and between 800-1000 when I touched the thumbtack, so I added a touch threshold of 750 which seems to work consistently.
Here is a very short video demonstrating using the left button to switch NeoPixel patterns.
Something I need to figure out is how to trigger an interrupt when the touch is detected. Otherwise I'll need to check for button "presses" while I'm running the NeoPixel patterns continuously.