Capacitive Touch Controlled LED With Arduino

by GrayCode in Circuits > Arduino

273 Views, 1 Favorites, 0 Comments

Capacitive Touch Controlled LED With Arduino

captouchNEW.png

Remember when you were young and you had encountered your first touch activated lamp and you thought it was magic? Well I’m sorry to say it wasn’t magic, it was probably capacitive touch.

What we’re doing here is using the conductive properties of the human finger to change the values read by the capacitive sensor (here we’re using a sheet of steel however you can use anything conductive) and depending on the state of the LED (ie. if the LED is on or off) we will change the state to be the opposite state.

View this project and more at my website Gray Code!

Supplies

  • 1x Breadboard
  • 1x Arduino Uno
  • 1x 1M Ohm Resistor
  • 1x 330 Ohm Resistor
  • 1x LED (Any colour)
  • 1x Conductive Pad/Sheet
  • 5x Jumper Wires
  • 1x USB-A Cable

Let's Construct the Circuit!

20200706_150659.jpg

The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.

The LED is connected to Digital Pin 11 of the Arduino via a resistor. Connect Pins 4 & 6 to either end of the resistor. And from the node where Pin 6 and the resistor meet, we connect a wire to our electrode. A quick summary of the circuit can be seen below:

Pin 11 > Resistor > LED > Ground

Pin 4 > Resistor > Pin 6

Resistor/Pin 6 node > Conductive Material

Now the Code:

20200706_151744.jpg

Let’s run through what the code is doing

The following code is used to activate an LED when the touch pad is touched using the Arduino Microcontroller. This part of the circuit is inside ‘void loop()’ therefore the steps mentioned above repeat forever or until told not to.

The values you return for your ‘val’ variable will vary depending on what you use for touch pad/conductive material. I found that for my application ‘val’ normally returned under 1000 when idling and raised above 1000 when being touched. This meant that I could use val >= 1000 to mean that the material had been touched.

Thus I created an if/else if statement which checked if val >=1000 and checked if pos == 0 or pos == 1. If val >= 1000 the touch pad had been touched and if pos == 0 then the LED is off, so then we used digitalWrite() function to turn the LED on drive it ‘high’.

We then changed the value of ‘pos’ from 0 to 1 to have the state of the LED stored to that variable. So that we can check whether or not the LED is on or off the next time we go around the loop. We set a 500ms delay, as a rudimentary debounce.

Check out the information given on Arduino documentation to learn more. You can find the Capacitive Sensor Library file on our GitHub, you’ll need it to complete the project.

#include <CapacitiveSensor.h>            //include library files
CapacitiveSensor Sensor = CapacitiveSensor(4,6);  //assign sensor pins
long val;                     //initalise variables
int pos;
int led = 10;                 //assign pin 10 to be 'led'

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);         //begin serial comms
  pinMode(led, OUTPUT);       //set led as an output
}

void loop() {
  // put your main code here, to run repeatedly:
  val = Sensor.capacitiveSensor(30);        //set val equal to the reading from 'sensor'
  Serial.println(val);                      //show value of val in serial monitor for debugging

  if (val >= 1000 && pos == 0)              //if value read is less that or equal to 1000 AND pos is equal to 0
  {
    digitalWrite(led,HIGH);                 //turn LED on
    pos = 1;                                //change pos to 1
    delay(500);                             //delay the program for 500ms
  }

  else if (val >= 1000 && pos == 1)         //if value read is greater that or equal to 1000 AND pos is equal to 1
  {
    digitalWrite(led,LOW);                  //turn LED off
    pos = 0;                                //change pos to 0
    delay(500);                             //delay the program for 500ms
  }

}

Upload the Code and Watch the Magic!

Upload the code from the Arduino IDE and enjoy your own touch LED!

View this project and more at my website Gray Code!