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)
- OVERSAMPLE_1
- OVERSAMPLE_2
- OVERSAMPLE_4
- OVERSAMPLE_8
- OVERSAMPLE_16
- OVERSAMPLE_32
- OVERSAMPLE_64
- Discharge resistor size
- RESISTOR_0
- RESISTOR_20K
- RESISTOR_50K
- RESISTOR_100K
- Frequency mode - set frequency mode to reject synchronous noise (I haven't found a good description of these modes)
- FREQ_MODE_NONE
- FREQ_MODE_HOP
- FREQ_MODE_SPREAD
- FREQ_MODE_SPREAD_MEDIAN
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);
Initialized them:
qt_1.begin()
qt_2.begin()
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.
Top Comments