|
| 1 | +##### |
| 2 | +TOUCH |
| 3 | +##### |
| 4 | + |
| 5 | +About |
| 6 | +----- |
| 7 | + |
| 8 | +Touch sensor is a peripheral, that has an internal oscilator circuit and it measures charge/discharge frequency over a fixed period of time on respective GPIO pins. |
| 9 | +Therefore these touch sensors are also known as capacitive sensors. For example, if you touch any of these pins, finger electrical charge will change this number of cycles, |
| 10 | +by changing the RC circuit attached to the touch sensor. The TouchRead() will return the number of cycles (charges/discharges) in a certain time (meas). |
| 11 | +The change of this count will be used to validate if a touch has happened or not. These pins can be easily integrated into capacitive pads, and replace mechanical buttons. |
| 12 | + |
| 13 | +.. note:: Touch peripheral is not present in every SoC. Refer to datasheet of each chip for more info. |
| 14 | + |
| 15 | +Arduino-ESP32 TOUCH API |
| 16 | +----------------------- |
| 17 | + |
| 18 | +TOUCH common API |
| 19 | +**************** |
| 20 | + |
| 21 | +touchRead |
| 22 | +^^^^^^^^^ |
| 23 | + |
| 24 | +This function gets the touch sensor data. Each touch sensor has a counter to count the number of charge/discharge cycles. When the pad is ‘touched’, |
| 25 | +the value in the counter will change because of the larger equivalent capacitance. The change of the data determines if the pad has been touched or not. |
| 26 | + |
| 27 | +.. code-block:: arduino |
| 28 | +
|
| 29 | + touch_value_t touchRead(uint8_t pin); |
| 30 | +
|
| 31 | +* ``pin`` GPIO pin to read TOUCH value |
| 32 | + |
| 33 | +This function will return touch pad value as uint16_t (ESP32) or uint32_t (ESP32-S2/S3). |
| 34 | + |
| 35 | +touchSetCycles |
| 36 | +^^^^^^^^^^^^^^ |
| 37 | + |
| 38 | +This function is used to set cycles that measurement operation takes. The result from touchRead, threshold and detection accuracy depend on these values. |
| 39 | +The defaults are setting touchRead to take ~0.5ms. |
| 40 | + |
| 41 | +.. code-block:: arduino |
| 42 | +
|
| 43 | + void touchSetCycles(uint16_t measure, uint16_t sleep); |
| 44 | +
|
| 45 | +* ``measure`` Sets the time that it takes to measure touch sensor value |
| 46 | +* ``sleep`` Sets waiting time before next measure cycle |
| 47 | + |
| 48 | +touchAttachInterrupt |
| 49 | +^^^^^^^^^^^^^^^^^^^^ |
| 50 | + |
| 51 | +This function is used to attach interrupt to the touch pad. The function will be called if a touch sensor value falls below the given |
| 52 | +threshold for ESP32 or rises above the given threshold for ESP32-S2/S3. To determine a proper threshold value between touched and untouched state, |
| 53 | +use touchRead() function. |
| 54 | + |
| 55 | +.. code-block:: arduino |
| 56 | +
|
| 57 | + void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), touch_value_t threshold); |
| 58 | +
|
| 59 | +* ``pin`` GPIO TOUCH pad pin |
| 60 | +* ``userFunc`` Function to be called when interrupt is triggered |
| 61 | +* ``threshold`` Sets the threshold when to call interrupt |
| 62 | + |
| 63 | +touchAttachInterruptArg |
| 64 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 65 | + |
| 66 | +This function is used to attach interrupt to the touch pad. In the function called by ISR you have the given arguments available. |
| 67 | + |
| 68 | +.. code-block:: arduino |
| 69 | +
|
| 70 | + void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, touch_value_t threshold); |
| 71 | +
|
| 72 | +* ``pin`` GPIO TOUCH pad pin |
| 73 | +* ``userFunc`` Function to be called when interrupt is triggered |
| 74 | +* ``arg`` Sets arguments to the interrupt |
| 75 | +* ``threshold`` Sets the threshold when to call interrupt |
| 76 | + |
| 77 | +touchDetachInterrupt |
| 78 | +^^^^^^^^^^^^^^^^^^^^ |
| 79 | + |
| 80 | +This function is used to detach interrupt from the touch pad. |
| 81 | + |
| 82 | +.. code-block:: arduino |
| 83 | +
|
| 84 | + void touchDetachInterrupt(uint8_t pin); |
| 85 | +
|
| 86 | +* ``pin`` GPIO TOUCH pad pin. |
| 87 | + |
| 88 | +TOUCH API specific for ESP32 chip (TOUCH_V1) |
| 89 | +******************************************** |
| 90 | + |
| 91 | +touchInterruptSetThresholdDirection |
| 92 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 93 | + |
| 94 | +This function is used to tell the driver if it shall activate the interrupt if the sensor is lower or higher than |
| 95 | +the threshold value. Default is lower. |
| 96 | + |
| 97 | +.. code-block:: arduino |
| 98 | +
|
| 99 | + void touchInterruptSetThresholdDirection(bool mustbeLower); |
| 100 | +
|
| 101 | +TOUCH API specific for ESP32S2 and ESP32S3 chip (TOUCH_V2) |
| 102 | +********************************************************** |
| 103 | + |
| 104 | +touchInterruptGetLastStatus |
| 105 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 106 | + |
| 107 | +This function is used get the lastest ISR status for the touch pad. |
| 108 | + |
| 109 | +.. code-block:: arduino |
| 110 | +
|
| 111 | + bool touchInterruptGetLastStatus(uint8_t pin); |
| 112 | +
|
| 113 | +This function returns true if the touch pad has been and continues pressed or false otherwise. |
| 114 | + |
| 115 | +Example Applications |
| 116 | +******************** |
| 117 | + |
| 118 | +Example of reading the touch sensor. |
| 119 | + |
| 120 | +.. literalinclude:: ../../../libraries/ESP32/examples/Touch/TouchRead/TouchRead.ino |
| 121 | + :language: arduino |
| 122 | + |
| 123 | +A usage example for the touch interrupts. |
| 124 | + |
| 125 | +.. literalinclude:: ../../../libraries/ESP32/examples/Touch/TouchInterrupt/TouchInterrupt.ino |
| 126 | + :language: arduino |
| 127 | + |
| 128 | +More examples can be found in our repository -> `Touch examples <https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples/Touch>`_. |
0 commit comments