How to Interface TTP223B Touch Sensor with Arduino
Tags: arduino, diy, programming, coding
Published: 2024-05-15 11:23:33

The capacitive TTP223B Touch Sensor is an electronic component used to detect touch or proximity. It operates based on changes in capacitance when a conductive object, like a finger, comes into contact with or near the sensor. 
The TTP223B sensor is a popular choice due to its low power consumption, simple interface, and reliability. It can be easily interfaced with Arduino. 

In this project, we will learn how to interface the TTP223B capacitive touch sensor switch with an Arduino. 


## What is TTP223B Capacitive Touch Sensor? 

The TTP223B is1-channel capacitive touch sensor module that detects touch input through a capacitive touch pad. It operates on low power consumption and is sensitive to even slight touch. The module includes a built-in power-on reset circuit, eliminating the need for an external power-on reset. 
It also features a toggle mode, where touching the pad toggles the output between high and low states, and a momentary mode, where the output remains high only as long as the pad is touched. With its simple interface and reliable performance, the TTP223B is commonly used in various electronic applications such as touch-sensitive controls for lamps, appliances, and interactive displays.


## Working of TTP223B Capacitive Touch Sensor

The TTP223B capacitive touch sensor operates based on the principle of capacitance change upon touch. When no touch is detected, the sensor's internal circuitry maintains a stable state. However, when a conductive object, such as a finger, comes into close proximity to the touch pad, it creates a change in capacitance. This change is detected by the sensor's circuitry, causing it to trigger an output signal. The output signal can be configured to behave in different ways depending on the mode selected, such as toggling between high and low states upon touch or remaining high only as long as the touch is present. 

## TTP223B Touch Sensor Pinout

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgyqh3gla8q19wcsif03.JPG)

Here's the pinout of the TTP223B touch sensor:
**VCC:** This pin is the power supply input for the sensor. It is typically connected to a voltage source such as 3.3V or 5V.

**Signal:** This pin is the digital output signal of the sensor.

**GND:** This pin is the ground connection for the sensor. It is connected to the ground of the circuit.


## Interfacing TTP223B Touch Sensor with Arduino

**Hardware Requirements**

- 1 Arduino UNO 	
- 1 Touch Sensor		
- 1 LED		
- 1 100 ohms Resistance			
- 1 Small size Breadboard			
- 5 Connection wires
		


**Software**
Arduino IDE, Version 2.1.1 or above installed on your PC


**Circuit Diagram**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/977m1bah4l9gnz50yve8.JPG)


## Code

```
/*
Interfacing TTP223B Touch Sensor with Arduino
by www.PlaywithCircuit.com
*/
// Define pin numbers
const int touchPin = 2;  // Define the pin number for touch sensor
const int ledPin = 4;    // Define the pin number for LED

void setup() {
 // Initialize serial communication at 9600 baud
 Serial.begin(9600);

 // Set pin modes
 pinMode(touchPin, INPUT);  // Set touchPin as input
 pinMode(ledPin, OUTPUT);   // Set ledPin as output
}

void loop() {
 // Read the state of the touch sensor
 int touchState = digitalRead(touchPin);

 // Check if touch is detected
 if (touchState == HIGH) {
   // Turn on LED
   digitalWrite(ledPin, HIGH);

   // Print touch detected message
   Serial.println("Touch detected!");
   // Staty in the below loop as long as touch is detected.
   while (digitalRead(touchPin) == HIGH);
 } else {
   // Turn off LED
   digitalWrite(ledPin, LOW);
 }
}
```


## Code Explanation

Here, two integer variables `touchPin` and `ledPin` are declared and assigned the pin numbers to which the touch sensor and LED are connected, respectively.


```
// Define pin numbers
const int touchPin = 2;  // Define the pin number for touch sensor
const int ledPin = 4;    // Define the pin number for LED
```
The `setup()` function is used to initialize the serial communication at a baud rate of 9600 and set the pin modes for `touchPin` and `ledPin`. `touchPin` is configured as an input pin to read the state of the touch sensor, while `ledPin` is configured as an output pin to control the LED.

```
void setup() {
  // Initialize serial communication at 9600 baud
  Serial.begin(9600);
  // Set pin modes
  pinMode(touchPin, INPUT);  // Set touchPin as input
  pinMode(ledPin, OUTPUT);   // Set ledPin as output
}
```
The `loop()` function continuously reads the state of the touch sensor using digitalRead(touchPin). If touch is detected (i.e., the touchState is HIGH), the LED connected to `ledPin` is turned on by setting it to HIGH using `digitalWrite(ledPin, HIGH)`, and a message "Touch detected!" is printed to the serial monitor. Then, it enters a loop that continuously checks if the touch sensor is still being touched. Once the touch is released, the loop exits, and the LED is turned off. If no touch is detected, the LED remains off.

```
void loop() {
  // Read the state of the touch sensor
  int touchState = digitalRead(touchPin);
  // Check if touch is detected
  if (touchState == HIGH) {
    // Turn on LED
    digitalWrite(ledPin, HIGH);
    // Print touch detected message
    Serial.println("Touch detected!");
    // Stay in the below loop as long as touch is detected.
    while (digitalRead(touchPin) == HIGH);
  } else {
    // Turn off LED
    digitalWrite(ledPin, LOW);
  }
}
```

## Output
{% embed https://youtu.be/zAkQ8X_ImGU %}

For more indepth guide checkout **[How to Interface TTP223B Touch Sensor with Arduino](https://playwithcircuit.com/how-to-interface-ttp223b-touch-sensor-with-arduino/)**