26
http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/ Food Living Outside Play Technology Workshop Arduino Tutorial: Capacitive Touch Sensors by DangerousTim on August 22, 2014 Table of Contents Arduino Tutorial: Capacitive Touch Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Intro: Arduino Tutorial: Capacitive Touch Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Step 1: About The CapSense library and Capacitive Sensing... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Step 2: CapSense Library Demo Code... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Step 3: Projects With Capacitive Touch And Arduino... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Step 4: Touch Controlled Light:- Materials Required... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Step 5: Touch Controlled Light:- The Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Step 6: Touch Controlled Light:- Arduino Code... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Step 7: Touch Controlled Light:- Adding a Relay... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The Circuit: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Step 8: Touch Controlled Light:- Making a Box for the Relay... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Prepare The Box: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Make Connections: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Step 9: Touch Controlled Light:- Add The Touch Sensor and Arduino... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Step 10: Touch Controlled Light:- Finished! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Step 11: Proximity Detecting Light Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Step 12: Proximity Detecting Light Switch:- The Circuit... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Step 13: Proximity Detecting Light Switch:- The Code.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Step 14: Controlling High Voltage Appliances... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Step 15: Touchless 3D Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Step 16: How it Works, and Materials... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Step 17: Preparing The Touch Sensors.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Step 18: The Circuit... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Step 19: Upload The Code... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Step 20: Conclusion... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Step 21: Please Comment And Express Your Opinions... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

How to Use Touch Sensors With Arduino

Embed Size (px)

DESCRIPTION

How to Use Touch Sensors With Arduino

Citation preview

Page 1: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Food      Living       Outside        Play        Technology       Workshop

Arduino Tutorial: Capacitive Touch Sensorsby DangerousTim on August 22, 2014

Table of Contents

Arduino Tutorial: Capacitive Touch Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro:   Arduino Tutorial: Capacitive Touch Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1:   About The CapSense library and Capacitive Sensing... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 2:   CapSense Library Demo Code... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 3:   Projects With Capacitive Touch And Arduino... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Step 4:   Touch Controlled Light:- Materials Required... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Step 5:   Touch Controlled Light:- The Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Step 6:   Touch Controlled Light:- Arduino Code... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Step 7:   Touch Controlled Light:- Adding a Relay... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

The Circuit: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Step 8:   Touch Controlled Light:- Making a Box for the Relay... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Prepare The Box: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Make Connections: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Step 9:   Touch Controlled Light:- Add The Touch Sensor and Arduino... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Step 10:   Touch Controlled Light:- Finished! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Step 11:   Proximity Detecting Light Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Step 12:   Proximity Detecting Light Switch:- The Circuit... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Step 13:   Proximity Detecting Light Switch:- The Code.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Step 14:   Controlling High Voltage Appliances... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Step 15:   Touchless 3D Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Step 16:   How it Works, and Materials... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Step 17:   Preparing The Touch Sensors.... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Step 18:   The Circuit... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Step 19:   Upload The Code... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Step 20:   Conclusion... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Step 21:   Please Comment And Express Your Opinions... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Page 2: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Author:DangerousTim    YouTube ChannelI'm just this guy who loves tinkering with Arduino and electronics, and for some reason like to share it with the rest of the world through the internet.

Intro:  Arduino Tutorial: Capacitive Touch SensorsHello everyone!

This instructable is a tutorial that will guide you on using the Capacitive touch sensors with Arduino. A Capacitive sensor is a kind of touch sensor, that requires little or noforce to activate.

In this Instructable about capacitive touch sensing, I'll show you:

How Capacitive Sensors workHow to use them with an ArduinoHow to implement them in 3 Arduino projects, that are included in this InstructableHow to use the CapSense library- which is used in the first two projects. I've also included the demo code and it's explanation for the CapSense library...

Skills Required for these projects are minimal:

Basic knowledge of electronics and arduino.Soldering (optional)

So let's get started by first getting to know what this Capacitive touch sensing is all about!

Image Notes1. The completed touch switch

Image Notes1. 3D controller

Image Notes1. Proximity sensing switch..

Page 3: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Step 1: About The CapSense library and Capacitive Sensing...For most capacitive sensing related projects, I used the CapSense library. The arduino playground has a good explanation of the Capsense library. However, if you are anovice in Arduino, I' m pretty sure you'd understand only half of what's written there. Therefore here I have a simplified explanation...

Capacitive Sensing:

Capacitive touch sensing is a way of human touch sensing, that requires little or no force to activate. It may be used to sense human touch through more than a quarter ofan inch of plastic, wood, ceramic or other insulating material (not any kind of metal though), enabling the sensor to be completely visually concealed.

Why Capacitive touch?

Each touch sensor requires only one wire connected to it.Can be concealed under any nonmetallic mmaterial.Can be easily used in place of a button.Can detect a hand from a few inches away, if required.Very inexpensive.

How does it work?

The sensor plate and your body forms a capacitor. We know that a capacitor stores charge. The more its capacitance, the more charge it can store.

The capacitance of this capacitive touch sensor depends on how close your hand is to the plate.

What does the Arduino do?

Basically the arduino measures how much time the capacitor (i.e the touch sensor) takes to charge, giving it an estimate of the capacitance.

The capacitance may be very small, nevertheless the Arduino measures it with accuracy.

One way of using capacitive touch in a project is to use the CapSense library. For the Capsense library, the arduino uses one send pin and any number of receive pinsrequired. A receive pin is connected to the send pin via a medium to high value resistor.

Here are some guidelines for resistors but be sure to experiment for a desired response.

Use a 1 megohm resistor (or less maybe) for absolute touch to activate.With a 10 megohm resistor the sensor will start to respond 4-6 inches away.With a 40 megohm resistor the sensor will start to respond 12-24 inches away (dependent on the foil size). Common resistor sizes usually end at 10 megohm soyou may have to solder four 10 megohm resistors end to end.One tradeoff with larger resistors is that the sensor's increased sensitivity means that it is slower.

*These resister value guideline is only for using the CapSense library. The last project doesn't use this library.

Summary: Capacitive touch sensors require little or no force to activate. The CapSense library can be used to make multiple touch sensors with Arduino.

Page 4: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Step 2: CapSense Library Demo Code...Here is an explanation of the demo code for the CapSense library.

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);

You can replace 'cs_4_2' with any other name. '4' is the send pin, '2' is the receive pin.

void setup() {

Setup runs only once.

cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);

// turn off autocalibrate on channel 1 - just as an example

Serial.begin(9600);}

Begins Serial Communication at 9600baud...

long total1 = cs_4_2.capacitiveSensor(30);

The above statement is declared in 'void loop ()'. When using a 1M resistor, total1 will less than or about ten. When touched, it becomes more than 60...

Serial.println(total1);

Sends the value of total1 to the Serial window of the computer...

The important parts of the code are explained. This isn't the full code; the full one is in the attachment below. Skip the step if you think the code is easy.

File Downloads

CapacitiveSensorSketch.pde (1 KB)[NOTE: When saving, if you see .tmp as the file ext, rename it to 'CapacitiveSensorSketch.pde']

Step 3: Projects With Capacitive Touch And Arduino...Now that you've got an idea of what capacitive touch is (if you happened to read the previous step), let's see what we can do with it. In this instructable I will show you 3projects incorporating capacitive touch.

Touch Operated Light:

A basic project that will help you grasp the ideas of capacitive touch and implement it in other projects. Using a relay and soldering is optional, but required if you want toooperatehigh voltage appliances.

Proximity Sensor Switch:

Bringing your hand close to this switch will turn on or off an led. Adding a relay and soldering will enable it to operate high voltage devices, though it is optional.

3D Touchless Hand Tracker:

Tracks the position of your hand in 3 dimensions. Can be used for innumerable applications, however I have included projects with it that are simple and easy to hack, soyou can use it for your own purpose. No soldering required.

Page 5: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. The completed touch switch

Image Notes1. Proximity sensing switch..

Image Notes1. 3D controller

Step 4: Touch Controlled Light:- Materials Required...This is a very cheap and easy project. It gives a good idea of how capacitive touch works and how one can implement in several other applications. This simple projectuses a capacitive touch sensor, that switches on and off an led, or even high voltage appliances, by touching a piece of aluminium foil.

Time: <1 hour, +1 hour for soldering relay ( optional)

Cost: Few cents, ~1$ for a relay ( excluding Arduino)

Difficulty: Very Very Easy.

Materials: (Available on RadioShack)

Arduino Board500K to 1M resistor220ohm resistorAny colour LEDFoilJumper wireRelay (optional)Mains plug (male and female) (optional)

Page 6: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. The completed touch switch

Image Notes1. Any arduino board2. Foil3. LED and 220ohm resistor in series...4. This time a 5.6M resistor

Image Notes1. Power supply for the arduino. My arduino's onboard regulator doesnt work, so ihad to make this.

Step 5: Touch Controlled Light:- The CircuitThe circuit is simple. I mean so simple that you won't even need a breadboard!

Note that I have used an Arduino Mega, because right now that's the only one I have. If you are using an Uno, connect the LED to any other convenient pin. However youmust make a small change in the code later. See the next step.

Follow the pictures for the circuit.

I connected the led and 220ohm resistor in series across pin 42 and ground.1.I have used pin 4 as the send pin and pin 2 as the receive pin on the Arduino.2.There is a 1M resistor across these pins. The foil is connected to the receive pin, i.e pin 2.3.

Page 7: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. Use a 5M resistor. You may need to expenriment though, and you may needupto a 10M resisotr...

Image Notes1. Foil connected to Arduino. Power it up and it'll work...

Step 6: Touch Controlled Light:- Arduino Code...Now to the programming part. The code uses the CapSense library (download here), that takes care of capacitive sensing. If you're not familiar with the process ofinstalling a library, check out this awesome tutorial:

Installing An Arduino Library - Learn.SFE

The code is simple and easy to understand. Simply copy this code and paste it a a blank Arduino wwindow. Change the led pin if required. Then upload...

#include <CapacitiveSensor.h>

int led = 42; //change '42' to any desired pin...

long time = 0;int state = HIGH;

boolean yes;boolean previous = false;

int debounce = 200;

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

// To add more sensors...//CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil//CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); //Calibrate the sensor...

pinMode(led, OUTPUT);}

Page 8: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

void loop() {

long total1 = cs_4_2.capacitiveSensor(30);

if (total1 > 60){yes = true;}else {yes = false;}

// to toggle the state of stateif(yes == true && previous == false && millis() - time>debounce){

if(state == LOW){ state = HIGH; }

else state = LOW; time = millis();

}

digitalWrite(led, state); previous = yes;

Serial.println(millis()-time);delay(10);

}

Step 7: Touch Controlled Light:- Adding a Relay...So you got a touch controlled LED. So what? You can make the project a tad bit more useful by controlling the appliances like lights and fans with touch. And fortunately,you only need to add a relay to the Arduino to do this.

What is a Relay ?

A relay is an electrically operated switch. Usually relays use an electromagnet to mechanically operate an isolated switch. It's main function is to use a small voltage toswitch on/off a high voltage or high current separately. It's handy for controlling mains appliances with small circuits or micro controllers.

To learn more about relays, check out this great Instructable - How Electronic Switches Work For Noobs: Relays and Transistors

The Circuit:

The Arduino cannot supply enough current to operate a relay, therefore you'd need a BC547 transistor. The transistor amplifies the Arduino's current to activate the relay.For help in understanding check this helpful video - How to Control Relays with Arduino

I have provided the circuit diagram for the transistor and relay. There is a Fritzing breadboard diagram too, to help you understand the connections.

After that prepare the male plug as shown in the pictures above...

Page 9: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. This is a relay.

Image Notes1. The pinout

Image Notes1. This pin is nc or normally closed, and we don't need it for the project. Theresistance between COM and this pin is a few ohms when the relay is idle.2. High voltage input or COM3. High voltage out.4. Input5. Connected to ground( of the battery ).

Image Notes1. Emitter connected to relays coil2. The relay coils other terminal; connected to ground..3. 1K resistor4. Connected to '+'5. A diode cpnnected acrosse the relay's coil, with the terminal next to the silverband connected to the transisotr's emitter.6. The diode's anode is connected to gound

Image Notes1. You need this kind of wire for the high voltage devices...

Image Notes1. I didnt use this one...2. Male plug

Page 10: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. A much better female plug.....

Image Notes1. Male plug ready!!!

Page 11: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Step 8: Touch Controlled Light:- Making a Box for the Relay...I used a plastic box of 5*2*2 inches for housing the relay and wires. It may be a bit too big, but good enough for demonstration. My main intention was to keep highvoltage wires separate from the Arduino and my body.

Prepare The Box:

Cut out a square hole from the lid of the box to fit the female plug. Make holes for the screws, and then fit the plug. Stick the relay to the bottom of the box.

Make Connections:

The circuit involving high voltage is very simple. Follow the pictures and their tags, or the following steps:

Relay's COM to one terminal of the mains supply.1.Relay's NO to one terminal of the appliance.2.The other terminal of the mains to the 2nd terminal of the appliance.3.

Image Notes1. Relay in the box

Image Notes1. One wire of he male plug...2. One wire connected to a terminal of the appliance.

Image Notes1. COM of the relay is connected to one wire of male plug...2. The "NO" pin of the relay is connected to one terminal of the appliance...

Image Notes1. All connected and insulated...

Page 12: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. Cut out some plastic for the female plug...

Image Notes1. Make holes for the screws...

Image Notes1. The female plug joined to the box...

Image Notes1. This yellow wire is directly connected to the male plug2. This red wire is connected to the "NO" pin of the relay...

Step 9: Touch Controlled Light:- Add The Touch Sensor and Arduino...Stick a piece of foil on the box with tape as shown above. This is connected to the Arduino's receive pin (pin 2 in my case). The "led pin", or pin 42 in my case, isconnected to the transistor.

Page 13: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. Stick a piece of foil here with a wire connected to it sticking out...

Image Notes1. Remember the transistor circuit? Stick it with insulation tape here...

Step 10: Touch Controlled Light:- Finished!Connect the male plug to the mains outlet, and plug in the appliance you want to control to the box. Power up your Arduino with a battery.

There you have it - your own touch controlled light! You can use more relays and touch sensors to control more appliances, or incorporate it in a home automationsystem. For now I use it to control my desk lamp, and it works quite well.

Here's a video showing the working of the touch switch...

Also I have a video showing the prototype working...

Hope you don't mind the blast in the end :P.....

Image Notes1. The completed touch switch

Image Notes1. Arduino Board hiding behind...2. The rubber band keeps the Arduino and the box together3. A sticker over the foil. It still works...

Page 14: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Step 11: Proximity Detecting Light SwitchBringing your hand close to the switch, or simply waving your hand over it will turn on or off a light. It is actually very similar to the "Touch Controlled Light" in the previoussteps. In fact, the latter can be easily modified to make this proximity detecting light switch...

Time: Total <1 hour, +1 hour for optional soldering

Cost: ~1$, excluding Arduino

Difficulty: Easy

Materials Required:

Arduino Board

5.6M resistor220ohm resistorAny colour LEDFoilJumper wireRelay (optional)Mains plug (male and female) (optional)

Image Notes1. Proximity sensing switch..

Image Notes1. Any arduino board2. Foil3. LED and 220ohm resistor in series...4. This time a 5.6M resistor

Page 15: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Step 12: Proximity Detecting Light Switch:- The Circuit...The circuit is simple, it's just a modification of the "Touch Controlled Light". In fact the only thing you have to change is a resistor; this time you have to use a ~5M resistoracross pins 2 and 4 of the arduino.

Image Notes1. Use a 5M resistor. You may need to expenriment though, and you may needupto a 10M resisotr...

Image Notes1. Foil connected to Arduino. Power it up and it'll work...

Image Notes1. Stick a piece of foil here with a wire connected to it sticking out...

Step 13: Proximity Detecting Light Switch:- The Code....Here the code is similar to the "Touch Controlled Light". You only have to change the debounce time in the code, and adjust some other values with trial and error...

Copy this code and paste it in the Arduino window....

#include <CapacitiveSensor.h>

int led = 42; //change '42' to any desired pin...

long time = 0;int state = HIGH;

boolean yes;boolean previous = false;

int debounce = 200;

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

// To add more sensors...//CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil//CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void <strong>setup</strong>() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); //Calibrate the sensor... pinMode(led, OUTPUT);

Page 16: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

}

void <strong>loop</strong>() { long total1 = cs_4_2.capacitiveSensor(30);

if (total1 > 60){yes = true;} else {yes = false;}

// to toggle the state of state if(yes == true & previous == false & millis() - time>debounce){

if(state == LOW){ state = HIGH; } else state = LOW; time = millis();

}

digitalWrite(led, state); previous = yes;

<strong>Serial</strong>.println(millis()-time); delay(10);

}

Step 14: Controlling High Voltage Appliances...To control high voltage appliances, a relay is required. Follow the same instructions for setting up the relay as in the previous project...

Step7- Adding A Relay

Page 17: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. Remember the transistor circuit? Stick it with insulation tape here...

Image Notes1. COM of the relay is connected to one wire of male plug...2. The "NO" pin of the relay is connected to one terminal of the appliance...

Step 15: Touchless 3D ControllerThis 3D controller will track the position of your hand in 3 dimensions. It is based on Capacitive touch sensing. Like the previous project, the Proximity Detecting LightSwitch, the touch sensor will be able to sense your hand from a few inches away.

This is project is inspired by kylemcdonald's awesome Instructable- DIY 3D Controller. Have a look at his Instructable too to get an idea of this 3D controller...

There is a lot of stuff you can do with this 3D controller, for example, as a computer mouse, to control a 3D animation scene, control LED cubes, and a lot more. But ofcourse, they aren't that easy and cheap to do.

Therefore I have got a simple yet cool project apt for beginners- A 3D Tracker Controlled RGB LED. Basically you can control the colour of an LED by moving your handin different positions. Didn't get it? Go on to the next step for more....

Page 18: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. 3D controller

Image Notes1. RGB LED controlled by the 3D controller...2. Thse transistors are actually not required, check out the circuit in the later step...

Step 16: How it Works, and Materials...This 3D controller has 3 touch sensors, that respond from a few inches away. By judging the distance from each sensor the Arduino can calculate the x, y and zcoordinates of your hand.

The RGB LED has 3 colours: red, green and blue. Each colour's brightness depends on respective coordinate of the hand. For example, the lesser the x coordinate (thecloser my hand is to the right sensor), the brighter the red colour of the rgb LED.

Time: 1 - 1.5 hours

Cost: <$1, excluding Arduino...

Difficulty: easy.

Materials Required:

Aluminium foilArduino Board (I recommend the Uno)10k ohm resistors x3220kohm resistors x3220 ohm resistors x3WireRGB LED

Page 19: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. 3D controller

Image Notes1. Make a kind of slit, where the mountboard bends easily, but don't cut it fully!

Step 17: Preparing The Touch Sensors....We need three separate pieces of foil, with dimensions of about 8*8 inches. One piece of foil is stuck on a thick Styrofoam(thermocol) base. Slits are made on theStyrofoam on 2 adjacent sides of the foil, as show in the pictures above.

The other two pieces of foil are stuck on mountboard (which is white cardboard).

These two pieces of mountboard are inserted into the slits in the Styrofoam, perpendicular to each other and the Styrofoam, as shown in the pictures above.

To make the whole thing sturdy, I used a small rectangular piece of mountboard to hold the two mountboards with the foil together. The pictures above and their tags willdefinitely help a lot.

Page 20: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. Make a kind of slit, where the mountboard bends easily, but don't cut it fully!

Image Notes1. Bend it like that...

Page 21: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. For Support

Step 18: The Circuit...Connect a wire to each touch sensor with insulation tape. Then assemble the circuit on a breadboard, with the help of the Fritzing breadboard layout above.

Page 22: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Image Notes1. 'X' touch sensors tracks hand's x coordinate2. Y touch sensor3. Z touch sensor

Image Notes1. 220ohm2. 10Kohm3. 220Kohm

Step 19: Upload The Code...The code is provided in the attachment below. Just click it and copy and paste the code into the Arduino code window.

Before hitting upload, be sure to make changes to pins, if required.

Calibrating the Touch Sensors..

After uploading the code, open the Serial Monitor, with the Arduino still connected. You will see many lines of values rolling in, with each line having 3 values representingeach touch sensor. Touch any one touch sensor (you must know which one you are touching), and see its corresponding value on the Serial Monitor.

You will see this line in the code before void setup (){

int maxval = 20;

Change 20 to the value you got after touching one of the sensors.

Then upload the code once again. Moving your hand inside the 3D controller will produce a variety of colours.

Important Note: The below code only works for Arduino Uno (and probably Leonardo) compatible boards. I tried the Mega and it didn't work, because it has verydifferent 'PORTS' (even I found it hard to understand at first- this is for the more experienced blokes)....

Summary: Upload the code, adjust the "maxval" variable and upload again. Using an Arduino Uno or clone is recommended.

File Downloads

_3d_controller.ino (1 KB)[NOTE: When saving, if you see .tmp as the file ext, rename it to '_3d_controller.ino']

Page 23: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Step 20: Conclusion...Those are just 3 projects you can do with Capacitive touch. Hopefully they will give you a solid idea on using touch sensors to make awesome things. Don't just stophere- the stuff you can do with Capacitive touch sensing are endless: Touch keypads, track pads, Touchless computer mouse, touch controlled clock are only a few of thecountless ideas I can think of.

Going Further.....

You could do more with the projects I've shown. For example, using the 1st project's idea, make a touch switch board for controlling several lights and fans in a room. Ormaybe incorporate it in a home automation project.

With the idea of the proximity detecting light, try making an automatic desk lamp, that senses your presence.

With the 3D controller, try making a 3D mouse with an Arduino Leonardo. Or use a Processing programme to communicate with the 3D controller.

Other Tutorials and more...

playground.arduino.cc/Main/CapSense

Hackaday Tutorial

Here's a great video (its not mine though) just a simple Capacitive touch tutorial, which I have also covered. A video may make things clearer though....

Step 21: Please Comment And Express Your Opinions...With your help I can certainly make this Instructable better..

Related Instructables

Building aCapacitiveProximitySensor usingBare Paint byBareConductive

CapacitivetouchMood/Ambilightby lunar

How to createyour owncapacitive touchairplane withelectro dough!by techwillsaveus

CapacitiveSensing + OpenFrameworks +SPACEBREW bySnax_and_Macs

Come Home!ConnectingDistant SpacesEASILY overWeb bySnax_and_Macs

Arduino AirCap-SensePiano byvishalapr

Advertisements

Comments

18 comments Add Comment

 fcfelix says:  Sep 25, 2014. 2:03 AM  REPLYVery useful. Will try it. Any idea if the intensity of an LED strip can be controlled using one touch sensor? Like, the closer you put the hand, the brighter thestrip gets?

 DangerousTim says:  Sep 25, 2014. 5:03 AM  REPLYYes, as Raitis says, it is possible. See step 2 of this instructable - there is an attachment containing the CapSense code. Download that. Now modify it.

In void loop(){}, add this line after the Serial.print statements...

int x = map(total1, 100, 800, 0, 255);

analogWrite(13, x);

In void setup() declare pin 13 as output.

Page 24: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

Then hit upload. For calibrating/troubleshooting you should have some basic Arduino knowledge.

Now add a resistor of about 10M ohms between pin 2 and 4 (i.e. the send and receive pins)

The closer you move your hands to the foil, the brighter the onboard led on pin 13 will be.....

 Raitis says:  Sep 25, 2014. 2:19 AM  REPLYYes, totally doable, the output has quite a wide range of values depending on the resistor used and size of the surface. Source: I'm currently working ona project which uses this library.

I believe a code provided in this page may be useful for your application.

 gbushta says:  Sep 30, 2014. 10:19 PM  REPLYThis project was very easy to make because the instructions are nicely detailed. I put the aluminum foil inside a book to demonstrate that touching is notneeded. After messing with this project I discovered that I only need to touch the insulation on the wire from pin 2 with my thumb and finger to light the lampbetween the pin and the resistor, or touch the wire going to the foil. A long touch will cause the LED to blink.

Thank you for this project.

 DangerousTim says:  Oct 1, 2014. 4:55 AM  REPLYThat looks great! Even I got the blinking problem. But a quick touch did the job and so I didn't worry.However I would like it if someone would come up with a solution....

 anilbot says:  Sep 28, 2014. 7:00 PM  REPLYKudos Tim... good instructable. .. many salutes for giving a rather exhaustive instruction...btw, which part of the beautiful world are you from? curious I asked because I saw a very familiar plug socket we see in India. .. :)

 DangerousTim says:  Sep 29, 2014. 12:17 AM  REPLYYou guessed it right. BTW that male plug you see is one of the worst quality plugs I have ever used... :-)

 baecker03 says:  Sep 28, 2014. 9:47 PM  REPLYdoes humidity drastically affect the capacitance?

Page 25: How to Use Touch Sensors With Arduino

http://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/

 DangerousTim says:  Sep 29, 2014. 12:14 AM  REPLYThats A great question! Well, I don't think they will affect this type of sensor greatly, but I'm not sure. However according to Wikipedia, some humiditysensors use Capacitive sensors.....

 djshark0 says:  Sep 28, 2014. 10:24 PM  REPLYThank you so much, easy and simple.

 electro18 says:  Sep 25, 2014. 9:50 AM  REPLYHey there ! first of all that's impressive !

I'm getting one problem with the CapacitiveSensor library. I installed the library ( in the library folder ) but when I try to restart the IDE, it won't respond andeventually gives the error "error while starting the application". The IDE works fine when I remove the library from it's main folder. Please guide me through it.Thanks.

PS : IDE version : Arduino ERW 1.0.5

 DangerousTim says:  Sep 26, 2014. 12:38 AM  REPLYI had some problems installing it too. Make sure that:

1. The file downloads correctly

2. You have unzipped the file- its quite easy; just right-click the folder, chose extract all files.

3. Move the file to the correct location:

Arduino > Libraries

(Capital "L")

4.Check the name of the file. It shouldn't have spaces or dashes. Underscores are fine.

These tutorials will help

https://learn.adafruit.com/adafruit-all-about-ardu...

For Mac users

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&...

Hope this helps...

BTW your gesture controlled robot is Awesome!!!

 electro18 says:  Sep 26, 2014. 1:10 AM  REPLYThanks for the response ! :)

The problem was that , my "libraries" folder had too many libraries. After deleting a couple of unused libraries it worked fine.

And the idea of using three Aluminium foils to track 3D movement of hand is superb ! :D Why don't you enter this instructable in any contest ?

While using the Serial monitor I noticed that the readings are not constant and they keep on fluctuating. Is there any way to counter this problem ?

 DangerousTim says:  Sep 26, 2014. 1:41 AM  REPLYI'm not very sure. Grounding the Arduino with help. Maybe try adding some code to calibrate it...

 DangerousTim says:  Sep 26, 2014. 1:08 AM  REPLYsorry wrong link for Mac users see

http://arduino.cc/en/Guide/Libraries

 jcduplessis says:  Sep 25, 2014. 8:33 AM  REPLYVery nice instructable! You give a lot of essential information and some great tips! Thanks!

 DangerousTim says:  Sep 25, 2014. 9:36 AM  REPLYYour Welcome!