TTP224 Touch Sensor
This module is a capacitive point-type touch switch module based on the touch detection IC (TTP223B). In normal state, the module outputs a low level and the mode is low-power mode; when a finger touches the corresponding position, the module outputs a high level and the mode switches to fast mode; when there is no touch for 12 seconds, the mode switches back to low-power mode. The module can be installed on the surface of non-metallic materials such as plastic and glass. You can also cover the surface of the module with a thin paper (non-metallic) until the touch position is correct, making it a button hidden in walls, desktops, etc. This module allows you to avoid the troubles of conventional push-type keys.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-24706531953.10.37476a4b0lAfi7&id=522552731734 Materials download link: https://pan.baidu.com/s/1lBksfqx_dT4uIyABkHVm3Q Materials extraction code: hj2n
Specifications
Operating voltage: 2.4-5.5V Operating current: 2.5uA~9uA Module size: 35x29 mm Fastest response time: 100Ms Control method: GPIO Number of pins: 6 Pin (2.54mm pitch pin header)
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting.
For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here.
However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_touchkey.c and bsp_touchkey.h, and change the folder name to TouchKey.
Write Code
Write the following in the bsp_touchkey.c file:
#include "bsp_touchkey.h"
#include "stdio.h"
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: TTP224_GPIO_Init
* Function Description: Initialize the capacitive touch chip TTP224
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void TTP224_GPIO_Init(void)
{
gpio_config_t KEY_config = {
.pin_bit_mask = (1ULL<<PIN_IN1)|(1ULL<<PIN_IN2)|(1ULL<<PIN_IN3)|(1ULL<<PIN_IN4), // Configure pins
.mode =GPIO_MODE_INPUT, // Input mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&KEY_config);
}
/******************************************************************
* Function Name: Key_IN1_Scanf
* Function Description: Return the pin level state of touch key 1
* Function Parameters: None
* Function Return: 0=no touch detected 1=touch detected
* Author: LC
* Notes: None
******************************************************************/
char Key_IN1_Scanf(void)
{
return KEY_IN1;
}
/******************************************************************
* Function Name: Key_IN2_Scanf
* Function Description: Return the pin level state of touch key 2
* Function Parameters: None
* Function Return: 0=no touch detected 1=touch detected
* Author: LC
* Notes: None
******************************************************************/
char Key_IN2_Scanf(void)
{
return KEY_IN2;
}
/******************************************************************
* Function Name: Key_IN3_Scanf
* Function Description: Return the pin level state of touch key 3
* Function Parameters: None
* Function Return: 0=no touch detected 1=touch detected
* Author: LC
* Notes: None
******************************************************************/
char Key_IN3_Scanf(void)
{
return KEY_IN3;
}
/******************************************************************
* Function Name: Key_IN4_Scanf
* Function Description: Return the pin level state of touch key 4
* Function Parameters: None
* Function Return: 0=no touch detected 1=touch detected
* Author: LC
* Notes: None
******************************************************************/
char Key_IN4_Scanf(void)
{
return KEY_IN4;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Write the following in the bsp_touchkey.h file:
#ifndef _BSP_TOUCHKEY_H_
#define _BSP_TOUCHKEY_H_
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gptimer.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "nvs_flash.h"
#define PIN_IN1 1
#define PIN_IN2 2
#define PIN_IN3 3
#define PIN_IN4 4
#define KEY_IN1 gpio_get_level(PIN_IN1)
#define KEY_IN2 gpio_get_level(PIN_IN2)
#define KEY_IN3 gpio_get_level(PIN_IN3)
#define KEY_IN4 gpio_get_level(PIN_IN4)
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void TTP224_GPIO_Init(void);// Pin initialization
char Key_IN1_Scanf(void);// Input state of touch key 1
char Key_IN2_Scanf(void);// Input state of touch key 2
char Key_IN3_Scanf(void);// Input state of touch key 3
char Key_IN4_Scanf(void);// Input state of touch key 4
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Porting Verification
Enter the following code in main.c:
#include <stdio.h>
#include "TouchKey\bsp_touchkey.h"
void app_main(void)
{
TTP224_GPIO_Init();
printf("TTP224 Start.....\n");
while(1)
{
printf("Key_IN1=%d\r\n",Key_IN1_Scanf() );
printf("Key_IN2=%d\r\n",Key_IN2_Scanf() );
printf("Key_IN3=%d\r\n",Key_IN3_Scanf() );
printf("Key_IN4=%d\r\n",Key_IN4_Scanf() );
delay_ms(300);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.