How to use TTP223 Capacitive Touch sensor with Arduino.

TTP223 Touch sensor with Arduino

The TTP223 capacitive touch sensor offers one touch key functionality designed for replacing traditional mechanical buttons and switches with a touch pad. Touch sensors are used in a number of consumer products like mobile phones, remote controls and in various home appliances control panels.

In this tutorial I will look at the hardware overview of TTP223 touch sensor and how this sensor can be used with Arduino.

TTP223 Touch sensor pinout.

This sensor is based on the TTP223-BA6 IC which is a six pin SOP IC whose pinout is illustrated below.

TTP223-BA6 IC pinout
  • Q – this is the CMOS output pin
  • VSS – negative power supply or ground
  • I – for sensor input
  • AHLB – selection of output as either active high or low. This pin is LOW by default giving Active high and when HIGH it turns Active low.
  • VDD – is the positive power supply
  • TOG – determines the output type. By default, this pin is LOW and is in Direct mode. When turned HIGH, the output will be in Toggle mode.

Touch sensors are designed to function as switches when touched by a conducting object. The TTP223 capacitive touch sensor breakout board pinout is as illustrated below.

TTP223 touch sensor breakout board pinout

How does the TTP223 touch sensor work?

Since this is a capacitive sensor, it is made similar to a normal capacitor where there are two parallel plate conductors separated by an insulator. There is also a capacitance measurement circuit that continuously measures the capacitance.

If any conductive object for example a person’s finger touches the plate, there will be an increase in the capacitance and this change will be detected by the capacitance measuring circuit which will convert it into a signal.

Just like any capacitor, the sensitivity of a capacitive touch sensor is determined by the size of the parallel plates and the thickness of the insulator. One of the major challenges of using this sensor is that, because this sensor needs only contact with a conductor and does not necessarily require pressure or force to operate, it can be affected by false triggers like water droplets.

Adjusting Sensitivity of the TTP223 touch sensor.

Sensitivity can adjust by adding a capacitance, the value of capacitor ranges from 0 to 50 pF, where 0pF gives the full sensitivity and 50pF will gives the lowest sensitivity. For example, if you needed the touch switch to work through glass or acrylic you will be needed to adjust the sensitivity based on the thickness of the material.

TTP223 Touch sensor configurations.

From the pinout diagram of the TTP223-BA6 IC above and the touch sensor breakout board, the AHLB and TOG pins are attached to the two pairs of solder pads A and B on the breakout board. These solder pads by default come unsoldered therefore A and B are considered open.

You can change the mode of operation of the TTP223 touch sensor by soldering the solder pads as described below.

  • If both A and B are open, the default value of the output pin is LOW. When the sensor is touched, the output turns HIGH and stays HIGH until released.
  • If A is open and B is closed, the default value of the output pin is LOW. When the sensor is touched, the output turns HIGH and remains HIGH until the next touch.
  • If B is open and A is closed, the default value of the output pin is HIGH. When the sensor is touched, the output turns LOW and stays LOW until released.
  • If both B and A are closed, the default value of the output pin is HIGH. When the sensor is touched, the output turns LOW and remains LOW until the next touch.

The above configuration modes are important when you want to use the TTP223 touch sensor as a switch without using a microcontroller for example in the simple setup below where an LED is attached directly to the sensor.

TTP223 touch sensor with LED

The LED and will turn on or off when the sensor is pressed depending on the configuration of the touch sensor determined by how pads A and B are soldered. For example, if both A and B are open, when the sensor is touched, the LED will turn ON and when released it turns OFF.

Connecting TTP223 Touch sensor to Arduino.

You can also use the TTP223 touch sensor with microcontrollers like Arduino and the connection is simple.

  • GND pin of the touch sensor is connected to Arduino GND.
  • VCC is connected to 5V pin of the Arduino.
  • I/O pin is connected to a digital pin of Arduino.

A device to be controlled by the touch sensor is attached to any other digital pin of the Arduino. In my case I’ll attach an LED to pin 5.

Connecting TTP223 touch sensor with Arduino

Code for TTP223 touch sensor with Arduino.

Since the TTP233 touch sensor is actually a switch, the code is simply the one for turning a switch on and off in order to control a given device. For example, in this case I want to control the lighting of an LED using the touch sensor.

const int SENSOR_PIN = 5; 
const int ledPin = 8

int lastState = LOW;      // previous state of input pin
int currentState;         // current reading of input pin
int ledState = LOW;      // current LED state

void setup() {
  pinMode(SENSOR_PIN, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  currentState = digitalRead(SENSOR_PIN);

  if(lastState == LOW && currentState == HIGH){
    if(ledState == LOW)
      ledState = HIGH;
    else if(ledState == HIGH)
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
  lastState = currentState;
}

Code Description.

This code is for toggling the lighting of LED whenever the sensor is touched.

First you have to declare the pins where the sensor and LED are connected to Arduino which are Pins 5 and 8. Also the previous and current states of the sensor readings and the current state of the LED are declared as LOW.

In the setup() section we initialize the sensor pin as INPUT and LED pin as OUTPUT.

The void() section controls the toggling of the LED by first checking the current state of the sensor. Then with the help of an if statement we compare the current and previous states of the sensor readings with the state of the LED.

When the sensor is touched the LED will light and when touched again it turns off.

Using TTP223 Touch sensor with Arduino and Relay module.

In case you need to use the touch sensor with Arduino to control high voltage devices like bulbs in your home, then you need to connect a relay module as shown in the schematic below.

Connect the touch sensor to any other digital pin of Arduino for example I have used pin 2.

The signal pin of the relay module is connected to a digital pin of the Arduino, in my case I have used pin 8.

VCC and GND of the relay module is connected to the 5V and GND of the Arduino respectively.

Then cut one of the wires from the light bulb and connect one end to the common terminal and the other end to the normally open terminal of the relay module.

Connecting TTP223 touch sensor with Arduino and 5V relay module

Code for TTP223 Touch sensor with Arduino and Relay module

The code is almost similar to the one given above for controlling LEDs only that this time we include a relay to one of the digital pins of Arduino.

#define TouchSensor 2      // Pin for capacitive touch sensor
int relay = 8;              //relay signal pin
boolean currentState = LOW;         //default state low
boolean lastState = LOW;              //default state low
boolean RelayState = LOW;           //default state low
 
void setup() {
  pinMode(relay, OUTPUT);  
  pinMode(TouchSensor, INPUT);
}
 
void loop() {
  currentState = digitalRead(TouchSensor);
    if (currentState == HIGH && lastState == LOW)

{    
    delay(1);
   
    if (RelayState == HIGH){
      digitalWrite(relay, LOW);   
      RelayState = LOW;
    } else {
      digitalWrite(relay, HIGH);   
      RelayState = HIGH;
    }
  }
  lastState = currentState;
}

When the sensor is touched when the bulb is OFF, the bulb turns ON and when touched again the bulb turns OFF.