This tutorial will teach you how to create a simple capacitive touch sensor using an Arduino. Capacitive touch sensors detect the human body’s ability to hold an electrical charge and are commonly used in touchscreens, touchpads, and modern gadgets. By the end of this tutorial, you’ll have a working capacitive touch sensor to control an LED with just a touch.
Capacitive touch sensors work by measuring the change in capacitance when a conductive object (like a human finger) comes in close proximity to the sensor. A simple way to detect touch is by using an Arduino and a resistor to measure the time it takes for a pin to change state, which changes depending on the capacitance.
Follow these steps to set up the touch sensor:
Here is the code to read touch inputs and trigger an LED:
#include
CapacitiveSensor cs = CapacitiveSensor(2, 4); // Create capacitive sensor object
int ledPin = 13; // Built-in LED pin
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
long sensorValue = cs.capacitiveSensor(30); // Measure capacitance with timeout of 30ms
Serial.println(sensorValue); // Print sensor value to serial monitor
if (sensorValue > 1000) { // If a touch is detected
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(10); // Small delay between readings
}
CapacitiveSensor cs = CapacitiveSensor(2, 4);
: This line creates a capacitive sensor object, specifying Pin 2 as the sender pin and Pin 4 as the receiver (sensing) pin.cs.capacitiveSensor(30);
: Reads the capacitance value, with 30ms as the timeout.sensorValue > 1000
: If the capacitance exceeds the threshold, it means a touch is detected, so the LED is turned on.You’ve built a basic capacitive touch sensor using an Arduino. This can be expanded to control multiple devices, make touchpads, or develop user interfaces for your projects. By modifying the code and adding more sensors, you can create more complex touch-sensitive applications.
If you’re interested in taking your capacitive touch projects further, here are some ideas:
If your touch sensor is not working properly, consider the following tips:
Here are some additional resources to help you with further projects involving capacitive touch sensors: