Capacitive touch is a common tool used in touch screens and smart phones, and Arduino has an easy library to utilize capacitive touch sensors! In this tutorial we show how to light up two different LED sensors, one for light touch and one for firm touch! The code shown in this project can be found here: https://github.com/plemaster01/ArduinoCapactiveTouch
Thanks for watching, don't forget to leave a like on the video and subscribe to the channel, and leave a comment with any questions you may have or to suggest future content!
what's up you guys welcome back to the
Master Tech and in today's video I'm
going to show you how to use capacitive
touch with Arduino
[Music]
so capacitive touch is a pretty cool
Concept in electrical engineering and
electronics and it's very similar to how
a lot of smartphone and smart screen
technology Works we've covered what
capacitors do on the channel in past
Arduino videos but just to recap a
capacitor is an electrical component
that stores a small amount of electrical
charge that can be used to smooth out
electrical signals and so capacitive
touch with Arduino is actually saying
you are being used as the capacitor so
your body is storing a small amount of
electrical charge and that's how
capacitive touch works so in total the
hardware that you'll need for this
project is just two LEDs two 220 kilo
ohm resistors one Mega ohm resistor
about six or seven jumper wires A
breadboard your Arduino and obviously a
Serial cable to communicate with the
computer now let's zoom in and take a
look at the physical build for this
project go ahead and start the physical
build by connecting your Arduino ground
pin to the ground rail on one power rail
of your board next let's hook up the
LEDs and we're going to put our 220 ohm
resistors in line with the negative leg
of both diodes remember that the
negative leg on LEDs is typically the
shorter leg now let's hook the power leg
up the positive lead of both our LEDs to
digital pins 12 and 13 on your Arduino
board I'm using 12 and 13. you can use
any digital pins that you want now let's
hook up our capacitive sensor and for
this just put your one Mega ohm resistor
on the board connect one leg of the
resistor to pin two of the Arduino board
and the other leg to pin 4 of your
Arduino board for these pins one is
going to be the transmitter and one is
going to be the receiver that we use to
detect how long before the two stabilize
then on the other side of the pin 2 leg
of the resistor hook up your capacitive
sensor wire and don't hook the other end
of this to anything since this is what
will be touching and making contact with
when we look at the sketch of the
project and that's it for the physical
build you can hold off on plugging your
board into the computer until we've
written the code which we'll do in this
next step alright so let's take a look
at the programming for this project it's
quite straightforward but let's go
through it together to make sure
everyone's on the same page the
capacitive sensor library that we're
going to use is free and open source but
it's not included by default with the
Arduino IDE so go to sketch go to
include library and then go to manage
libraries and just type in
capacitive sensor okay and the one that
we'll be using is by Paul Badger Paul
stofgren it's uh it's recommended by the
Arduino official Arduino beginner
projects book and just install that
there's a big button right there for
install and then once you've done that
you can go back into your code and you
can use this pound sign include
capacitivesensor.h and that's going to
give you access to the capacitive sensor
library then we need to set up an
instance of capacitive sensor and say
what two pins we want to use as our
sensor and the first pin we put in is
going to send the charge and the second
pin we put in is going to sense the
charge since we put our sensor pin on
the same leg of the resistor as a pin
two we are going to use pin 2 as our
sensor and pin 4 as our sender if you
were to move the capacitive sensor wire
that we put on pin 2 over to pin 4 then
you would want that to be second here
then for now you can use the same two
threshold lines that I have of 1400 and
300 if you want or you can hold off
until we actually boot this project up
and start seeing what the different
values are for your sensor basically
what I found is it's darn close to zero
when not nothing is touching that wire
if you let it lightly rest on your
finger it can go anywhere between like
500 and 1000 and then when you squeeze
it it's usually like fifteen hundred to
two thousand so I found these to be good
thresholds for light touch and then firm
touch but feel free to play around with
these as we get into the testing phase
of this build now our two LEDs are on
pin 12 and pin 13 so just set those up
in the beginning as well and then in the
setup loop we're just going to open the
serial connection because we know we
want to read what our sensor is reading
and then we're going to set both of our
LED pins up as outputs now in the loop
section of the code we're going to use a
long version of the sensor value
variable that we're creating to sense
what the capacitive sensor is detecting
and this value is how many readings you
want it to take 30 is a good
intermediate because if you don't read
enough then you're going to start
getting wide variance in your sensor but
if you read too many data points here
you're going to start getting a really
long lag so again feel free to
experiment with this number but 30 is a
really nice medium value for taking
enough sense sensor value readings that
you're going to get a fairly smooth
reading but it's not going to slow your
program down too much
and then we're going to print to the
serial window this sensor value and what
we want to do now is control the LED
pins based off of our sensor value so if
it's greater than that first threshold
let's turn our first LED pin on and
that's what we're saying our firm pin uh
our threshold our higher threshold was
just threshold one and that's going to
be our firm squeeze so we'll turn that
first LED pin on if we break the
threshold otherwise turn that LED pin
off and then the same exact if else
statement for LED pin 2 just now based
off threshold 2 which is a lower
threshold so that's our green light our
light touch threshold and then put a
small delay in here so that the program
has a little time to actually respond
and react this is just 10 milliseconds
it's not a long delay and it just means
your sensor has a reasonable amount of
time to adjust to actual field data okay
and so uh this is all you need you can
find a link to this code in the
description of the video below or you
can go to file examples uh and then in
the starter kit basic kit p13 touch
sensor lamp is very close to this all
you need to do is add threshold to an
LED pin 2 to your program so that you
get two sensors because that only has
one uh LED in it by default go ahead and
locate your Arduino board from the drop
down hit upload as long as you don't get
any errors saying that something was
done wrong it'll say successful it'll
show you how much the memory it used and
now we can check out what's happening
with our board so right away when you
power it on you should notice that your
communication lights start lighting up
but your LEDs don't immediately turn on
if you have an LED turned on right away
then your thresholds you used in the
code might be a little too low so you
can tweak those but let's take a look at
what happens when I lightly rest the PIN
on my finger hopefully if you set the
good thresholds up your green light
should be on pretty consistently and
your red light shouldn't come on until
we go ahead and give it a good squeeze
and when you give it a good squeeze what
you should see on your serial monitor on
your screen is that you start getting
much higher values than when the wire is
just resting on your finger this is
because when you're squeezing it you're
you're actually storing quite a bit more
electricity because you're able to make
a better connection but when it's just
resting on your finger you have a light
connection so you're not storing as much
capacitive energy feel free to play
around with the thresholds until you
start getting really good numbers where
you're able to make the both lights turn
off when you're not making contact the
green light on when you're making light
contact and the red light on when you're
making firm contact if you want try
connecting this capacitive sensor pin to
a piece of tin foil or any other surface
and see how that changes the values you
get through your capacitive sensor okay
guys that is going to do it for 10
today's video on capacitive touch using
Arduino if you have any questions about
what you saw here or something you want
to see in a Future video just let me
know about in the comments below I'll
get back to you as soon as I can and you
might see that content in a future video
all the hardware and the pieces that I
used in this video can be found in the
Arduino starter kit that I'll leave a
link to in the description of this video
if you need any of those pieces check it
out if you love the content we're
producing on the Channel please consider
becoming a patreon Super supporter of
the channel at the link below as well as
always good luck with your projects
thanks for watching until next time bye