Arduino Tutorial: Capacitive Touch Sensors
Arduino Tutorial: Capacitive Touch Sensors
Arduino Tutorial: Capacitive Touch Sensors
Arduino Tutorial: Capacitive Touch Sensors

Hello 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 no force to activate.

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

  • How Capacitive Sensors work
  • How to use them with an Arduino
  • How to implement them in 3 Arduino projects, that are included in this Instructable
  • How 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!

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

    Arduino Tutorial: Capacitive Touch Sensors

    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 a novice 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 of an 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 pins required. 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 so you 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.

    Step 2: CapSense Library Demo Code...

    Arduino Tutorial: Capacitive Touch Sensors

    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.

    Arduino Tutorial: Capacitive Touch Sensors
    CapacitiveSensorSketch.pde1 KB

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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 3 projects 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 to ooperatehigh 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, so you can use it for your own purpose. No soldering required.

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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 project uses a capacitive touch sensor, that switches on and off an led, or even high voltage appliances, by touching a piece of aluminium foil.

    Time:

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

    Difficulty: Very Very Easy.

    Materials: (Available on RadioShack)

  • Arduino Board
  • 500K to 1M resistor
  • 220ohm resistor
  • Any colour LED
  • Foil
  • Jumper wire
  • Relay (optional)
  • Mains plug (male and female) (optional)

    Step 5: Touch Controlled Light:- The Circuit

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    The 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 you must 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.
  • I have used pin 4 as the send pin and pin 2 as the receive pin on the Arduino.
  • There is a 1M resistor across these pins. The foil is connected to the receive pin, i.e pin 2.

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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 of installing 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 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); } void loop() { 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; Serial.println(millis()-time); delay(10); }

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Show All 12 Items

    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 to switch 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...

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Show All 8 Items

    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 high voltage 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.
  • Relay's NO to one terminal of the appliance.
  • The other terminal of the mains to the 2nd terminal of the appliance.

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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, is connected to the transistor.

    Step 10: Touch Controlled Light:- Finished!

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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 automation system. 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.....

    Step 11: Proximity Detecting Light Switch

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    Bringing 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 previous steps. In fact, the latter can be easily modified to make this proximity detecting light switch...

    Time: Total

    Cost: ~1$, excluding Arduino

    Difficulty: Easy

    Materials Required:

  • Arduino Board
  • 5.6M resistor
  • 220ohm resistor
  • Any colour LED
  • Foil
  • Jumper wire
  • Relay (optional)
  • Mains plug (male and female) (optional)

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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 resistor across pins 2 and 4 of the arduino.

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

    Arduino Tutorial: Capacitive Touch Sensors

    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 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); } void loop() { 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; Serial.println(millis()-time); delay(10); }

    Step 14: Controlling High Voltage Appliances...

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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

    Step 15: Touchless 3D Controller

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    This 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 Light Switch, 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 of course, 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 hand in different positions. Didn't get it? Go on to the next step for more....

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

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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 z coordinates 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 (the closer my hand is to the right sensor), the brighter the red colour of the rgb LED.

    Time: 1 - 1.5 hours

    Cost:

    Difficulty: easy.

    Materials Required:

  • Aluminium foil
  • Arduino Board (I recommend the Uno)
  • 10k ohm resistors x3
  • 220kohm resistors x3
  • 220 ohm resistors x3
  • Wire
  • RGB LED

    Step 17: Preparing The Touch Sensors....

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Show All 7 Items

    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 the Styrofoam 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 will definitely help a lot.

    Step 18: The Circuit...

    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors
    Arduino Tutorial: Capacitive Touch Sensors

    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.

    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 representing each 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 very different '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.
    Arduino Tutorial: Capacitive Touch Sensors
    _3d_controller.ino1 KB

    Step 20: Conclusion...

    Arduino Tutorial: Capacitive Touch Sensors

    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 stop here- 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 the countless 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. Or maybe 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..

  •  
     

    Tag cloud

    make build easy simple arduino making homemade solar laser printed portable cheap mini building custom cardboard wooden create super lego turn paracord chocolate your paper light intel