About The Project
In this project, we will study how to interface a touch sensor with Arduino Uno board.
Touch Sensor
The TTP223-1 is a capacitive touch sensor module used to detect touch input. When touched, its output pin goes HIGH; otherwise, the output remains LOW.
Circuit Wiring
Program Code
C++
// www.matthewtechub.com
//touch sensor
const int touchPin = 2;
// Pin connected to the touch sensor module
void setup() {
Serial.begin(9600);
// Initialize serial communication
pinMode(touchPin, INPUT);
// Set the touch pin as input
}
void loop() {
// Read the state of the touch sensor
int touchState = digitalRead(touchPin);
// Print (1 for touched, 0 for not touched)
Serial.println(touchState);
delay(1000); // Delay for stability
}
Code Explanation
C++
const int touchPin = 2;
Declarations
- This line defines a constant `touchPin` and assigns it the value `2`, indicating that the touch sensor module is connected to pin 2 of the Arduino.
C++
void setup() {
Serial.begin(9600);
// Initialize serial communication
pinMode(touchPin, INPUT);
// Set the touch pin as an input to read the state of the touch sensor
}
Setup Function
- Serial.begin(9600);` initializes serial communication at a baud rate of 9600 bits per second, allowing the Arduino to send data to the Serial Monitor.
- pinMode(touchPin, INPUT);` sets the touch sensor pin as an input, so the Arduino can read the state of the touch sensor.
C++
void loop() {
// Read the state of the touch sensor
int touchState = digitalRead(touchPin);
// Print the touch sensor (1 for touched, 0 for not touched)
Serial.println(touchState);
delay(1000);
// Delay for 1 second to stabilize the readings
}
Loop Function
- int touchState = digitalRead(touchPin);` reads the current state of the touch sensor pin. If the sensor is touched, `digitalRead` returns `HIGH` (1); if not, it returns `LOW` (0).
- Serial.println(touchState);` prints the state of the touch sensor (1 for touched, 0 for not touched) to the Serial Monitor.
- delay(1000);` pauses the program for 1000 milliseconds (1 second) to stabilize the readings and avoid excessive printing to the Serial Monitor.
Try Yourself
Modify the program code to turn on a buzzer for 1 second when the sensor is touched.