Learn to code

ARDUINO & CODES

Previous Post
Detecting light with an LDR Sensor
Next Post
How to use a Magnetic Contact Switch with Arduino

Detecting touch with Capacitive Touch Sensor (TTP223B)

in Arduino Sensors

Introduction

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 these 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.

Hardware required:

  • Arduino Uno
  • Breadboard
  • TTP223B Toch sensor
  • hook-up wires
  • LED
  • 220-ohm resistor

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 write 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 

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

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);
  pinMode(ledPin, OUTPUT);  // set the Led Pin to output
  pinMode(touchPin, INPUT); // set the Touch Pin to 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 (). 

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 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu