Capacitive Touch Sensor with Arduino

Capacitive Touch Control with Arduino

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.

Components Required

Understanding Capacitive Touch Sensors

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.

Wiring the Capacitive Touch Sensor

Follow these steps to set up the touch sensor:

Arduino Code

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
}
            

Code Explanation

Upload and Test

  1. Upload the code to your Arduino.
  2. Open the Serial Monitor (set baud rate to 9600) to see the capacitance values in real time.
  3. Touch the aluminum foil and watch the LED turn on, indicating a touch has been detected!

Conclusion

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.

Further Exploration

If you’re interested in taking your capacitive touch projects further, here are some ideas:

Troubleshooting Tips

If your touch sensor is not working properly, consider the following tips:

Additional Resources

Here are some additional resources to help you with further projects involving capacitive touch sensors: