Public post

Create a touch button with copper/aluminum foil

· Subscription Projects

Cell, Row, Column | Range Examples | Fill a Range | Move a Range | Copy/Paste a Range | Insert Row, Column

We are going to create a LED that turns on when you touch a piece of a 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 charges 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

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 2x
Breadboard 400 points 1x
220Ω resistor 1x
330Ω resistor 1x
Copper/Aluminium Foil 1x

Circuit Diagram

touch sensor aluminium foil

Circuit Diagram – Connect an LED to pin 12, and connect the cathode to the 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 experiment with that and see what values are given by different resistors.

The Code

#include <CapacitiveSensor.h>


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

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

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

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

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

  if (sensorValue > treshold) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
   // arbitrary delay to limit data to serial port
  delay(10);                             
}

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 charges, 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 used 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 <CapacitiveSensor.h>

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

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.

 // the minimum value for turning the LED on
int treshold = 500;
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.

// the minimum value for turning the LED on int ledPin = 12;
int treshold = 500; 
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);
  }
// arbitrary delay to limit data to serial port
  delay(10);                             

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.