How do I start?
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
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.
Now open the Arduino IDE and copy the code below
If you don’t know how to do this, check out our Arduino Sketch set up tutorials and youtube videos.
#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
}
}
/*
Created 02/2020
by Joel Gray
graycode.ie
*/