Touch Sensor¶
Introduction¶
A touch sensor system is built on a substrate which carries electrodes and relevant connections under a protective flat surface. When a user touches the surface, the capacitance variation is used to evaluate if the touch was valid.
ESP32-S2 can handle up to 14 capacitive touch pads / GPIOs.
The sensing pads can be arranged in different combinations (e.g., matrix, slider), so that a larger area or more points can be detected. The touch pad sensing process is under the control of a hardware-implemented finite-state machine (FSM) which is initiated by software or a dedicated hardware timer.
Design, operation, and control registers of a touch sensor are discussed in ESP32-S2 Technical Reference Manual (PDF). Please refer to this manual for additional details on how this subsystem works.
In-depth design details of touch sensors and firmware development guidelines for ESP32-S2 are available in Touch Sensor Application Note.
Functionality Overview¶
Description of API is broken down into groups of functions to provide a quick overview of the following features:
Initialization of touch pad driver
Configuration of touch pad GPIO pins
Taking measurements
Adjusting parameters of measurements
Filtering measurements
Touch detection methods
Setting up interrupts to report touch detection
Waking up from Sleep mode on interrupt
For detailed description of a particular function, please go to Section API Reference. Practical implementation of this API is covered in Section Application Examples.
Initialization¶
Before using a touch pad, you need to initialize the touch pad driver by calling the function touch_pad_init()
. This function sets several .._DEFAULT
driver parameters listed in API Reference under Macros. It also removes the information about which pads have been touched before, if any, and disables interrupts.
If the driver is not required anymore, deinitialize it by calling touch_pad_deinit()
.
Configuration¶
Enabling the touch sensor functionality for a particular GPIO is done with touch_pad_config()
.
Use the function touch_pad_set_fsm_mode()
to select if touch pad measurement (operated by FSM) should be started automatically by a hardware timer, or by software. If software mode is selected, use touch_pad_sw_start()
to start the FSM.
Touch State Measurements¶
The following function come in handy to read raw measurements from the sensor:
It can also be used, for example, to evaluate a particular touch pad design by checking the range of sensor readings when a pad is touched or released. This information can be then used to establish a touch threshold.
For the demonstration of how to read the touch pad data, check the application example peripherals/touch_pad_read.
Optimization of Measurements¶
A touch sensor has several configurable parameters to match the characteristics of a particular touch pad design. For instance, to sense smaller capacity changes, it is possible to narrow down the reference voltage range within which the touch pads are charged / discharged. The high and low reference voltages are set using the function touch_pad_set_voltage()
.
Besides the ability to discern smaller capacity changes, a positive side effect is reduction of power consumption for low power applications. A likely negative effect is an increase in measurement noise. If the dynamic range of obtained readings is still satisfactory, then further reduction of power consumption might be done by reducing the measurement time with touch_pad_set_meas_time()
.
The following list summarizes available measurement parameters and corresponding ‘set’ functions:
Touch pad charge / discharge parameters:
voltage range:
touch_pad_set_voltage()
speed (slope):
touch_pad_set_cnt_mode()
Measurement time:
touch_pad_set_meas_time()
Relationship between the voltage range (high / low reference voltages), speed (slope), and measurement time is shown in the figure below.
The last chart Output represents the touch sensor reading, i.e., the count of pulses collected within the measurement time.
All functions are provided in pairs to set a specific parameter and to get the current parameter’s value, e.g., touch_pad_set_voltage()
and touch_pad_get_voltage()
.
Filtering of Measurements¶
If measurements are noisy, you can filter them with provided API functions. The ESP32-S2’s touch functionality provide two sets of APIs for doing this.
There is an internal touch channel that is not connected to any external GPIO. The measurements from this denoise pad can be used to filters out interference introduced on all channels, such as noise introduced by the power supply and external EMI.
The denoise paramaters are set with the function touch_pad_denoise_set_config()
and started by with touch_pad_denoise_enable()
There is also a configurable hardware implemented IIR-filter (infinite impulse response). This IIR-filter is configured with the function touch_pad_filter_set_config()
and enabled by calling touch_pad_filter_enable()
Touch Detection¶
Touch detection is implemented in ESP32’s hardware based on the user-configured threshold and raw measurements executed by FSM. Use the functions touch_pad_get_status()
to check which pads have been touched and touch_pad_clear_status()
to clear the touch status information.
Hardware touch detection can also be wired to interrupts. This is described in the next section.
If measurements are noisy and capacity changes are small, hardware touch detection might be unreliable. To resolve this issue, instead of using hardware detection / provided interrupts, implement measurement filtering and perform touch detection in your own application. For sample implementation of both methods of touch detection, see peripherals/touch_pad_interrupt.
Touch Triggered Interrupts¶
Before enabling an interrupt on a touch detection, you should establish a touch detection threshold. Use the functions described in Touch State Measurements to read and display sensor measurements when a pad is touched and released. Apply a filter if measurements are noisy and relative capacity changes are small. Depending on your application and environment conditions, test the influence of temperature and power supply voltage changes on measured values.
Once a detection threshold is established, it can be set during initialization with touch_pad_config()
or at the runtime with touch_pad_set_thresh()
.
Finally, configure and manage interrupt calls using the following functions:
When interrupts are operational, you can obtain the information from which particular pad an interrupt came by invoking touch_pad_get_status()
and clear the pad status with touch_pad_clear_status()
.
Application Examples¶
Touch sensor read example: peripherals/touch_pad_read.
Touch sensor interrupt example: peripherals/touch_pad_interrupt.
API Reference¶
Header File¶
Functions¶
-
esp_err_t
touch_pad_fsm_start
(void)¶ Set touch sensor FSM start.
- Note
Start FSM after the touch sensor FSM mode is set.
- Note
Call this function will reset benchmark of all touch channels.
- Return
ESP_OK on success
-
esp_err_t
touch_pad_sw_start
(void)¶ Trigger a touch sensor measurement, only support in SW mode of FSM.
- Return
ESP_OK on success
-
esp_err_t
touch_pad_set_meas_time
(uint16_t sleep_cycle, uint16_t meas_times)¶ Set touch sensor times of charge and discharge and sleep time. Excessive total time will slow down the touch response. Too small measurement time will not be sampled enough, resulting in inaccurate measurements.
- Note
The greater the duty cycle of the measurement time, the more system power is consumed.
- Return
ESP_OK on success
- Parameters
sleep_cycle
: The touch sensor will sleep after each measurement. sleep_cycle decide the interval between each measurement. t_sleep = sleep_cycle / (RTC_SLOW_CLK frequency). The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function.meas_times
: The times of charge and discharge in each measure process of touch channels. The timer frequency is 8Mhz. Range: 0 ~ 0xffff. Recommended typical value: Modify this value to make the measurement time around 1ms.
-
esp_err_t
touch_pad_get_meas_time
(uint16_t *sleep_cycle, uint16_t *meas_times)¶ Get touch sensor times of charge and discharge and sleep time.
- Return
ESP_OK on success
- Parameters
sleep_cycle
: Pointer to accept sleep cycle numbermeas_times
: Pointer to accept measurement times count.
-
esp_err_t
touch_pad_set_idle_channel_connect
(touch_pad_conn_type_t type)¶ Set connection type of touch channel in idle status. When a channel is in measurement mode, other initialized channels are in idle mode. The touch channel is generally adjacent to the trace, so the connection state of the idle channel affects the stability and sensitivity of the test channel. The
CONN_HIGHZ
(high resistance) setting increases the sensitivity of touch channels. TheCONN_GND
(grounding) setting increases the stability of touch channels.- Return
ESP_OK on success
- Parameters
type
: Select idle channel connect to high resistance state or ground.
-
esp_err_t
touch_pad_get_idle_channel_connect
(touch_pad_conn_type_t *type)¶ Set connection type of touch channel in idle status. When a channel is in measurement mode, other initialized channels are in idle mode. The touch channel is generally adjacent to the trace, so the connection state of the idle channel affects the stability and sensitivity of the test channel. The
CONN_HIGHZ
(high resistance) setting increases the sensitivity of touch channels. TheCONN_GND
(grounding) setting increases the stability of touch channels.- Return
ESP_OK on success
- Parameters
type
: Pointer to connection type.
-
esp_err_t
touch_pad_set_thresh
(touch_pad_t touch_num, uint32_t threshold)¶ Set the trigger threshold of touch sensor. The threshold determines the sensitivity of the touch sensor. The threshold is the original value of the trigger state minus the benchmark value.
- Note
If set “TOUCH_PAD_THRESHOLD_MAX”, the touch is never be triggered.
- Return
ESP_OK on success
- Parameters
touch_num
: touch pad indexthreshold
: threshold of touch sensor. Should be less than the max change value of touch.
-
esp_err_t
touch_pad_get_thresh
(touch_pad_t touch_num, uint32_t *threshold)¶ Get touch sensor trigger threshold.
- Return
ESP_OK on success
ESP_ERR_INVALID_ARG if argument is wrong
- Parameters
touch_num
: touch pad indexthreshold
: pointer to accept threshold
-
esp_err_t
touch_pad_set_channel_mask
(uint16_t enable_mask)¶ Register touch channel into touch sensor scan group. The working mode of the touch sensor is cyclically scanned. This function will set the scan bits according to the given bitmask.
- Note
If set this mask, the FSM timer should be stop firsty.
- Note
The touch sensor that in scan map, should be deinit GPIO function firstly by
touch_pad_io_init
.- Return
ESP_OK on success
- Parameters
enable_mask
: bitmask of touch sensor scan group. e.g. TOUCH_PAD_NUM14 -> BIT(14)
-
esp_err_t
touch_pad_get_channel_mask
(uint16_t *enable_mask)¶ Get the touch sensor scan group bit mask.
- Return
ESP_OK on success
- Parameters
enable_mask
: Pointer to bitmask of touch sensor scan group. e.g. TOUCH_PAD_NUM14 -> BIT(14)
-
esp_err_t
touch_pad_clear_channel_mask
(uint16_t enable_mask)¶ Clear touch channel from touch sensor scan group. The working mode of the touch sensor is cyclically scanned. This function will clear the scan bits according to the given bitmask.
- Note
If clear all mask, the FSM timer should be stop firsty.
- Return
ESP_OK on success
- Parameters
enable_mask
: bitmask of touch sensor scan group. e.g. TOUCH_PAD_NUM14 -> BIT(14)
-
esp_err_t
touch_pad_config
(touch_pad_t touch_num)¶ Configure parameter for each touch channel.
- Note
Touch num 0 is denoise channel, please use
touch_pad_denoise_enable
to set denoise function- Return
ESP_OK Success
ESP_ERR_INVALID_ARG if argument wrong
ESP_FAIL if touch pad not initialized
- Parameters
touch_num
: touch pad index
-
esp_err_t
touch_pad_reset
(void)¶ Reset the FSM of touch module.
- Note
Call this function after
touch_pad_fsm_stop
.- Return
ESP_OK Success
-
touch_pad_t
touch_pad_get_current_meas_channel
(void)¶ Get the current measure channel.
- Note
Should be called when touch sensor measurement is in cyclic scan mode.
- Return
touch channel number
-
uint32_t
touch_pad_read_intr_status_mask
(void)¶ Get the touch sensor interrupt status mask.
- Return
touch interrupt bit
-
esp_err_t
touch_pad_intr_enable
(touch_pad_intr_mask_t int_mask)¶ Enable touch sensor interrupt by bitmask.
- Return
ESP_OK on success
- Parameters
int_mask
: Pad mask to enable interrupts
-
esp_err_t
touch_pad_intr_disable
(touch_pad_intr_mask_t int_mask)¶ Disable touch sensor interrupt by bitmask.
- Return
ESP_OK on success
- Parameters
int_mask
: Pad mask to disable interrupts
-
esp_err_t
touch_pad_intr_clear
(touch_pad_intr_mask_t int_mask)¶ Clear touch sensor interrupt by bitmask.
- Return
ESP_OK on success
- Parameters
int_mask
: Pad mask to clear interrupts
-
esp_err_t
touch_pad_isr_register
(intr_handler_t fn, void *arg, touch_pad_intr_mask_t intr_mask)¶ Register touch-pad ISR. The handler will be attached to the same CPU core that this function is running on.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG Arguments error
ESP_ERR_NO_MEM No memory
- Parameters
fn
: Pointer to ISR handlerarg
: Parameter for ISRintr_mask
: Enable touch sensor interrupt handler by bitmask.
-
esp_err_t
touch_pad_timeout_set
(bool enable, uint32_t threshold)¶ Enable/disable the timeout check and set timeout threshold for all touch sensor channels measurements. If enable: When the touch reading of a touch channel exceeds the measurement threshold, a timeout interrupt will be generated. If disable: the FSM does not check if the channel under measurement times out.
- Note
The threshold compared with touch readings.
- Note
In order to avoid abnormal short circuit of some touch channels. This function should be turned on. Ensure the normal operation of other touch channels.
- Return
ESP_OK Success
- Parameters
enable
: true(default): Enable the timeout check; false: Disable the timeout check.threshold
: For all channels, the maximum value that will not be exceeded during normal operation.
-
esp_err_t
touch_pad_timeout_resume
(void)¶ Call this interface after timeout to make the touch channel resume normal work. Point on the next channel to measure. If this API is not called, the touch FSM will stop the measurement after timeout interrupt.
- Note
Call this API after finishes the exception handling by user.
- Return
ESP_OK Success
-
esp_err_t
touch_pad_read_raw_data
(touch_pad_t touch_num, uint32_t *raw_data)¶ get raw data of touch sensor.
- Note
After the initialization is complete, the “raw_data” is max value. You need to wait for a measurement cycle before you can read the correct touch value.
- Return
ESP_OK Success
ESP_FAIL Touch channel 0 haven’t this parameter.
- Parameters
touch_num
: touch pad indexraw_data
: pointer to accept touch sensor value
-
esp_err_t
touch_pad_read_benchmark
(touch_pad_t touch_num, uint32_t *benchmark)¶ get benchmark of touch sensor.
- Note
After initialization, the benchmark value is the maximum during the first measurement period.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG Touch channel 0 haven’t this parameter.
- Parameters
touch_num
: touch pad indexbenchmark
: pointer to accept touch sensor benchmark value
-
esp_err_t
touch_pad_filter_read_smooth
(touch_pad_t touch_num, uint32_t *smooth)¶ Get smoothed data that obtained by filtering the raw data.
- Parameters
touch_num
: touch pad indexsmooth
: pointer to smoothed data
-
esp_err_t
touch_pad_reset_benchmark
(touch_pad_t touch_num)¶ Force reset benchmark to raw data of touch sensor.
- Return
ESP_OK Success
- Parameters
touch_num
: touch pad indexTOUCH_PAD_MAX Reset basaline of all channels
-
esp_err_t
touch_pad_filter_set_config
(touch_filter_config_t *filter_info)¶ set parameter of touch sensor filter and detection algorithm. For more details on the detection algorithm, please refer to the application documentation.
- Return
ESP_OK Success
- Parameters
filter_info
: select filter type and threshold of detection algorithm
-
esp_err_t
touch_pad_filter_get_config
(touch_filter_config_t *filter_info)¶ get parameter of touch sensor filter and detection algorithm. For more details on the detection algorithm, please refer to the application documentation.
- Return
ESP_OK Success
- Parameters
filter_info
: select filter type and threshold of detection algorithm
-
esp_err_t
touch_pad_filter_enable
(void)¶ enable touch sensor filter for detection algorithm. For more details on the detection algorithm, please refer to the application documentation.
- Return
ESP_OK Success
-
esp_err_t
touch_pad_filter_disable
(void)¶ disable touch sensor filter for detection algorithm. For more details on the detection algorithm, please refer to the application documentation.
- Return
ESP_OK Success
-
esp_err_t
touch_pad_denoise_set_config
(touch_pad_denoise_t *denoise)¶ set parameter of denoise pad (TOUCH_PAD_NUM0). T0 is an internal channel that does not have a corresponding external GPIO. T0 will work simultaneously with the measured channel Tn. Finally, the actual measured value of Tn is the value after subtracting lower bits of T0. The noise reduction function filters out interference introduced simultaneously on all channels, such as noise introduced by power supplies and external EMI.
- Return
ESP_OK Success
- Parameters
denoise
: parameter of denoise
-
esp_err_t
touch_pad_denoise_get_config
(touch_pad_denoise_t *denoise)¶ get parameter of denoise pad (TOUCH_PAD_NUM0).
- Return
ESP_OK Success
- Parameters
denoise
: Pointer to parameter of denoise
-
esp_err_t
touch_pad_denoise_enable
(void)¶ enable denoise function. T0 is an internal channel that does not have a corresponding external GPIO. T0 will work simultaneously with the measured channel Tn. Finally, the actual measured value of Tn is the value after subtracting lower bits of T0. The noise reduction function filters out interference introduced simultaneously on all channels, such as noise introduced by power supplies and external EMI.
- Return
ESP_OK Success
-
esp_err_t
touch_pad_denoise_read_data
(uint32_t *data)¶ Get denoise measure value (TOUCH_PAD_NUM0).
- Return
ESP_OK Success
- Parameters
data
: Pointer to receive denoise value
-
esp_err_t
touch_pad_waterproof_set_config
(touch_pad_waterproof_t *waterproof)¶ set parameter of waterproof function.
The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. Guard pad is used to detect the large area of water covering the touch panel. Shield pad is used to shield the influence of water droplets covering the touch panel. It is generally designed as a grid and is placed around the touch buttons.
- Return
ESP_OK Success
- Parameters
waterproof
: parameter of waterproof
-
esp_err_t
touch_pad_waterproof_get_config
(touch_pad_waterproof_t *waterproof)¶ get parameter of waterproof function.
- Return
ESP_OK Success
- Parameters
waterproof
: parameter of waterproof
-
esp_err_t
touch_pad_waterproof_enable
(void)¶ Enable parameter of waterproof function. Should be called after function
touch_pad_waterproof_set_config
.- Return
ESP_OK Success
-
esp_err_t
touch_pad_waterproof_disable
(void)¶ Disable parameter of waterproof function.
- Return
ESP_OK Success
-
esp_err_t
touch_pad_proximity_enable
(touch_pad_t touch_num, bool enabled)¶ Enable/disable proximity function of touch channels. The proximity sensor measurement is the accumulation of touch channel measurements.
- Note
Supports up to three touch channels configured as proximity sensors.
- Return
ESP_OK: Configured correctly.
ESP_ERR_INVALID_ARG: Touch channel number error.
ESP_ERR_NOT_SUPPORTED: Don’t support configured.
- Parameters
touch_num
: touch pad indexenabled
: true: enable the proximity function; false: disable the proximity function
-
esp_err_t
touch_pad_proximity_set_count
(touch_pad_t touch_num, uint32_t count)¶ Set measure count of proximity channel. The proximity sensor measurement is the accumulation of touch channel measurements.
- Note
All proximity channels use the same
count
value. So please pass the parameterTOUCH_PAD_MAX
.- Return
ESP_OK: Configured correctly.
ESP_ERR_INVALID_ARG: Touch channel number error.
- Parameters
touch_num
: Touch pad index. In this version, pass the parameterTOUCH_PAD_MAX
.count
: The cumulative times of measurements for proximity pad. Range: 0 ~ 255.
-
esp_err_t
touch_pad_proximity_get_count
(touch_pad_t touch_num, uint32_t *count)¶ Get measure count of proximity channel. The proximity sensor measurement is the accumulation of touch channel measurements.
- Note
All proximity channels use the same
count
value. So please pass the parameterTOUCH_PAD_MAX
.- Return
ESP_OK: Configured correctly.
ESP_ERR_INVALID_ARG: Touch channel number error.
- Parameters
touch_num
: Touch pad index. In this version, pass the parameterTOUCH_PAD_MAX
.count
: The cumulative times of measurements for proximity pad. Range: 0 ~ 255.
-
esp_err_t
touch_pad_proximity_get_data
(touch_pad_t touch_num, uint32_t *measure_out)¶ Get the accumulated measurement of the proximity sensor. The proximity sensor measurement is the accumulation of touch channel measurements.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG Touch num is not proximity
- Parameters
touch_num
: touch pad indexmeasure_out
: If the accumulation process does not end, themeasure_out
is the process value.
-
esp_err_t
touch_pad_sleep_channel_get_info
(touch_pad_sleep_channel_t *slp_config)¶ Get parameter of touch sensor sleep channel. The touch sensor can works in sleep mode to wake up sleep.
- Note
After the sleep channel is configured, Please use special functions for sleep channel. e.g. The user should uses
touch_pad_sleep_channel_read_data
instead oftouch_pad_read_raw_data
to obtain the sleep channel reading.- Return
ESP_OK Success
- Parameters
slp_config
: touch sleep pad config.
-
esp_err_t
touch_pad_sleep_channel_enable
(touch_pad_t pad_num, bool enable)¶ Enable/Disable sleep channel function for touch sensor. The touch sensor can works in sleep mode to wake up sleep.
- Note
ESP32S2 only support one sleep channel.
- Note
After the sleep channel is configured, Please use special functions for sleep channel. e.g. The user should uses
touch_pad_sleep_channel_read_data
instead oftouch_pad_read_raw_data
to obtain the sleep channel reading.- Return
ESP_OK Success
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.enable
: true: enable sleep pad for touch sensor; false: disable sleep pad for touch sensor;
-
esp_err_t
touch_pad_sleep_channel_enable_proximity
(touch_pad_t pad_num, bool enable)¶ Enable/Disable proximity function for sleep channel. The touch sensor can works in sleep mode to wake up sleep.
- Note
ESP32S2 only support one sleep channel.
- Return
ESP_OK Success
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.enable
: true: enable proximity for sleep channel; false: disable proximity for sleep channel;
-
esp_err_t
touch_pad_sleep_set_threshold
(touch_pad_t pad_num, uint32_t touch_thres)¶ Set the trigger threshold of touch sensor in deep sleep. The threshold determines the sensitivity of the touch sensor.
- Note
In general, the touch threshold during sleep can use the threshold parameter parameters before sleep.
- Return
ESP_OK Success
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.touch_thres
: touch sleep pad threshold
-
esp_err_t
touch_pad_sleep_get_threshold
(touch_pad_t pad_num, uint32_t *touch_thres)¶ Get the trigger threshold of touch sensor in deep sleep. The threshold determines the sensitivity of the touch sensor.
- Note
In general, the touch threshold during sleep can use the threshold parameter parameters before sleep.
- Return
ESP_OK Success
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.touch_thres
: touch sleep pad threshold
-
esp_err_t
touch_pad_sleep_channel_read_benchmark
(touch_pad_t pad_num, uint32_t *benchmark)¶ Read benchmark of touch sensor sleep channel.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG parameter is NULL
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.benchmark
: pointer to accept touch sensor benchmark value
-
esp_err_t
touch_pad_sleep_channel_read_smooth
(touch_pad_t pad_num, uint32_t *smooth_data)¶ Read smoothed data of touch sensor sleep channel. Smoothed data is filtered from the raw data.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG parameter is NULL
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.smooth_data
: pointer to accept touch sensor smoothed data
-
esp_err_t
touch_pad_sleep_channel_read_data
(touch_pad_t pad_num, uint32_t *raw_data)¶ Read raw data of touch sensor sleep channel.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG parameter is NULL
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.raw_data
: pointer to accept touch sensor raw data
-
esp_err_t
touch_pad_sleep_channel_reset_benchmark
(void)¶ Reset benchmark of touch sensor sleep channel.
- Return
ESP_OK Success
-
esp_err_t
touch_pad_sleep_channel_read_proximity_cnt
(touch_pad_t pad_num, uint32_t *proximity_cnt)¶ Read proximity count of touch sensor sleep channel.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG parameter is NULL
- Parameters
pad_num
: Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode.proximity_cnt
: pointer to accept touch sensor proximity count value
Header File¶
Functions¶
-
esp_err_t
touch_pad_init
(void)¶ Initialize touch module.
- Note
If default parameter don’t match the usage scenario, it can be changed after this function.
- Return
ESP_OK Success
ESP_ERR_NO_MEM Touch pad init error
-
esp_err_t
touch_pad_deinit
(void)¶ Un-install touch pad driver.
- Note
After this function is called, other touch functions are prohibited from being called.
- Return
ESP_OK Success
ESP_FAIL Touch pad driver not initialized
-
esp_err_t
touch_pad_io_init
(touch_pad_t touch_num)¶ Initialize touch pad GPIO.
- Return
ESP_OK on success
ESP_ERR_INVALID_ARG if argument is wrong
- Parameters
touch_num
: touch pad index
-
esp_err_t
touch_pad_set_voltage
(touch_high_volt_t refh, touch_low_volt_t refl, touch_volt_atten_t atten)¶ Set touch sensor high voltage threshold of chanrge. The touch sensor measures the channel capacitance value by charging and discharging the channel. So the high threshold should be less than the supply voltage.
- Return
ESP_OK on success
ESP_ERR_INVALID_ARG if argument is wrong
- Parameters
refh
: the value of DREFHrefl
: the value of DREFLatten
: the attenuation on DREFH
-
esp_err_t
touch_pad_get_voltage
(touch_high_volt_t *refh, touch_low_volt_t *refl, touch_volt_atten_t *atten)¶ Get touch sensor reference voltage,.
- Return
ESP_OK on success
- Parameters
refh
: pointer to accept DREFH valuerefl
: pointer to accept DREFL valueatten
: pointer to accept the attenuation on DREFH
-
esp_err_t
touch_pad_set_cnt_mode
(touch_pad_t touch_num, touch_cnt_slope_t slope, touch_tie_opt_t opt)¶ Set touch sensor charge/discharge speed for each pad. If the slope is 0, the counter would always be zero. If the slope is 1, the charging and discharging would be slow, accordingly. If the slope is set 7, which is the maximum value, the charging and discharging would be fast.
- Note
The higher the charge and discharge current, the greater the immunity of the touch channel, but it will increase the system power consumption.
- Return
ESP_OK on success
ESP_ERR_INVALID_ARG if argument is wrong
- Parameters
touch_num
: touch pad indexslope
: touch pad charge/discharge speedopt
: the initial voltage
-
esp_err_t
touch_pad_get_cnt_mode
(touch_pad_t touch_num, touch_cnt_slope_t *slope, touch_tie_opt_t *opt)¶ Get touch sensor charge/discharge speed for each pad.
- Return
ESP_OK on success
ESP_ERR_INVALID_ARG if argument is wrong
- Parameters
touch_num
: touch pad indexslope
: pointer to accept touch pad charge/discharge slopeopt
: pointer to accept the initial voltage
-
esp_err_t
touch_pad_isr_deregister
(void (*fn)(void *), void *arg, )¶ Deregister the handler previously registered using touch_pad_isr_handler_register.
- Return
ESP_OK on success
ESP_ERR_INVALID_STATE if a handler matching both fn and arg isn’t registered
- Parameters
fn
: handler function to call (as passed to touch_pad_isr_handler_register)arg
: argument of the handler (as passed to touch_pad_isr_handler_register)
-
esp_err_t
touch_pad_get_wakeup_status
(touch_pad_t *pad_num)¶ Get the touch pad which caused wakeup from deep sleep.
- Return
ESP_OK Success
ESP_ERR_INVALID_ARG parameter is NULL
- Parameters
pad_num
: pointer to touch pad which caused wakeup
-
esp_err_t
touch_pad_set_fsm_mode
(touch_fsm_mode_t mode)¶ Set touch sensor FSM mode, the test action can be triggered by the timer, as well as by the software.
- Return
ESP_OK on success
ESP_ERR_INVALID_ARG if argument is wrong
- Parameters
mode
: FSM mode
-
esp_err_t
touch_pad_get_fsm_mode
(touch_fsm_mode_t *mode)¶ Get touch sensor FSM mode.
- Return
ESP_OK on success
- Parameters
mode
: pointer to accept FSM mode
-
esp_err_t
touch_pad_clear_status
(void)¶ To clear the touch sensor channel active status.
- Note
The FSM automatically updates the touch sensor status. It is generally not necessary to call this API to clear the status.
- Return
ESP_OK on success
-
uint32_t
touch_pad_get_status
(void)¶ Get the touch sensor channel active status mask. The bit position represents the channel number. The 0/1 status of the bit represents the trigger status.
- Return
The touch sensor status. e.g. Touch1 trigger status is
status_mask & (BIT1)
.
-
bool
touch_pad_meas_is_done
(void)¶ Check touch sensor measurement status.
- Return
True measurement is under way
False measurement done
GPIO Lookup Macros¶
Some useful macros can be used to specified the GPIO number of a touch pad channel, or vice versa. e.g.
TOUCH_PAD_NUM5_GPIO_NUM
is the GPIO number of channel 5 (12);TOUCH_PAD_GPIO4_CHANNEL
is the channel number of GPIO 4 (channel 0).
Macros¶
-
TOUCH_PAD_GPIO1_CHANNEL
¶
-
TOUCH_PAD_NUM1_GPIO_NUM
¶
-
TOUCH_PAD_GPIO2_CHANNEL
¶
-
TOUCH_PAD_NUM2_GPIO_NUM
¶
-
TOUCH_PAD_GPIO3_CHANNEL
¶
-
TOUCH_PAD_NUM3_GPIO_NUM
¶
-
TOUCH_PAD_GPIO4_CHANNEL
¶
-
TOUCH_PAD_NUM4_GPIO_NUM
¶
-
TOUCH_PAD_GPIO5_CHANNEL
¶
-
TOUCH_PAD_NUM5_GPIO_NUM
¶
-
TOUCH_PAD_GPIO6_CHANNEL
¶
-
TOUCH_PAD_NUM6_GPIO_NUM
¶
-
TOUCH_PAD_GPIO7_CHANNEL
¶
-
TOUCH_PAD_NUM7_GPIO_NUM
¶
-
TOUCH_PAD_GPIO8_CHANNEL
¶
-
TOUCH_PAD_NUM8_GPIO_NUM
¶
-
TOUCH_PAD_GPIO9_CHANNEL
¶
-
TOUCH_PAD_NUM9_GPIO_NUM
¶
-
TOUCH_PAD_GPIO10_CHANNEL
¶
-
TOUCH_PAD_NUM10_GPIO_NUM
¶
-
TOUCH_PAD_GPIO11_CHANNEL
¶
-
TOUCH_PAD_NUM11_GPIO_NUM
¶
-
TOUCH_PAD_GPIO12_CHANNEL
¶
-
TOUCH_PAD_NUM12_GPIO_NUM
¶
-
TOUCH_PAD_GPIO13_CHANNEL
¶
-
TOUCH_PAD_NUM13_GPIO_NUM
¶
-
TOUCH_PAD_GPIO14_CHANNEL
¶
-
TOUCH_PAD_NUM14_GPIO_NUM
¶
Header File¶
Structures¶
-
struct
touch_pad_denoise
¶ Touch sensor denoise configuration
Public Members
-
touch_pad_denoise_grade_t
grade
¶ Select denoise range of denoise channel. Determined by measuring the noise amplitude of the denoise channel.
-
touch_pad_denoise_cap_t
cap_level
¶ Select internal reference capacitance of denoise channel. Ensure that the denoise readings are closest to the readings of the channel being measured. Use
touch_pad_denoise_read_data
to get the reading of denoise channel. The equivalent capacitance of the shielded channel can be calculated from the reading of denoise channel.
-
touch_pad_denoise_grade_t
-
struct
touch_pad_waterproof
¶ Touch sensor waterproof configuration
Public Members
-
touch_pad_t
guard_ring_pad
¶ Waterproof. Select touch channel use for guard pad. Guard pad is used to detect the large area of water covering the touch panel.
-
touch_pad_shield_driver_t
shield_driver
¶ Waterproof. Shield channel drive capability configuration. Shield pad is used to shield the influence of water droplets covering the touch panel. When the waterproof function is enabled, Touch14 is set as shield channel by default. The larger the parasitic capacitance on the shielding channel, the higher the drive capability needs to be set. The equivalent capacitance of the shield channel can be estimated through the reading value of the denoise channel(Touch0).
-
touch_pad_t
-
struct
touch_filter_config
¶ Touch sensor filter configuration
Public Members
-
touch_filter_mode_t
mode
¶ Set filter mode. The input of the filter is the raw value of touch reading, and the output of the filter is involved in the judgment of the touch state.
-
uint32_t
debounce_cnt
¶ Set debounce count, such as
n
. If the measured values continue to exceed the threshold forn+1
times, the touch sensor state changes. Range: 0 ~ 7
-
uint32_t
noise_thr
¶ Noise threshold coefficient. Higher = More noise resistance. The actual noise should be less than (noise coefficient * touch threshold). Range: 0 ~ 3. The coefficient is 0: 4/8; 1: 3/8; 2: 2/8; 3: 1;
-
uint32_t
jitter_step
¶ Set jitter filter step size. Range: 0 ~ 15
-
touch_smooth_mode_t
smh_lvl
¶ Level of filter applied on the original data against large noise interference.
-
touch_filter_mode_t
-
struct
touch_pad_sleep_channel_t
¶ Touch sensor channel sleep configuration
Public Members
-
touch_pad_t
touch_num
¶ Set touch channel number for sleep pad. Only one touch sensor channel is supported in deep sleep mode. If clear the sleep channel, point this pad to
TOUCH_PAD_NUM0
-
bool
en_proximity
¶ enable proximity function for sleep pad
-
touch_pad_t
Macros¶
-
TOUCH_PAD_SLOPE_DEFAULT
¶
-
TOUCH_PAD_TIE_OPT_DEFAULT
¶
-
TOUCH_PAD_BIT_MASK_MAX
¶
-
TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD
¶
-
TOUCH_PAD_LOW_VOLTAGE_THRESHOLD
¶
-
TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD
¶
-
TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT
¶
-
TOUCH_PAD_THRESHOLD_MAX
¶ If set touch threshold max value, The touch sensor can’t be in touched status
-
TOUCH_PAD_SLEEP_CYCLE_DEFAULT
¶ Excessive total time will slow down the touch response. Too small measurement time will not be sampled enough, resulting in inaccurate measurements.
- Note
The greater the duty cycle of the measurement time, the more system power is consumed.The number of sleep cycle in each measure process of touch channels. The timer frequency is RTC_SLOW_CLK (can be 150k or 32k depending on the options). Range: 0 ~ 0xffff
-
TOUCH_PAD_MEASURE_CYCLE_DEFAULT
¶ The times of charge and discharge in each measure process of touch channels. The timer frequency is 8Mhz. Recommended typical value: Modify this value to make the measurement time around 1ms. Range: 0 ~ 0xffff
-
TOUCH_PAD_INTR_MASK_ALL
¶ All touch interrupt type enable.
-
TOUCH_PROXIMITY_MEAS_NUM_MAX
¶ Touch sensor proximity detection configuration
-
TOUCH_DEBOUNCE_CNT_MAX
¶
-
TOUCH_NOISE_THR_MAX
¶
-
TOUCH_JITTER_STEP_MAX
¶
Type Definitions¶
-
typedef struct touch_pad_denoise
touch_pad_denoise_t
¶ Touch sensor denoise configuration
-
typedef struct touch_pad_waterproof
touch_pad_waterproof_t
¶ Touch sensor waterproof configuration
-
typedef struct touch_filter_config
touch_filter_config_t
¶ Touch sensor filter configuration
Enumerations¶
-
enum
touch_pad_t
¶ Touch pad channel
Values:
-
TOUCH_PAD_NUM0
= 0¶ Touch pad channel 0 is GPIO4(ESP32)
-
TOUCH_PAD_NUM1
¶ Touch pad channel 1 is GPIO0(ESP32) / GPIO1(ESP32-S2)
-
TOUCH_PAD_NUM2
¶ Touch pad channel 2 is GPIO2(ESP32) / GPIO2(ESP32-S2)
-
TOUCH_PAD_NUM3
¶ Touch pad channel 3 is GPIO15(ESP32) / GPIO3(ESP32-S2)
-
TOUCH_PAD_NUM4
¶ Touch pad channel 4 is GPIO13(ESP32) / GPIO4(ESP32-S2)
-
TOUCH_PAD_NUM5
¶ Touch pad channel 5 is GPIO12(ESP32) / GPIO5(ESP32-S2)
-
TOUCH_PAD_NUM6
¶ Touch pad channel 6 is GPIO14(ESP32) / GPIO6(ESP32-S2)
-
TOUCH_PAD_NUM7
¶ Touch pad channel 7 is GPIO27(ESP32) / GPIO7(ESP32-S2)
-
TOUCH_PAD_NUM8
¶ Touch pad channel 8 is GPIO33(ESP32) / GPIO8(ESP32-S2)
-
TOUCH_PAD_NUM9
¶ Touch pad channel 9 is GPIO32(ESP32) / GPIO9(ESP32-S2)
-
TOUCH_PAD_NUM10
¶ Touch channel 10 is GPIO10(ESP32-S2)
-
TOUCH_PAD_NUM11
¶ Touch channel 11 is GPIO11(ESP32-S2)
-
TOUCH_PAD_NUM12
¶ Touch channel 12 is GPIO12(ESP32-S2)
-
TOUCH_PAD_NUM13
¶ Touch channel 13 is GPIO13(ESP32-S2)
-
TOUCH_PAD_NUM14
¶ Touch channel 14 is GPIO14(ESP32-S2)
-
TOUCH_PAD_MAX
¶
-
-
enum
touch_high_volt_t
¶ Touch sensor high reference voltage
Values:
-
TOUCH_HVOLT_KEEP
= -1¶ Touch sensor high reference voltage, no change
-
TOUCH_HVOLT_2V4
= 0¶ Touch sensor high reference voltage, 2.4V
-
TOUCH_HVOLT_2V5
¶ Touch sensor high reference voltage, 2.5V
-
TOUCH_HVOLT_2V6
¶ Touch sensor high reference voltage, 2.6V
-
TOUCH_HVOLT_2V7
¶ Touch sensor high reference voltage, 2.7V
-
TOUCH_HVOLT_MAX
¶
-
-
enum
touch_low_volt_t
¶ Touch sensor low reference voltage
Values:
-
TOUCH_LVOLT_KEEP
= -1¶ Touch sensor low reference voltage, no change
-
TOUCH_LVOLT_0V5
= 0¶ Touch sensor low reference voltage, 0.5V
-
TOUCH_LVOLT_0V6
¶ Touch sensor low reference voltage, 0.6V
-
TOUCH_LVOLT_0V7
¶ Touch sensor low reference voltage, 0.7V
-
TOUCH_LVOLT_0V8
¶ Touch sensor low reference voltage, 0.8V
-
TOUCH_LVOLT_MAX
¶
-
-
enum
touch_volt_atten_t
¶ Touch sensor high reference voltage attenuation
Values:
-
TOUCH_HVOLT_ATTEN_KEEP
= -1¶ Touch sensor high reference voltage attenuation, no change
-
TOUCH_HVOLT_ATTEN_1V5
= 0¶ Touch sensor high reference voltage attenuation, 1.5V attenuation
-
TOUCH_HVOLT_ATTEN_1V
¶ Touch sensor high reference voltage attenuation, 1.0V attenuation
-
TOUCH_HVOLT_ATTEN_0V5
¶ Touch sensor high reference voltage attenuation, 0.5V attenuation
-
TOUCH_HVOLT_ATTEN_0V
¶ Touch sensor high reference voltage attenuation, 0V attenuation
-
TOUCH_HVOLT_ATTEN_MAX
¶
-
-
enum
touch_cnt_slope_t
¶ Touch sensor charge/discharge speed
Values:
-
TOUCH_PAD_SLOPE_0
= 0¶ Touch sensor charge / discharge speed, always zero
-
TOUCH_PAD_SLOPE_1
= 1¶ Touch sensor charge / discharge speed, slowest
-
TOUCH_PAD_SLOPE_2
= 2¶ Touch sensor charge / discharge speed
-
TOUCH_PAD_SLOPE_3
= 3¶ Touch sensor charge / discharge speed
-
TOUCH_PAD_SLOPE_4
= 4¶ Touch sensor charge / discharge speed
-
TOUCH_PAD_SLOPE_5
= 5¶ Touch sensor charge / discharge speed
-
TOUCH_PAD_SLOPE_6
= 6¶ Touch sensor charge / discharge speed
-
TOUCH_PAD_SLOPE_7
= 7¶ Touch sensor charge / discharge speed, fast
-
TOUCH_PAD_SLOPE_MAX
¶
-
-
enum
touch_tie_opt_t
¶ Touch sensor initial charge level
Values:
-
TOUCH_PAD_TIE_OPT_LOW
= 0¶ Initial level of charging voltage, low level
-
TOUCH_PAD_TIE_OPT_HIGH
= 1¶ Initial level of charging voltage, high level
-
TOUCH_PAD_TIE_OPT_MAX
¶
-
-
enum
touch_fsm_mode_t
¶ Touch sensor FSM mode
Values:
-
TOUCH_FSM_MODE_TIMER
= 0¶ To start touch FSM by timer
-
TOUCH_FSM_MODE_SW
¶ To start touch FSM by software trigger
-
TOUCH_FSM_MODE_MAX
¶
-
-
enum
touch_trigger_mode_t
¶ Values:
-
TOUCH_TRIGGER_BELOW
= 0¶ Touch interrupt will happen if counter value is less than threshold.
-
TOUCH_TRIGGER_ABOVE
= 1¶ Touch interrupt will happen if counter value is larger than threshold.
-
TOUCH_TRIGGER_MAX
¶
-
-
enum
touch_trigger_src_t
¶ Values:
-
TOUCH_TRIGGER_SOURCE_BOTH
= 0¶ wakeup interrupt is generated if both SET1 and SET2 are “touched”
-
TOUCH_TRIGGER_SOURCE_SET1
= 1¶ wakeup interrupt is generated if SET1 is “touched”
-
TOUCH_TRIGGER_SOURCE_MAX
¶
-
-
enum
touch_pad_intr_mask_t
¶ Values:
-
TOUCH_PAD_INTR_MASK_DONE
= BIT(0)¶ Measurement done for one of the enabled channels.
-
TOUCH_PAD_INTR_MASK_ACTIVE
= BIT(1)¶ Active for one of the enabled channels.
-
TOUCH_PAD_INTR_MASK_INACTIVE
= BIT(2)¶ Inactive for one of the enabled channels.
-
TOUCH_PAD_INTR_MASK_SCAN_DONE
= BIT(3)¶ Measurement done for all the enabled channels.
-
TOUCH_PAD_INTR_MASK_TIMEOUT
= BIT(4)¶ Timeout for one of the enabled channels.
-
-
enum
touch_pad_denoise_grade_t
¶ Values:
-
TOUCH_PAD_DENOISE_BIT12
= 0¶ Denoise range is 12bit
-
TOUCH_PAD_DENOISE_BIT10
= 1¶ Denoise range is 10bit
-
TOUCH_PAD_DENOISE_BIT8
= 2¶ Denoise range is 8bit
-
TOUCH_PAD_DENOISE_BIT4
= 3¶ Denoise range is 4bit
-
TOUCH_PAD_DENOISE_MAX
¶
-
-
enum
touch_pad_denoise_cap_t
¶ Values:
-
TOUCH_PAD_DENOISE_CAP_L0
= 0¶ Denoise channel internal reference capacitance is 5pf
-
TOUCH_PAD_DENOISE_CAP_L1
= 1¶ Denoise channel internal reference capacitance is 6.4pf
-
TOUCH_PAD_DENOISE_CAP_L2
= 2¶ Denoise channel internal reference capacitance is 7.8pf
-
TOUCH_PAD_DENOISE_CAP_L3
= 3¶ Denoise channel internal reference capacitance is 9.2pf
-
TOUCH_PAD_DENOISE_CAP_L4
= 4¶ Denoise channel internal reference capacitance is 10.6pf
-
TOUCH_PAD_DENOISE_CAP_L5
= 5¶ Denoise channel internal reference capacitance is 12.0pf
-
TOUCH_PAD_DENOISE_CAP_L6
= 6¶ Denoise channel internal reference capacitance is 13.4pf
-
TOUCH_PAD_DENOISE_CAP_L7
= 7¶ Denoise channel internal reference capacitance is 14.8pf
-
TOUCH_PAD_DENOISE_CAP_MAX
= 8¶
-
-
enum
touch_pad_shield_driver_t
¶ Touch sensor shield channel drive capability level
Values:
-
TOUCH_PAD_SHIELD_DRV_L0
= 0¶ The max equivalent capacitance in shield channel is 40pf
-
TOUCH_PAD_SHIELD_DRV_L1
¶ The max equivalent capacitance in shield channel is 80pf
-
TOUCH_PAD_SHIELD_DRV_L2
¶ The max equivalent capacitance in shield channel is 120pf
-
TOUCH_PAD_SHIELD_DRV_L3
¶ The max equivalent capacitance in shield channel is 160pf
-
TOUCH_PAD_SHIELD_DRV_L4
¶ The max equivalent capacitance in shield channel is 200pf
-
TOUCH_PAD_SHIELD_DRV_L5
¶ The max equivalent capacitance in shield channel is 240pf
-
TOUCH_PAD_SHIELD_DRV_L6
¶ The max equivalent capacitance in shield channel is 280pf
-
TOUCH_PAD_SHIELD_DRV_L7
¶ The max equivalent capacitance in shield channel is 320pf
-
TOUCH_PAD_SHIELD_DRV_MAX
¶
-
-
enum
touch_pad_conn_type_t
¶ Touch channel idle state configuration
Values:
-
TOUCH_PAD_CONN_HIGHZ
= 0¶ Idle status of touch channel is high resistance state
-
TOUCH_PAD_CONN_GND
= 1¶ Idle status of touch channel is ground connection
-
TOUCH_PAD_CONN_MAX
¶
-
-
enum
touch_filter_mode_t
¶ Touch channel IIR filter coefficient configuration.
- Note
On ESP32S2. There is an error in the IIR calculation. The magnitude of the error is twice the filter coefficient. So please select a smaller filter coefficient on the basis of meeting the filtering requirements. Recommended filter coefficient selection
IIR_16
.
Values:
-
TOUCH_PAD_FILTER_IIR_4
= 0¶ The filter mode is first-order IIR filter. The coefficient is 4.
-
TOUCH_PAD_FILTER_IIR_8
¶ The filter mode is first-order IIR filter. The coefficient is 8.
-
TOUCH_PAD_FILTER_IIR_16
¶ The filter mode is first-order IIR filter. The coefficient is 16 (Typical value).
-
TOUCH_PAD_FILTER_IIR_32
¶ The filter mode is first-order IIR filter. The coefficient is 32.
-
TOUCH_PAD_FILTER_IIR_64
¶ The filter mode is first-order IIR filter. The coefficient is 64.
-
TOUCH_PAD_FILTER_IIR_128
¶ The filter mode is first-order IIR filter. The coefficient is 128.
-
TOUCH_PAD_FILTER_IIR_256
¶ The filter mode is first-order IIR filter. The coefficient is 256.
-
TOUCH_PAD_FILTER_JITTER
¶ The filter mode is jitter filter
-
TOUCH_PAD_FILTER_MAX
¶
-
enum
touch_smooth_mode_t
¶ Level of filter applied on the original data against large noise interference.
- Note
On ESP32S2. There is an error in the IIR calculation. The magnitude of the error is twice the filter coefficient. So please select a smaller filter coefficient on the basis of meeting the filtering requirements. Recommended filter coefficient selection
IIR_2
.
Values:
-
TOUCH_PAD_SMOOTH_OFF
= 0¶ No filtering of raw data.
-
TOUCH_PAD_SMOOTH_IIR_2
= 1¶ Filter the raw data. The coefficient is 2 (Typical value).
-
TOUCH_PAD_SMOOTH_IIR_4
= 2¶ Filter the raw data. The coefficient is 4.
-
TOUCH_PAD_SMOOTH_IIR_8
= 3¶ Filter the raw data. The coefficient is 8.
-
TOUCH_PAD_SMOOTH_MAX
¶