Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Create a Touch Button with copper /aluminum foil

in Arduino Project

We are going to create a LED that turns on when you touch a piece of conductive material such as aluminum or copper.

To be able to make a touch sensor with the conductive material, you will be using the CapacitiveSensor library made by Paul Badger. The library allows you to write code that measures the capacitance of your body. The library checks two pins on your Arduino board (one is a sender, the other one is the receiver). It measures the time it takes for both pins to have the same state. The pins are connected with a conductive material such as copper foil or aluminum foil. As you get closer to the conductive material, your body will absorb some of the charge in the material, causing it to take longer for the two points to be in the same state.

Preparing the library

The most recent version of the CapacitiveSensor library is here: CapacitiveSensor Library. Download the file to your computer and unzip it. Open your Arduino sketch folder (it will be in your “Documents” folder by default). In the folder, create a new directory named “libraries”. Place the CapacitiveSensor folder you unzipped in this folder and restart the Arduino software. Click the File>Examples menu in the Arduino software, and you’ll see a new entry for “CapacitiveSensor”. The library you added included an example project. Open the CapacitiveSensorSketch example and compile it. If you don’t get any errors, you’ll know you installed it correctly.

In this project, you will edit the existing sketch that you opened. Before you can do that we need to wire everything according to the circuit diagram below

Parts Required

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 5
Copper / Aluminum foil Copper / Aluminum foil × 1
330 Ohm Resistor 330 Ohm Resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Circuit Diagram

touch sensor aluminium foil

Circuit Diagram –  Connect an LED to pin 12, and connect the cathode to ground through a 220-ohm resistor as shown. Connect digital pins 2 and 4 to your breadboard. Connect the two pins with a 330-ohm resistor. In the same row as pin 2, insert a long wire (8-10cm at least) that extends away from the breadboard, not connected to anything on the other end. This will become your touch sensor. Remember, the resistor between the two pins connected with the conductive material can be replaced by different resistor values. You can expiriment with that and see what values are given by different resistors.

The Code

#include 

/*
   CapitiveSense Library Demo Sketch
   Paul Badger 2008
   Uses a high value resistor e.g. 10M between send pin and receive pin
   Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
   Receive pin is the sensor pin - try different amounts of foil/metal on this pin
*/


CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);       // 330-ohmn resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int treshold = 500; // the minimum value for turning the LED on
int ledPin = 12;

void setup()
{
  cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  long start = millis();
  long sensorValue =  cs_4_2.capacitiveSensor(30);


  Serial.print(millis() - start);        // check on performance in milliseconds
  Serial.print("\t");                    // tab character for debug windown spacing

  Serial.print(sensorValue);                  // print sensor output 1
  Serial.print("\n");

  if (sensorValue > treshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

  delay(10);                             // arbitrary delay to limit data to serial port
}

 

Code Explanation

You’ll be using the CapacitiveSensor library by Paul Badger for this project. This library allows you to measure the capacitance of your body.
Capacitance is a measure of how much electrical charge something can store. The library checks two pins on your Arduino (one is a sender, the other a receiver), and measures the time it takes for them to have the same state. These pins will be connected to a metal object like aluminum foil. As you get closer to the object, your body will absorb some of the charge, causing it to take longer for the two pins to be the same.

Import the CapacitiveSensor library

At the beginning of your program, include the CapacitiveSensor library. You include it the same way you would a native Arduino library like the Servo library in the earlier projects. Create a named instance of the library. When you use this library, you tell the instance what pins it will be using to send and receive information. In this case, pin 4 sends to the conductive sensor material through the resistor, and pin 2 is the sense pin.

#include 

/*
   CapitiveSense Library Demo Sketch
   Paul Badger 2008
   Uses a high value resistor e.g. 10M between send pin and receive pin
   Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
   Receive pin is the sensor pin - try different amounts of foil/metal on this pin
*/


CapacitiveSensor   cs_4_2 = CapacitiveSensor(4, 2);       // 330-ohmn resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil 

 

Set up the threshold

Set up a variable for the sensing threshold at which the lamp will turn on. You’ll change this number after you test the sensor’s functionality. Then define the pin your LED will be on.

int treshold = 500; // the minimum value for turning the LED on
int ledPin = 12;

 

Setup() function

Set up a variable for the sensing threshold at which the lamp will turn on. You’ll change this number after you test the sensor’s functionality. Then define the pin your LED will be on.

int treshold = 500; // the minimum value for turning the LED on
int ledPin = 12;

 

Sensing Touch 

In the loop() function, create a variable of type long to hold the sensor’s value. The library returns the sensor value using a command called CapacitiveSensor() that takes an argument identifying the number of samples you want to read. If you read only a few samples, it’s possible you’ll see a lot of variation in the sensor. If you take too many samples, you could introduce a lag as it reads the sensor multiple times. 30 samples is a good starting value. Print the sensor value to the serial monitor.

long start = millis();
  long sensorValue =  cs_4_2.capacitiveSensor(30);


  Serial.print(millis() - start);        // check on performance in milliseconds
  Serial.print("\t");                    // tab character for debug windown spacing

  Serial.print(sensorValue);                  // print sensor output 1
  Serial.print("\n");

 

LED Control

With an if()…else statement, check to see if the sensor value is higher than the threshold. If it is, turn the LED on. If it is not, turn it off.

if (sensorValue > treshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

  delay(10);                             // arbitrary delay to limit data to serial port

 

Previous Post
Creating a password protected lock with a keypad, relay, and display for Arduino
Next Post
Create multiple proximity buttons with MPR121 for Arduino
You must be logged in to post a comment.
Menu