in this lesson we are going to use this moisture sensor to check the level of
moisture in The Soil and Water our plant I'm not going to turn on the actual water because
it will be very messy because we have already had a water pump lesson please refer to that
listen here I'm just using a buzzer and we are going to test it for pin when there is enough
width and width 780 analog 0 to the motor driver then from this positive which says
12 volts we are so let me just remove it still there is moisture I will dry it up
this course is based on SunFounder three in one Arduino kit sold on Amazon everything
that you learn in this course is included in this kit the course comes with tons of
components and projects all with instruction and video for smart car and other projects that
you can use for school or home automation you can purchase it from sunfounder.com
the kit also comes with full documentation code download learning projects with full
explanation wiring diagram and the code that you can download a link to purchase the kit
and documentation is below the video in the description welcome to Arduino course by Robojax
and in this lesson now we are going to use this moisture sensor if we go to a learning project
analog read and measure soil moisture 4.4 the social sensor it has three pins one is ground
the other is VCC which will be connected to 5 volts and then the output will be connected to
analog 0 because we are reading analog depending on the moisture amount we will have some value
and we can set our moisture level and then based on the amount we can take action so a pump can be
turned on or a relay or light whatever you have so because this is moisture sensor perhaps if the
moisture is too much maybe you turn on a fan so it can remove the moisture or maybe it can turn on
the pump to bring water and here is the moisture sensor this is just an operational amplifier and
there is a capacitive measurement there is one pin of One path here at the middle and there
is another part surrounding it so the distance between these two is an empty spot where the
moisture will affect the capacitive charge between the two the paths it is enough moisture between
these two paths the capacity of the capacitor will change and we are amplifying it using this
op-amp as a voltage and we are measuring it using Arduino to take action so when it is
dry we get one value at the output when it when it is moist the output will change a basal plant
these smells good I'm going to measure the moisture
now let's open the project by clicking on file open to wherever you download it in my case
it's un founder downloads and founder three-in-one kit learning project and 4.4 measure soil moisture
double click and open the file so the code is very simple we initialize the serial monitor
with 9600 bot inside the setup that's done and then inside the loops we use analog read and then
we use another agree to read analog zero pin the result is stored as an integer which is between
0 to 1023 and then we use serial print Ln this value is printed on the serial Monitor and we have
10 millisecond delay so this will give you just a vague value which
I'm going to experiment in the next section of this tutorial
here is a wiring diagram ground is connected to ground VCC is
connected to 5 volts and a out or analog out is connected to zero
to connect the sensor because the connector is like that I've used three male to male this side
is male and the other side is as well male here is a wiring black is black red I've connected to
a red yellow is blue because in the diagram it was showing us blue this black is connected to ground
red is connected to 5 volts this yellow is now blue and blue is connected to analog 0.
now I have here water and
the code is loaded, and we are constantly reading the value
it shows around 940 and I'm immersing it in water and let's see what we did
now it's about 890 870 as you can see as the time passes it's being reduced 840 830 820 800
780
so, let's just wait and if I remove it back it goes to 800. so, we can see that have immersed
it in water and we are reading let me just dry it up you see it's already dried up
when you're reversing it in water be careful this these components will die so I have enough
water here up to a certain point that even if I immerse it fully it doesn't go beyond this point
so now when I remove it as you can see the number is very HIGH
which means when it is dry the number is HIGH when it is with the number is LOW
so first we have to learn this and come up with something, so we are you have to
experiment and find out what value is considered dry and what value is what
these smells good I'm gonna insert this
into the soil as you can see, we are reading our 880 and let's insert it
to show you I've inverted it that much
and as you can see, we are reading around 800 and it is decreasing 790
oh, these smells good
to 790 or 780 now you will touch the soil and you see you will come up with a number that which
number represents with and which number represents dry for this purpose I'm gonna pour some water in
that area see what happens but okay now there is enough water now 750 so Lower numbers means with
now it's fully wet with water that's why we are reading around 7 40.
let's because if I wait, I'm it might decrease let's not worry let me just remove it
so, 850 so it will go up to 900 if I dry it up or clean it
so now let's go to another spot which I have not watered recently it was maybe this morning or so
I've just inserted it
so seven fifty-Seven forty-eight forty so it's being reduced as the time passes
this is around 8 20. now when it is around 8 20 you can turn on a water pump to water
it up when it reaches for example uh 700 50 you can stop the water pump
now because we know the values that it works now we are going to insert it in soil and also
turn on a buzzer and I'm going to show you how to control the water pump from a previous project I
will show you the code and here is our code I've defined an alarm pin of type integer with a value
2 this is where our pump or alarm or buzzer is connected then we are reading the moisture pin
moisture per analog 0 we have two values when the value is greater than 860 it's we defined
it as dry value and any value below 750 is our wet value so you can experiment with
this to find out which value works for your environment or the soil and then we have a
variable called watering with a initial value of false so this is just holding the state
of watering and inside the setup we use pin mode and alarm pin is set as an output so we
can control it as an output and send signal to the buzzer or relay or whatever device we have
inside the loop we use analog reads and moisture pin moisture pin so
moisture noise
so, let's change it to moisture pen
using analog read we need moisture pin and then the value will be stored in sensor value
after that we use let me show you we use this if sensor value is greater than drive value
and here it says and watering is not this means not true so currently we are not watering then
we say Digital Light alarm pin HIGH so this will turn on the water pump or alarm and
then we update the variable well with the value true because we have turned on the
watering so we have to say watering equal through then we print this text watering
then we say if the sensor value is smaller than watering and watering is true then
we send the pin we turn the pin off and we also set the value of watering to off here it was on
now we turned it off and then we print this sensor with space watering stopped and here
then if watering is not true which means we are not watering then we say the value we set
the value growing and we put the value and we type this the word growing here
and after one second everything will be repeated again
now here is a demonstration as you can see the value is 730 which means it's below 750 it's too
wet so the pump is stopped at the moment but if you want to do if the if it is below 750 we can
also turn on but for us it's just watering so it's working at the moment the sensor is in here let
me just remove it has dirtier okay so let me just remove it still there is moisture I will dry it up
as soon as I dry it up the value is 862 and as you can see it says watering now the pump can
be connected instead of this buzzer we could have a pump or relay that is controlling a
water pump or a pump that we just shown in previous lessons so the water the water is
watering it and until it reaches back to wet now I'm just immersing it this area is very wet
so, when it senses that it is wet
just wait and when it goes below 750 the water will whether pump will stop
so, to make it faster I'm gonna drop some water here
wait for it don't worry
so, after the 745 So Below 750 and as you can see now uh the pump is stopped now even if the number
goes HIGH the pump will not turn on because the pump will turn on only when it goes above 860.
so you see a 750 751 now the pump is not working it has to reach h60 because we don't want to turn
on and off the pump anytime every time when the number changes we want it when it reaches
at this drying level then it will turn on this is a small water pump where we
can connect a pipe here so it can suck the water from the bottom and pump it via this
uh pipe hole to wherever we want it there this is a pipe that we can connect it
like that to be secure and this will be connected to the motor driver
so the same as before this needs HIGH power so we send very weak signal from Arduino to the motor
driver and motor driver will send strong signal to control the rotation of this pump or motor
here is the wiring if I click on 1.4 this is pumping,
we are connecting exactly the same as before you see the same l29 is n and the motor is
connected exactly the same way wiring is the same way so there is no difference
and here the two wires of n304 are connected to the same point
everything is the same except we are just replacing the wheel motor to pump motor
for the water pump project all the wiring is exactly the same except we replace the wheel
driver with a water pump and make sure to connect the red on this side and black on this side
now let me show you how we can combine the pump
control code with this soil moisture sensor code let's go and open it
1.4 pumping
and then open it
so, this is
this is one could so this is the soil measurement code, and this is the
pumping code I also let's save it to another project save us I'm just adding pump to it
top two pin I've been defined here I'm copying them and pasting it here that now
inside the setup we just need to copy the pin definition I'm copying it and pasting it here
so this is for our pin pen a and pin B which we have defined by 9 and 10.
inside the loop under this condition where it says watering now let's just put this so this is now
turning one pin HIGH one pin LOW and the pump will run and to turn it off we
turn off both in this line you can see this line is for turning it off just put it below this
and make this also LOW so this will keep both of them off both pin off and motor will stop