in this tutorial I'm going to teach you
how to read a capacitive soil moisture sensor
for this tutorial you need an Arduino
a capacitive soil moisture sensor
a sensor cable, 3 jumper wires male-male
and a glass of water
before we're going to build the circuit
I would like to show you how our moist sensor works
one of the sensors used is this one
it consists of two pins and 
you see also some kind of metal on top of it
what this sensor does is it measures the resistance between these two pins
how this works is that water changes the resistance
and so you can measure the amount of moist in our soil
problem is that fertiliser also changes
the resistance
so you cannot be absolutely sure that 
you're measuring the water
another problem is corrosion
this is a sensor used for some time
what happens is that the DC current reacts with water
and make this sensor oxidise so
as you can see you can throw away this sensor
because it's completely damaged
therefore I do not recommend to use this sensor
our sensor works in a different way
it works like a capacitor
so this is called a capacitive sensor
on this image you see v1.2 but you can also use v2.0 
for this tutorial
this sensor works in a different way
what happens is that since our sensor
is working like a capacitor
the water level changes the capacity
so the nice thing is that we can measure the 
charge and discharge timing
so what this looks in a schematic 
is that on the top left we have some kind of pulse
and this pulse goes into our circuits to our capacitor
so when the current flows through
our capacitor is charging and eventually
discharging
and this is what we can measure with our timer
the trick here is that the water affects
the capacitor capacity
when there is more, or less water
the amount of time it takes to 
charge and discharge the capacitor changes
and that's what we can measure
this is all embedded in our sensor and
if you look closely you see this little chip
this chip is called a 555 timer chip
and that chip helps with the timing things of our sensor
eventually on the analog out there is
the signal we want to measure with our Arduino
and how that works is what I'm going to show you now by first building the circuit
the first step is to connect the signal cable
with our jumper wires
I've connected the red wire to the red jumper wire
the black one to the black jumper wire
and the yellow one to the green jumper wire
we continue to connect the sensor wire to our sensor
our last step is to connect the sensor with our Arduino
we connect the red jumper wire to the 5V,
the black one to the GND
the green one to analog zero (A0)
the code to read out the sensor is pretty straightforward
as you can see who on line 19 I initialised
 the serial monitor with 9600 baud
that is 9600 characters per second
the loop is also very straightforward
read the value of A0 and print it to the serial monitor
then we wait for 100ms, and we start all over again
what I'm going to do is upload the sketch to my Arduino and open the serial monitor
and when it opens you see a value
and my sensor is now not in the water
so it's dry
and it shows a value of 583-582
so when I put my sensor in the water
you see that the value will drop
it takes some time but eventually it will drop very low
so what we're now
going to do in the second exercise is to
translate these values to a percentage
of moist in the soil
in order to determine the percentage of
moist in our soil we need to change the code a little bit
on line 17 and 18 I've defined two constants 
one called "dry" and one called "wet"
these two values are used to measure 
the total range of our sensor
so my sensor is still in the water
so I'm going to open the serial monitor and
the previous sketch is still loaded and
what I see here is that the value is 240
239 I see here
so I'm going to adjust my "wet" value to 239
then I take my sensor out of the water make it dry
then we open the serial monitor again to see its value
and this helps us to determine this full range
I expect a value around 580 so I open the serial monitor again
and what I see is even higher
but it's still kind of adjusting
because I see it going lower to 594
it's kinda it's now 596, 595
so I enter 595 and I can close the serial monitor
now we have the correct values for a
sensor
I didn't change much at this setup()
it's still the same setup()
but I've changed the loop()
so what we do is, I've defined this integer
called sensorVal to store the sensor value
and I do an analogRead() on A0
to which a sensor is connected
I need to change this to make the story consistent
so our sensor has a range of 239 to 595
and we want to translate this into a percentage 
from 0 to 100
there's a function for that 
our Arduino supports a function called map()
what map() does is it translates a certain 
range of numbers to another range of numbers
and more info is here on this website
what we have here is a sensor value
which we've measured here
and the range for our sensorValue is
between "wet" which is 239,
and "dry" which is 595
we want to translate this to a range of 100 to 0
because "wet" is 100% humidity
 and "dry" is 0% humidity
the result is this translated value
and that's a new variable "percentageHumidity"
which we print to the serial monitor 
and as you can see I'm not using println()
because println() adds a new line
at the end of what we print
and what we want is the value
with a percentage so this one is println()
because then we have a value
percentage sign and then a new line
then we wait for a 100ms
and we start all over again
so what we're now going to do is 
upload this sketch to the Arduino
my sensor is still dry so I expect a value
somewhere around the 0
here it goes, and it says 0-1%
still analog so it might change a little bit
now I'm going to put my sensor in the water
as you can see it slightly increases
it's not going immediately because of the timing, the capacitor and everything
so you have to wait a little bit but you can trust me 
that it will reach around 100%
and while the numbers are going up
I conclude this lesson
because you now know why you shouldn't
use a moist sensor which is using resistance
you know that it will damage the sensor
you know that we're using a capacitive
sensor and how it works and how it acts as a capacitor
you also know how to use the map() function 
to translate one range to another
so that's also very useful for future projects
in the description of this video there's
a link to the course material
the course material contains the code, 
the circuit diagram
also a shopping list with all the parts used in this video
if you have any questions drop them in the comments
and I see you in the next episode!
