Skip to content

Capacitive sensor for Arduino

Add a capacitive sensor to your Arduino and make your Arduino work as a keyboard.

Capacitive-touch-1-1024x513

Circuit diagram

Materials

  1. Arduino Uno or Leonardo (use a Leonardo if you want to use it as a keyboard on your laptop)
  2. Breadboard
  3. Jumpercables
  4. 1㏁ resistor
  5. Crocodile clip
  6. Something conductive (metal/aluminium foil/conductive paint/copper tape/…)

Hardware

Use the materials and circuit diagram to set up the hardware. The pins 2 and 12 are chosen random, but they should match the pins in your code. The yellow cable labeled β€˜TOUCH’ can be connected with a crocodile clip to whatever conductive material you want to use.

Code

Start up the Arduino IDE and open the Library manager:

Screenshot-2022-06-02-at-22 03 41-1024x548

Search for the CapacitiveSensor library, hit install and restart the Arduino IDE.

Screenshot-2022-06-02-at-22 05 05

Use the following code to make the built-in LED flash for a moment. The example code also has lines for two other capacitive sensors on pins 4 and 5.

#include <CapacitiveSensor.h>

// Set the Send Pin & Receive Pin.
CapacitiveSensor cs_12_2 = CapacitiveSensor(12, 2);
//CapacitiveSensor   cs_12_4 = CapacitiveSensor(12,4);
//CapacitiveSensor   cs_12_5 = CapacitiveSensor(12,5);

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // Set the sensitivity of the sensor.
  long touch1 = cs_12_2.capacitiveSensor(1000);
  //  long touch2 =  cs_12_4.capacitiveSensor(1000);
  //  long touch3 =  cs_12_5.capacitiveSensor(1000);

  // When we touched the sensor, the built-in LED is turn on
  if (touch1 > 1000) {
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(100);                       // wait for a moment (1/10 second)
    digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  }
}