TTP223B Capacitive Touch Sensor Review and Code
The TTP223B Capacitive touch sensor is a button type sensor that allows you to detect when it is being pressed. In this post I review the sensor and provide code and a wiring diagram to use such a sensor.
What can a Capacitive Touch Sensor do?
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. Classically buttons will be rated to have a specific actuation force (the amount of force required 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 SIG
output. 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 any situations.
Wiring it up to an Arduino
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.
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 to read the sensor
Again since this sensor is a basic button it only requires a simple Arduino sketch to work with it.
#define TOUCH_PIN 7
int touchVal = 0;
void setup() {
Serial.begin(115200);
pinMode(TOUCH_PIN, INPUT);
}
void loop() {
touchVal = digitalRead(TOUCH_PIN);
Serial.println((touchVal == HIGH ? "DETECTED!" : "Not detected"));
delay(200);
}
Initially, I define the pin that I want to read the sensor from as a #define
to save using a variable to store this constant. The pin used to read the data has been configured as an input pin.
In the loop I perform a digitalRead
to see whether someone has touched the sensor. If the sensor value is high I print out “DETECTED!”. Instead of just printing out the message detected you could turn on a LED or similar using the below code
if(touchVal == HIGH) {
//do something like turn on a LED
digitalWrite(13, HIGH);
}
Depending on your situation a ternary if statement (such as the one in the first code section) may be shorter and cleaner to use. However the basic if statement (second code segment) is always more versatile.
Testing the sensor
During testing I have found that 0.2 second delay between checking should be fast enough to detect a quick tap of the device.
Depending on your use can you will want to test how fast you will want to check the sensor for presence of something pressing it. The above code can easily be modified to change how often the Arduino checks the sensor. If you are also checking other sensors you may want to increase the delay in checking this sensor. However this will mean that to trigger the sensor you will need to press it for longer.
If you only want to detect a long press you could have your delay up to a second or two. This would mean the user would need to place their finger on the device for a couple seconds until it has been detected.
Final Review
As noted above this sensor works well for circuits where a conventional button would not be appropriate. Specifically, in fragile enclosures where physically pressing the button may cause issues over time.
This has no disadvantages that a standard button would not have. The noted issues on choosing the delay between which the Arduino will check to see if it pressed are identical to a standard button.
Overall I would recommend this over a standard button as it allows presses without any force. You are able to just place your finger on the button to trigger it.
Overall I would definitely recommend this sensor to use it in your project.