TTP223 Touch Switch Module Documentation
The TTP223 Touch Switch Module is a popular, low-cost, and easy-to-use capacitive touch sensor module designed for a variety of applications, including IoT projects, robotics, and interactive devices. This module allows users to create touch-based interfaces without the need for physical buttons or switches.
The TTP223 Touch Switch Module typically has the following pinout:
VCC: Power supply (3.3V or 5V)
GND: Ground
OUT: Digital output signal (HIGH or LOW)
Operating voltage: 3.3V to 5V
Operating current: 1mA to 10mA
Sensitivity: Adjustable via an onboard potentiometer
Output type: Digital (HIGH or LOW)
Response time: 60ms to 120ms
### Example 1: Basic Touch Detection with Arduino
This example demonstrates how to use the TTP223 Touch Switch Module to detect touches and toggle an LED on and off using an Arduino board.
```c++
const int ledPin = 13; // choose the pin for the LED
const int touchPin = 2; // choose the pin for the TTP223 module
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(touchPin, INPUT);
}
void loop() {
int touchState = digitalRead(touchPin);
if (touchState == HIGH) {
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // turn the LED off
}
delay(50);
}
```
### Example 2: Touch-Based Button Press with Raspberry Pi (Python)
This example shows how to use the TTP223 Touch Switch Module to detect touches and simulate a button press using a Raspberry Pi and Python.
```python
import RPi.GPIO as GPIO
import time
# set up GPIO mode
GPIO.setmode(GPIO.BCM)
# define the pin for the TTP223 module
touch_pin = 17
# set up the touch pin as an input
GPIO.setup(touch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
if GPIO.input(touch_pin) == GPIO.LOW:
print("Touch detected!")
# simulate a button press (e.g., send a signal to a microcontroller)
# ...
time.sleep(0.1)
```
### Example 3: Debounced Touch Detection with ESP32 (MicroPython)
This example demonstrates how to use the TTP223 Touch Switch Module to detect touches with debouncing using an ESP32 board and MicroPython.
```python
import machine
import utime
# define the pin for the TTP223 module
touch_pin = machine.Pin(32, machine.Pin.IN, machine.Pin.PULL_UP)
def debounce(pin):
last_state = machine.Pin.IN
current_state = machine.Pin.IN
while True:
current_state = pin.value()
if last_state != current_state:
last_state = current_state
utime.sleep_ms(50) # debounce time
if current_state == 0:
return True
return False
while True:
if debounce(touch_pin):
print("Touch detected!")
# take action based on the touch event
# ...
utime.sleep_ms(10)
```
These examples demonstrate the basic usage of the TTP223 Touch Switch Module and can be modified to fit various IoT applications.