in this tutorial I'm going to show you
how to control the onboard LED
with a touch sensor
for this tutorial you'll need an Arduino
the touch sensor and three jumper wires male-to-female
if you don't have these jumper wires
you can also use a breadboard and three jumper wires
male-to-male
the first step is to connect your 
jumper wires to the touch sensor PCB
the touch sensor is actually a resistor
when you touch the PCB
the resistance will increase
therefore the PCB can detect
that you've touched the sensor
and sends the signal HIGH to the Arduino
now it's time to connect our touch sensor to the Arduino
the red wire is connected to the 5V
the blue wire is connected to the ground
and the green wire is connected to pin 8
our circuit is now complete 
let's have a look at the code
the code is pretty straightforward
if you look at line 19
we set pin number 8 as an input
on line 20, we use the LED_BUILTIN constant
to define the built-in LED
we set it as an output
the advantage of using LED_BUILTIN instead of pin 13
is that for the Arduino UNO you have pin 13
for the built-in LED
but for example for the Arduino MEGA
it could be some different pin
so with this constant
you are absolutely sure that you have
the correct pin for the built-in LED
then we make sure that the LED is turned off 
by doing a digitalWrite to the LED_BUILTIN pin
and set it to LOW
then we continue to the very small loop()
what we do is we read on pin number 8 to which
our sensor is connected
and check it if the value is HIGH
if so, we turn the LED on
and if not we turn it off
then we wait for 100ms, and the loop continues
so basically it checks 10 times per second
if the touch sensor has been touched
and turns the LED on and off accordingly
now I am going to upload this code to my Arduino
and show you what it looks like
I've uploaded the code to my Arduino
and when I touch the sensor
you see this onboard LED, marked with the L,
light up
when I press it, you see it's on
and now it's off, when I release my finger
we're now going to implement debounce on this
in this second part we're going to add the so-called
debounce to the touch sensor
debounce means that the touch is only triggered once
in a defined period of time
and I show you this in a minute
first we start with some new variables
we have the "previousMillis"
which is a "unsigned long" since we're going to store milliseconds in that
and the same goes for the "elapsedMillis"
then we have a "ledState" which is the state of the LED
and that's LOW because we want to turn
it off when we start
here we have the debounce time in milliseconds
and that's the amount of time which has to be
between two triggers of the touch sensor
so when I touch the touch sensor
multiple times within these 1000ms
the Arduino only sees a it tapped once
the setup() is basically the same
we define pin 8 as an INPUT
the LED_BUILTIN as an OUTPUT
and we turn the built-in LED off
so in the loop() something has changed
we store first in "elapsedMillis" 
the amount of milliseconds
minus the previous milliseconds
and these millis are the amount of milliseconds
since the Idina was turned on
then we have an if statement
and instead of just checking
whether pin number 8 is HIGH
we also check if the elapsed time
is larger than the debounce time
remember: if this isn't the case 
we don't have to look any further
because we only want our touch sensor 
to be triggered once every debounce time
so then we're going to
check the "ledState" if it was HIGH
we make it LOW or else we make it HIGH
so this toggles the LED
and then we write this new "ledState" 
to the LED_BUILTIN pin
so it goes ON or OFF depending
on the "ledState"
here on line 44 or 46
and if so we store the "previousMillis"
sorry the "currentMillis" in the "previousMillis"
and we do this, so that this calculation here 
with the "elapsedMillis"
here is the "previousMillis" is is the same
as this one, so we can see that there is
enough time elapsed when we want to
calculate this condition
I'm now going to upload this code to the Arduino
and show you what it looks like
I've uploaded the debounce code to my Arduino
and as you can see the LED is off
when I press gently on the touch sensor and
wait for a second you see the LED goes
on and off
because of our "debounce" there is only one possibility to change every second
if I tap quickly you still see the LED
is changing every second
so the debounce prevents that we
change the LED continuously and will blink
if we increase the debounce time
to for example 10 seconds you can only
turn it on and off every 10 seconds
this concludes our tutorial about the
touch sensor
you've learned how to read the state of the touch sensor and how to add debounce to the sensor
if you have any questions 
feel free to drop them in the comments
and for the circuit diagram
and the course material there's a link
in the description below this video
I see you in the next episode!
