Public post

8.3 Detecting touch with Capacitive Touch Sensor (TTP223B)

· 8. Sensors

The capacitive touch sensor (TTP223B) is a touchpad detector which offers 1 touch key. The touching detection is designed for replacing traditional direct button key.  The touch sensor can be used for various projects.

The sensor is essentially a button-style sensor that detects when it is being pressed. One of the main differences is that it detects the presence of your finger on the sensor. This means that it doesn’t require any force to activate the button.

Depending on your setup using a capacitive touch sensor may aid your design as there is no actuation force needed. This can be useful in situations where pressing a button may stress the circuit board.

This sensor has three standard pins, VCC, GND, and SIG. Like a standard button, when it is pressed it will output a high signal on its output (SIG). The VCC pin will accept voltages from 2 to 5.5 Volts. This level is within most standard projects and developer boards so should be useful in many situations. Since the VCC pin accepts this range of values, we do not need any resistor between the Arduino Board and the touch sensor. Remember, between the LED and the Arduino Board is you definitely need a resistor.

Material that you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 5x
Breadboard 400 points 1x
220Ω resistor 1x
LED 1x
TTP223B Toch sensor 1x

Circuit Diagram

To test the touch sensor I wired it up to an Arduino Uno.

The sensor is relatively easy to configure as it only requires a signal pin and VCC and GND.

The signal pin (SIG) must be wired up to one of the Arduino’s digital pins which should be configured as an input pin. We are going to connect the ground pin (GND) to one of the grounds of the Arduino Board. The last connection is between the VCC of the touch sensor and the 5v of the Board. In

Since the sensor will support between 2.2 and 5 volts you are free to wire it up to either the 3.3V or 5V on the Arduino. Here I have wired it up to the 5V line.

Code Example 1

// When Sig Output is high, touch sensor is being pressed

int touchPin = 2; // pin for the touch sensor
int ledPin = 13; // pin for the LED

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // set the Led Pin to output
  pinMode(touchPin, INPUT); // set the Touch Pin to input
}

void loop() {
  int touchValue = digitalRead(touchPin); // read the Touch Pin
  if (touchValue == HIGH) { //check if the sensor is touched
    digitalWrite(ledPin, HIGH);
    Serial.println("TOUCHED");
  }
  else {
    digitalWrite(ledPin, LOW);
    Serial.println("not touched");
  }
  delay(100);

}

Code explanation 1

In the above example, we are using the touch sensor for detecting if the sensor is touched at the moment.

Firstly, we need to define the variables that we will use in our sketch. We need to have a variable for the LED and one for the Touch Sensor

// pin for the touch sensor 
int touchPin = 2;
// pin for the LED
int ledPin = 13; 

Secondly, we will set ledPin as an output pin and the touchPin as an input pin. In de setup() part of this sketch, we will also enable communication between the Arduino Uno and the Serial monitor on your computer.

Serial.begin(9600); 
// set the Led Pin to output 
pinMode(ledPin, OUTPUT);  
// set the Touch Pin to input
pinMode(touchPin, INPUT);

In our void loop () part, our loop starts to detect if the touch sensor is touched or not. We need to declare a variable to hold the state of the touchPin. Next, we need to decide what to to do with the state of the touch sensor. If the sensor is touched it will send a HIGH signal to the Arduino. So, we know that if the sensor is not touched the sensor will send a LOW signal. Depending if the sensor is touched or not we will send some text to the Serial Monitor by using Serial.println ().

// read the Touch Pin 
int touchValue = digitalRead(touchPin); 
//check if the sensor is touched
if (touchValue == HIGH) { 
digitalWrite(ledPin, HIGH); Serial.println("TOUCHED"); 
} 
else {
digitalWrite(ledPin, LOW); 
Serial.println("not touched"); 
} 
delay(100);

Code Example 2

int touchSensor = 2; // Pin for capactitive touch sensor
int ledPin = 13;
boolean currentState = LOW;
boolean lastState = LOW;
boolean ledState = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(touchSensor, INPUT);
  Serial.begin(9600);
}

void loop() {
  currentState = digitalRead(touchSensor);
  if (currentState == HIGH && lastState == LOW) {
    delay(5);
    if (ledState == HIGH) {
      digitalWrite(ledPin, LOW);
      ledState = LOW;
      Serial.println("Turn LED off");
    } else {
      digitalWrite(ledPin, HIGH);
      ledState = HIGH;
      Serial.println("Turn LED on");
    }
  }
  lastState = currentState;
}

Code explantion 2

In the second example, we declare both the touch sensor pin and the LED pin. However, we also need to know the state of the sensor and the LED and therefore we create three boolean variables. One for detecting the state of the LED, HIGH or LOW. The second and third state is for the touch sensor. We will use these states in the void loop () part of our sketch.

Firstly, we are going to check what the current state is of the touch sensor. Therefore, we need to read the state of the sensor. After that, we will use two if statements to control the LED.

The if statements will check whether the touch pin is touched and if the LED is on or off. It will check the condition and then decide what to do. If we start the sketch all states are low, meaning that the sensor is not touched and the LED is off. If we touch the sensor the current state will be HIGH and the last state will be LOW. The first condition is true, the second statement will be FALSE since the ledstate is LOW, meaning that the LED is off. We arrive at the else statement, this statement turns the LED on and sets the ledstate HIGH if the condition is met that the touch sensor is touched and the last state of the LED is lOW.

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.