The TouchSensor library provides a simple and reliable way to interface with capacitive touch modules like the TTP223 using Arduino boards. With built-in debounce and edge detection, this library is ideal for touch-based input in interactive electronics, smart switches, and DIY projects.
- TTP223 capacitive touch sensor
- Any other capacitive touch sensor with a digital output pin
- Works with all Arduino-compatible boards (Uno, Nano, Mega, ESP32, etc.)
- 📲 Touch detection via simple
.isTouched()function - ⏱️ Built-in debounce support to avoid false multiple triggers
- 🔁 Edge detection:
.justTouched()— true only on the transition from not touched to touched.justReleased()— true only on the transition from touched to not touched
- 🧠 Clean abstraction over
digitalRead()with enhanced control - ⚡ Lightweight and fast: minimal memory and CPU usage
You can install this library by:
- Downloading the ZIP from GitHub and adding it via Arduino IDE > Sketch > Include Library > Add .ZIP Library
- Or placing it in your
Arduino/librariesfolder manually
| TTP223 Pin | Connect To |
|---|---|
| VCC | 3.3V or 5V (Arduino) |
| GND | GND (Arduino) |
| OUT | Digital Pin (e.g., D2) |
#include <TouchSensor.h>
TouchSensor touch(2); // TTP223 sensor connected to digital pin 2
void setup() {
Serial.begin(9600);
}
void loop() {
touch.update(); // Always call this first to refresh sensor state
if (touch.isTouched()) {
Serial.println("Currently being touched");
}
if (touch.justTouched()) {
Serial.println("Touch started");
}
if (touch.justReleased()) {
Serial.println("Touch ended");
}
delay(50); // Adjust debounce timing if needed
}| Method | Description |
|---|---|
TouchSensor(pin) |
Constructor — sets the pin the sensor is connected to |
void update() |
Refreshes internal state — must be called in loop() |
bool isTouched() |
Returns true if the sensor is currently touched |
bool justTouched() |
Returns true only on the frame when touch starts |
bool justReleased() |
Returns true only on the frame when touch ends |
- Capacitive touch light switch
- Smart home control panels
- Secret keypad (touch-based password input)
- Contactless interfaces in public devices
- DIY electronics and Arduino projects
- To make your sensor more or less sensitive, try:
- Increasing the grounding around the sensor pad
- Adding a resistor (~1MΩ) between OUT and GND for stability
- Works well with interrupts if
.update()is moved into an ISR-safe structure
This library is licensed under the MIT License, so you are free to use, modify, and distribute it in personal or commercial projects.
Developed and maintained by Herobrine Pixel
Feel free to open an issue or submit a pull request if you'd like to improve the library.
TouchSensor/
├── examples/
│ └── BasicTouchSensor_examples/ # Sample sketch
│ └── BasicTouchSensor_examples.ino
├── src/
│ └── TouchSensor.h
│ └── TouchSensor.cpp
├── library.properties
├── README.md
├── LICENSE
└── keywords.txt
- ArduinoButton Library – Similar logic but for mechanical buttons
- CapacitiveSensor Library – For analog capacitive sensors
Happy Tinkering! 🛠️