Water Your Garden with IoT - Soil Moisture Sensors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we are using soil moisture sensors we'll learn how these sensors work how to calibrate them and how to use them with microcontrollers we'll also build a soil moisture meter and an automated watering system that you can control with the arduino iot cloud we're growing something special today so welcome to the workshop [Music] well hello and welcome to the workshop and today we're working with soil moisture sensors which you can probably guess from their name or devices that you place into the ground and they'll measure the amount of water content in the soil now we're going to learn about how these sensors work we're going to calibrate a few of them and then we're going to build a couple of projects the first project is a soil moisture meter with an oled display and our second project is an automated watering system that's going to be using the arduino iot cloud so that we can water and maintain our gardens from pretty well anywhere in the world now before we go on with our lessons and our projects there is a bit of a disclaimer i have to give now there are some people out there perhaps you're one of them who are just wonderful with gardening they can grow beautiful crops of vegetables they can make gorgeous flowers they're just whizzes in the garden and we call these people people with a green thumb well if you look on the color wheel the opposite of green is red and so i probably have a red thumb because plants do not do well with me i've never been good at gardening i've even managed to kill plastic plants so i'm not the kind of person who can dispense advice about gardening to you i can however dispense advice about technology so let me dispense some right now by telling you how soil moisture sensors work soil and moisture sensors have applications in agriculture irrigation research and just plain gardening some advanced soil moisture sensors use the property of water to alter microwaves and they use techniques such as frequency domain reflectometry and time domain reflectometry a very advanced method of measuring soil moisture is neutron scattering which uses the moderator properties of water on neutrons between a source and a sensor the soil itself can also act as a galvanic cell this allows the construction of a soil moisture detector that does not require its own power source the resistance and capacitance properties of soil can also be used to measure soil moisture today we'll be focusing on inexpensive sensors designed for microcontrollers and microcomputers these sensors are based on either the resistance or capacitive properties of the soil these sensors can be run in multiple modes in analog mode the resistance or voltage output will change in response to the amount of moisture in the soil in digital mode a set point is established and the output will go either high or low depending on whether the moisture is above or below that set point in addition to analog outputs there are also serial and i2c devices available and we'll be taking a look at an i2c device later on a common application for soil and moisture sensors is an automated watering system and we'll be putting one of these together later on in the video in this sort of system a soil moisture sensor is connected to the input of a microcontroller it's also typical to have some sort of a readout of soil moisture as well you will also connect the pump or other source of water to the output of the microcontroller if the sensor detects the moisture dropping below a certain point it will activate the watering system and water the soil once the moisture has gone above a certain threshold the watering will stop resistive soil moisture sensors depend upon the resistance property of soil which decreases with the amount of moisture in it these are very simple to design and quite easy to manufacture however they have one major drawback in that corrosion of the probes can alter its resistive properties with a capacitive soil moisture sensor the soil itself serves as the dialect within a capacitor these sensors have a timer on them that measure the amount of time that this capacitor takes to discharge because we're measuring soil capacitance we don't require any direct contact to an electrode so the electrode can be insulated and this eliminates the corrosion effect that occurs with resistive sensors resistive sensors have an advantage in that they are a lower cost capacitive sensors however are longer lasting and generally provide better performance as these sensors both provide analog outputs they can both be used in our designs so you can use any sensor you happen to have with the circuits you're about to see so now let's take a look at a few soil moisture sensors now here are some soil moisture sensors for you to take a look at we're going to be using a few of these later on and some experiments three of these are capacitive sensors and one of them is resistive and you can sort of tell which one the resistive one is it's this one here and it has these two electrodes and what we're actually doing is measuring the resistance between the electrodes so when that's inserted in soil the soil will provide an electrical resistance and that's what's being measured in these the rest of these are capacitive sensors so you don't actually see any electrodes they've all been nicely sealed up and protected from the elements so there's nothing going to corrode because of course a capacitive sensor works on a different principle in this case the soil becomes a dialect in the capacitor now this is a very standard and very common capacitive soil moisture sensor and these ones are quite inexpensive and it has an analog output these other two have i2c outputs this one is made by a company called catnip electronics and it's a very interesting little sensor it also measures temperature and light one other thing about it though is it didn't have a connector on it so i have to solder these jumper wires onto the output here but this has an i2c output so i've got power ground sda and scl coming out of here this is also another i2c sensor and this one is called a seesaw and it's made by adafruit it does have a little connector on it so it's a little bit easier to use and these other ones also have connectors as well but there you go a few soil moisture sensors that we can be using in our experiments now before we put our soil moisture sensors to use we need to calibrate them it doesn't matter whether they're resistive or capacitive each sensor has its own unique properties and even if you get a batch of them you'll find that no two of them are exactly alike now calibrating the sensor is pretty easy all you need to do is take a measurement of its output when it's completely dry and then take its output measurement when it's completely wet and since resistive and capacitive sensors both output a voltage in response to the amount of soil moisture we can use the same circuit and the same sketch to calibrate both of them now i'm going to be doing this with a raspberry pi pico it's an inexpensive micro controller and one of its advantages is that it has a 12 bit analog to digital converter so we can get a very accurate reading from it now i'm also going to be using the classic arduino ide 1.8 for these experiments and if you haven't used the pico yet with the arduino ide 1.8 i urge you to go and take a look at that monster size video that i just did for the 10th anniversary of the raspberry pi you don't have to watch the whole video but go to the third project you can use a table of contents in the video to go there directly or use the chapters feature on the youtube video the third project was a raspberry pi pico simon game and i show you exactly how you set up the arduino ide to use the pico so i'm going to assume you already know how to do that now if you don't want to use a pico i'm also going to show you how you can use these calibration with a arduino uno and it's basically the same sketch with one modification and i'll show you the hookup for the uno as well so you don't necessarily have to use a picot although you will get more resolution so let's go and take a look at how we hook up the pico and then i'll show you the sketch that we're going to be using to calibrate both our resistive and capacitive soil moisture sensors for our experiments we're going to be using a raspberry pi pico microcontroller however you could use a different microcontroller and i'll show you how to do the same experiments with an arduino uno a little bit later we will also of course need a soil moisture sensor you could use a resistive sensor or a capacitive sensor because the hookup and the code are identical for both i'll be using a capacitive sensor in this illustration but again the hookup for the resistive sensor is identical we'll start by connecting the analog ground pin of the raspberry pi pico to the ground of the soil sensor you could also use another ground pin if you wish but analog ground is the specific reference for the analog to digital converter and will produce the best results we use the pico's 3.3 volt output and connect it to the vcc of the soil moisture sensor and analog to digital converter 0 input which is also gp26 will be connected to the output of the soil moisture sensor and this completes our wiring now let's take a look at some code we can use to calibrate our sensor now here's the sketch that we're going to be using to calibrate our sensors and we can use this with both capacitive and resistive sensors and it's an extremely simple sketch we start off by defining a value that we're going to be using to get the sensor value and this will be the output of the analog to digital converter and then we define the analog to digital converter pin itself which is adc 0. then we'll open up our serial monitor so we can see our results and we're also going to set our analog to digital converter resolution to 12 bits if we don't do this it'll default to being 10 bits and we want to get all the resolution we can and then in the loop it's extremely simple all we do is we use an analog read to read the value from the sensor pin and assign it to sensorval and then we just print that to the serial monitor we add a very short delay and we continue to do that and so a super simple sketch let's try it out with both capacitive and resistive sensors so we're going to start off by calibrating the capacitive sensor which i've got hooked up to the pico right now and as you can see in the serial monitor i'm getting a reading and it's hovering around 2500 25 23 25 18. this will be my dry reading and i want to record that because this is what i'm getting at what should be about zero percent moisture now i'm going to stick it into this glass of water and you'll see that the reading went down it's going down about 1340 1350 this will be the reading that i'm going to get for my wet reading and so you'll also want to calibrate that so those are the two different extremes for the sensor and we'll be using those calibration values in further experiments okay now we're working with the resistive sensor and the first thing you'll notice on the serial monitor is that the dry reading is a very very low value it's down around 20 it even approaches zero at times and when i put it into the water you'll notice that the wet reading is very high and there's quite a discrepancy between the two and with that kind of a range it means that this type of soil moisture sensor is actually more sensitive to small differences in soil moisture so in some ways it's a bit more accurate however of course the resistive ones do have a problem in which the probes will eventually start to corrode and the values will start changing they'll go out of calibration but as you can see we can use the same circuit to calibrate both resistive and capacitive soil moisture sensors now you could also use an arduino uno to perform these experiments again i'm showing a capacitive soil sensor but you could use a resistive sensor as well as the hookup is identical we'll start by hooking the ground of the soil sensor to the arduino's ground the vcc of the soil moisture sensor will be connected to the 3.3 volt output of the arduino uno the output of the moisture sensor will be connected to analog pin a0 and you'll also want to connect the a ref or analog reference pin on the arduino uno to the 3.3 volt output now let me show you the changes you need to make in the code in order to use an arduino uno now there's only one change you're going to need to make in the code to run this with an arduino uno and it's down here in the setup this command analog read resolution is not going to compile with an arduino uno because the arduino uno only has a 10 bit analog to digital converter at the same time if you remember in our wiring we've wired the a ref or analog reference pin to 3.3 volts so we need to use that so change analog read resolution to analog reference and make this value here external like this and that way you'll use the 3.3 volt power supply that we've fed into the are pin and you also won't have an analog read resolution that was set there so obviously this line is incorrect right now but that's the only change you need to make and you can run the code on an arduino uno now when i showed you all of the different soil moisture sensors that i've collected i showed you two models that were using an i2c interface now these are capacitive soil moisture sensors and so they work in a similar fashion to the capacitive sensor we've already worked with but of course their output is different they are i2c and therefore they can be used with just about any microcontroller or a microcomputer that supports i2c now one of the models i showed you was from a company called catnip electronics and it's a very interesting sensor not only does it give you soil moisture readings it also outputs temperature and light level readings and it's quite easy to use as long as you use a library with it so we're going to take an arduino uno and my catnip electronics i2c sensor and see how we put them to use for our i2c experiment we'll be using an arduino uno we'll also require a catnip electronics i2c moisture sensor and you'll need to solder some wire onto the connections in order to make use of it we'll begin by connecting the ground connection on the moisture sensor to one of the grounds on the arduino we'll connect the moisture sensor's sda output to analog pin a4 on the arduino we'll connect the scl input to analog pin a5 and the vcc pin on the catnip electronics i2c moisture sensor will be connected to the arduino's 5 volt output and this completes our wiring now let's take a look at some code we can use with the moisture sensor now the i2c soil moisture sensor that we're going to be using is a great example of some of the electronics that we're seeing from all over the world this was built by a company called catnip electronics and they're in lithuania now you can get this off of tindy where they sell it directly and as you can see it's not particularly expensive but i actually got mine from mouser electronics and so you have that as an option as well now wherever you happen to get it from you're going to need a library in order to use it properly and there are a lot of great examples up on github for doing that there are also python examples up on github so you could use it with python or micropython use it with a different microcontroller or perhaps with a raspberry pi but we're going to be using it with an arduino uno so we're going to get some arduino code and here's where you can grab a library on github you just hit code and do download zip and you'll get the library in a zip in a zip format now you'll need to install that into your arduino ide which i've just done but i'll show you how you do it you just go under sketch you go include library and then from include library you do add zip library you navigate to where your downloaded your files and here it is the i2c soil moisture sensor you would hit ok but i've already done that and now you've installed the library there's also an example sketch that we can use in order to work with it so just go into file go into examples go down to examples from custom libraries and you'll see i2c soil moisture sensor and you've got two of them one that allows you to change the i2c address on the device the other one called read sensor data which is what we're going to do and so i'll just expand that so we can see it and it's a pretty simple sketch really it includes the library that we just installed and also includes the wire library of course because we'll need that for i2c and it defines something called uh sensor and uh we go into the setup we'll start the wire library we'll put our serial monitor on and we'll reset the sensor with the sensor dot begin we'll give it some time to boot up for a second and then we'll print out to the serial monitor the address of the soil moisture sensor and its firmware version so we'll use a get address and a get firmware get virgin excuse me to show the firmware version of it and then we just go into the loop and we wait if the sensor is busy and then after that we'll print out the capacitance value so we use get capacitance to read that and print it out to the serial monitor this also reads temperature and light so we'll get the temperature and we'll get the light and these are just simple commands that are made possible because of the library and these are just printed out to the serial monitor so you could see how you would use these in your own sketches so this is a really cool little sensor and with this library it should be pretty easy to use it so let's just load up the code and take a look at it in action and so we're going to do a quick demo of the i2c moisture sensor and if you look at the serial monitor you can see that we're getting capacitance temperature and light readings as well too and so those could also i suppose be useful for your gardening needs now this is the dry reading that we're going to get now remember this takes about a second or so before it gets the reading so it's not going to be instantaneous i'm going to put it in here and there you can see now that the capacitance reading has changed the temperature and light readings have changed quite a bit as well too and so i'm not sure how you interpret the light values the temperature is in degrees celsius and i think it's a little bit off because i don't think the water is really that warm and the capacitance is really of course what we're looking for this is the capacitance when it's wet and i'll remove it again and dry it off and we'll wait a second or so and it drops down around 180 785 so uh once again this is a useful soil moisture sensor and because it's an i2c device you can use this with microcontrollers and microcomputers that don't happen to have an analog input so now that we've seen how soil moisture sensors work and we've calibrated a few of our own sensors it's time to put them to use in the first project we're going to build is a soil moisture meter now this meter has an oled display that will display the percentage of moisture in the soil it is based around the raspberry pi pico now you're going to need the calibration values you took for your soil moisture sensor in order to complete this job so if you haven't done that yet go back and calibrate your sensor first and after you've done that let me show you how you wire up the soil moisture meter we'll be constructing our soil moisture meter with a raspberry pi pico we'll also be using an oled display a standard ssd 1306 display with an i2c input and of course will require a soil moisture sensor again you could use a resistive or a capacitive sensor and i'm illustrating a capacitive sensor here the connection for the soil moisture sensor is identical to the ones we used for calibration we'll start off by connecting the ground of the soil moisture sensor to the analog ground or agnd pin on the raspberry pi pico the vcc connection on the moisture sensor will go to the picos 3.3 volt output and the output of the soil moisture sensor will go to analog to digital converter 0 input on the raspberry pi pico this is also gp26 we'll connect the ground of our oled display to any of the grounds on the raspberry pi pico you can identify the ground pins on the pico as they have square pads we'll connect the vcc of the oled display to the 3.3 volt output of the raspberry pi pico the scl or clock pin on the oled display will be connected to pico pin gp5 and the sda or data pin on the oled display will be connected to pico pin gp4 and this completes our wiring now here's a sketch that we're going to be using for our soil moisture meter and it's been written to use capacitive sensors however you could rewrite it for resistive sensors with one simple change which i'll show you in a moment now we start off by including some libraries that we're going to need to drive our oled display we need the wire library because that's the library for i2c and that's already included in your arduino ide the next couple of libraries are libraries from adafruit and if you don't have them go into your library manager and look for ssd 1306 install the adafruit library and it'll also install this other library for you however if you've already played with oled displays there's a good chance you have these libraries already now we're going to go and set some parameters for the oled display so we set its height and its width and we set some other parameters up as well this reset as negative one just needs to be set so we know that we're not using a separate line for reset and the screen address and this is the i2c address now if you happen to have a display that has been set to a different i2c address you will of course need to change that then we define an object called display by passing it all of the different parameters for the oled and then we go and we put in our sensor constants now these are the values that we determined from the calibration sketch that we ran earlier so your values could very well be different than mine you could start off with these and experiment with them but the best way to do it is just to calibrate your sensor using the sketch that we looked at previously and then plug the values in over here we set up a couple of variables we're going to need for the soil moisture we're going to get the soil moisture value which is actually the value that we get from the analog the digital converter output and we're also going to get the value and percentage which is what we want to display in our display and then we define the port that we're using which again is adc0 for the input from the sensor in our setup we're going to set up the serial monitor and we're just using the serial monitor for troubleshooting and also so we can fine tune those wet values and drive values up over here we might want to change them and the serial monitor will help us do that we're going to initialize our display and an important thing when we initialize it is this switch cap vcc parameter that we use when we initialize it if we don't use it if we just use ssd 1306 it's going to expect the 5 volt power supply but in this case it'll know it's getting a 3.3 volt power supply so you need to plug that in and then the screen address itself then we're going to clear our display and once again we'll set the analog to digital converter to use 12 bits with an analog read resolution function then we go into the loop and in the loop just as we did with the calibration sketch we're going to get the value from the sensor assign that to the soil moisture value and we'll print it out to the serial monitor and then we'll go and we're going to determine the percentage and we do that with a map command and we take the soil moisture value and we map it between the drive value and the wet value to a value of 0 to 100 to get the percentage now i said earlier that you would possibly be able to use this with a resistive sensor and you can what you need to do is just reverse the order of dry value and wet value because as we saw the values work in the opposite direction for resistive sensors now it's possible these values could drop below 0 or above 100 if we get values that go beyond the drive value or the wet value and that can happen if the calibration isn't perfect and so we're going to use a constrain command to constrain our results between zero and one hundred so if it goes over a hundred it'll just read a hundred and if it goes below zero it'll just read zero and you can use a serial monitor to fine tune things so this doesn't occur very often and we print that out to the serial monitor the percentage we've had and then we just go and print the oled display so we set the cursor on the oled we set the text size to two and the detects color to white now the top of my display is yellow but it doesn't matter you use white all the time as the color when you're writing to an oled display and then we print the word moister and that's going to print on the top of the display in the case of my display it's going to be yellow but your display may be just one single color it might be two different colors it doesn't matter and then we go and we move our cursor down again set our text size even bigger and again set the color to white then we'll print that soil moisture percentage value will print the percent symbol behind it and then at the very end of that we will do a display display which actually puts everything onto the oled because nothing is written until we do that we're just basically filling a buffer over here we'll delay for a quarter of a second and then we'll clear the display so we can go back and get the next reading and that's basically the entire sketch just load it up to your pico and take a look at the results and so here we have our soil moisture sensor on this small sawdust breadboard and i've got a couple of things to test it with over here now it's reading zero percent because the sensor is just uh loose in the air let's just drop it into the water here and as you can see it goes up to a hundred percent right now now by the way you might be seeing some flickering on this display the flickering is just because of the cameras it's an effect that they are making it probably has something to do with the frame rate or something because there is no actual flickering that you can visibly see i just wanted to point that out let's put it into some soil right now just move these wires a bit so we can see it and this isn't particularly moist soil let's see if i can find a moist spot i don't know there's a moisture area there we go i had dropped some water into it it went to one side so it is indeed measuring the moisture in the soil and so this is actually a cool little project and you could build it uh into its own little enclosure and give it a power supply if you wanted to and actually make some sort of a useful instrument out of it now at the beginning of this video i illustrated a pretty classic use for a soil moisture sensor and a microcontroller essentially you had the soil moisture sensor attached to the microcontroller and the microcontroller was also attached to a pump or another method of dispensing water when the microcontroller had detected that the soil moisture had dropped below a certain threshold it would activate the pump which in turn would add moisture to the soil and when the moisture went above that threshold then it would turn off the pump but i'm sure you've seen about a dozen different systems for doing exactly that well we're going to build something like that right now but it's got a bit of a difference my design is based around an arduino nano 33 iot board and it also makes use of the power of the arduino iot cloud and with this we can make a cloud-based system that we can monitor using our phones or tablets or our computer pretty well from anywhere in the world now i'm going to make the assumption that you already know how to use the arduino iot cloud but if you don't i've already done a video on that and so there's a video and article that you can check out that'll bring you up to speed right away so you'll understand some of the terminology that i'm using before i show you how we wire this up let me show you some of the features of our cloud-based watering system now here's my automated watering system assembled on a solderless breadboard as you can see it's got an oled display that cycles through a number of different values a temperature and humidity sensor a relay to drive the pump and of course the arduino nano 33 iot now if we take a look on the control panel for this you can see that i'm displaying the moisture i'm also displaying the moisture over a period of time and this is the live view but i can go back an hour a day a week or even 15 days so you can see how the moisture has changed in your soil over a period of time i've got a live temperature and humidity display and i'm also displaying the status of the pump and right now it's green to indicate that my pump is on and that's because i've got the trigger level set to 40 but i can adjust the trigger level over here so let's bring it down to 20 which is below the 30 percent moisture so we have to give this a few seconds for it to react and there you go the pump status is now changed to red indicating that the pump is off and so now let me show you how you can wire up this project to build our iot watering system we'll be using an arduino nano 33 iot board you could probably also use an arduino nano rp2040 connect board and they're pin for pin compatible we'll be using a capacitive moisture sensor and the dht22 temperature and humidity sensor now you may wish to use a dht21 as it's more suitable for outdoor applications you could also use a dht11 if you wish and i'll show you where you can make some changes in the code to use one of those other sensors we're going to be using the same oled display that we used when we constructed our moisture meter and we'll be using a 5 volt relay module now you'll also need a pump and you'll need a power supply to drive that pump i would strongly suggest using a low voltage dc pump we'll begin by connecting pin 1 of the dht 22 to the nano's 3.3 volt output pin we'll connect pin two of the d8t 22 to nano pin d8 and pin 4 of our dht 22 will be connected to one of the nano's ground pins the moisture sensor's output pin will be connected to the nano's pin a0 the moisture sensor's vcc will be connected to the nano's 3.3 volt output and the ground from the moisture sensor will be connected to one of the nano's grounds the oled's ground will also be connected to one of the nano's grounds the oled vcc will go to the nano 3.3 volt output the scl connection from the oled will be connected to the nano's scl pin which is also address pin a5 but note that on the nano it's not usable as an analog pin because it has an internal pull up resistor the same goes for the oled's sda pin which is connected to the nano's sda pin which is also a4 the relay module's g or ground pin is connected to the nano ground the relay module's 5v pin is connected to the nano's 5 volt output and the module's input pin is connected to nano pin d3 you'll also want to run the power supply for your pump to the common output on the relay the relays normally open output will be connected to one side of the pump and the other side of the pump will be connected to the pump's power supply and this completes our wiring now let's go into the arduino iot cloud and see how we can build this project now we begin our project up on the arduino iot cloud and as i said earlier if you're not familiar with the iot cloud i strongly suggest you check out the video and article that i did about using the iot cloud to get yourself up to speed but essentially when you're building for the arduino iot cloud the main thing that you're building is called a thing it's after all the internet of things and this is the thing that i am building and called it auto water and the thing has three different components to it it has variables it has a device and it has a network now my device is the arduino nano 33 iot board that i've already registered with arduino and my network is of course my own wi-fi network and so i've got the credentials for my network over here and then we have our variables and these are the variables that we have for my project and there are actually five variables for it the current moisture the current temperature the current humidity the pump status the status of the pump whether it's on or off and a trigger level which is the level that we are going to tell it to trigger and turn the pump on so this is the percentage of moisture level now i'll show you some of the variables if you go into current moisture for example you'll see that it's an integer and i've set it to read only and if we go into the current temperature we'll see that it is a float and it is also set to read only and it sends the values on change and that's actually one thing about current moisture if i go back to it that i have set i set it to go on change and that means anytime the moisture value changes it'll send something up to the cloud and it'll update my dashboard now since i'm using that graph on the dashboard if i keep it let's say at zero percent or a hundred percent for a long time the graph won't change so you could actually go into the variable if you wanted to and you could edit that and instead of doing on change you could say periodically and give it a time period and that might be a good thing to do if you wanted the graph to be more constant so that's just something you may want to change in yours current humidity is the same as a temperature it's a floating variable it's a read only the pump status is also read only and it's a boolean and the trigger level is a bit different it's an integer but it's got read and write permissions and that's because i'm using a control on the dashboard in order to set the trigger level so i have to be able to both read and write that value now if we go back onto my dashboard you'll see all the various components that i have the moisture and right now the sensor is completely out of the water so it's dry it's down at 20 percent and here is the graph of everything and you notice the graph actually hasn't updated itself for a little while because it's been down at 20 percent for a long time and as i said if you made that work periodically would see the graph update a lot if i end up putting the probe into the water and changing that moisture all of a sudden the graph should come back to life and as i said earlier there's a bit of a latency over here so it's actually already happened the pump has turned itself off but we don't see it here right away and there it goes we see it right now and you see that whole period of time where i didn't get a reading because it was constantly at zero and now it'll stay at a hundred until they take it out of the water so again you may want to change that to trigger periodically instead of on change let me show you how these are done on the dashboard so i'll go into edit mode we go to moisture and we can go and edit the settings and the moisture i've got it linked to the current moisture from auto water so if i wanted to do that initially what i would do is i would go in here and get a list of my things i'd pick auto water and it would give me all of the potential ones that it would link to and notice for example pump status has been grayed out and that's because that is a boolean and it won't work for this kind of thing so it will just look at the variables that are applicable in this particular case it was current moisture i'm actually not going to change anything so i'm going to set it to done in order to use the graph the graph is very simple it's exactly the same actually you just set it to link to current moisture there's really nothing else you have to do to the graph because all of the heavy lifting so to speak has been done by the iot cloud to make that neat display temperature and humidity as you can imagine are linked to their associated variables the pump status is linked to the uh pump status which is the boolean and i've got it set so that it's an led they call this one over here is the type of a control i used over here and you can change this to use only one led or the two so i decided to use the two leds and so i have a green one and a red one and then the trigger level over here is linked to the trigger level variable and of course this is a two-way variable it's read write so as i change the slider control it will change the value of trigger level and i can pick that up in my code so now let's go and take a look at the code now when you go into the iot cloud it actually builds code for you it does a sketch over here and you can go and open that in the full online editor what i did is i built this i went into the full online editor and added my libraries but then i did the rest on the arduino ide 2.0 because you can link that to the cloud as well too and i find it a lot easier to work on this because you've got all sorts of different features here that you don't have on the online editor so this is a sketch in the arduino ide 2.0 but of course you could build it on the cloud as well too and it'll work just fine it starts off by including arduino secrets and the arduino secrets is a file that's included here i won't show you my arduino secrets file because that's what contains the ssid and the password for my network and so but that comes along in the package with the files there's also another one called thing properties that comes along with it as well now it tells you that these variables have been automatically declared so you don't need to declare them in your code and these are the ones that i declared up on the cloud so you'll notice my five variables over here now these libraries again i installed through the cloud which is why there are so many library files but basically it's the same ones that i installed for the moisture sensor the ssd 1306 and the gfx libraries from adafruit it just added a few other ones i just took them there because that's what the cloud installed i also installed a dht sensor library for my dht22 and by the way you could also have used dht21 or dht11 you'd install the same library and i installed the wire library for i2c and then after that we have some stuff you've seen before in the in the sketch that we did for the moisture meter that set the oled size the oled parameters and set up the oled it's basically the same thing this is just the parameters for the dht22 so if you decided to use let's say a dht21 you would just change this here to dht21 or dht11 etc otherwise the code is the same and our dht pin is pin 8 because we're using data input 8 for the sensor now i've defined some local variables for the temperature and humidity and you might find that strange because of course up over here there are cloud variables for it but the reason i'm doing local variables for that is because when you first start this up you're not actually connected to the cloud and so i'm not quite sure exactly what is going to happen so i want local variables so that this can work even when it doesn't have an internet connection i don't want it to be completely dependent on the cloud for the local thing to work because after all it's controlling a pump with water i want it to be in a controlled situation now these constants you've seen before the dry and the wet value for our probe for our moisture sensor and of course we need to determine those experimentally and so your values could quite well be different than mine and then this again we've seen it's identical to what we did for our soil and moisture meter we get the moisture value which is the value coming from the a to d converter and the moisture percent which is our converted version and uh then the sensor input is on analog pin a0 we've got that the relay port is defined on pin 3. and then the pump status text is just what's going to print on the pump status either on or off i've defined a variable for that a string variable and i've given an initialization value of off and i've also given it a trigger value and again of course i've got a trigger that's up on the iot cloud but i want a local trigger and so i set it to about 30 because i thought that was a reasonable trigger point for the water you could change that value if you like but basically when it starts up before it can connect to the cloud and see where the trigger has been set it's going to use a value of 30. and then this has been inserted for me with the arduino iot cloud it's one of the many things that inserts and it includes another file called thin properties and that's this file over here and thing properties has been generated in the cloud and includes all the different uh variables that i'm using so it defines them over here and uh init properties over here just uh initialize all the different uh different properties it tells you what it is current temperatures read on chain and the default value of null etc and uh so we go now into our setup and set up this again was added by the iot cloud this sets up the serial monitor and you notice it gives it a rather long delay of about a second and a half afterwards and so again this is going to take a couple of seconds for everything to establish itself and this is part of that delay we just saw knit properties that's in the thing properties file and it connects to the arduino iot cloud this again has been added automatically when i generated the initial sketch in the cloud and so is this and this is just a debug level thing and you can change the debug level if you wish to you can set it all the way up to four if you want to get all kinds of information if you're having problems we saw this before to initialize our oled display using the 3.3 volts so that's over here we initialize the dht 22 and this will work also with the dht 21 or 11 so we just do a begin set our analog to digital converter to use 12 bits as we've seen before set the relay as an output and we'll turn off the relay initially because we want this to start always with the pump off even if the soil is bone dry we'll wait a second or so to establish that we don't want to start by running the pump and we set the pump status which is the cloud variable to false because again we've set the pump off and then we go into our loop now the first thing in the loop is something the cloud added by itself and that's an arduino cloud update function to just update between the cloud and the local machine here the the arduino nano 33 iot then we're going to get our temperature and humidity from the dht and we'll assign them to the local variables and then we'll pass those values off into the cloud variables and so we've got both the local and cloud variable with the same value we're going to go get the moisture value and determine the percentage and keep the values between 0 and 100 exactly the same way we did in our soil moisture meter and then we print that out to the serial port we can just use that if we need to calibrate our soil moisture sensors and then we'll pass that to its equivalent cloud value the current moisture value which is a cloud variable gets the same value as this now we go and see if our pump needs to be triggered and so basically if the soil moisture percent is a lower value than the pump trigger then we're going to turn the pump on and otherwise we're going to turn the pump off and so pump on and pump off or two functions i'll show you in a moment and then we go to cycle the values on the oled display and i've got a couple of functions here called print oled and we just pass a number of parameters to them and you can actually even see the parameters thanks to the arduino iot cloud and um basically we're pumping the position and the value of the top text the position of the value of the bottom text and then the delay period how long do we want it to be on and so let's go and take a look at those functions because that's the end of our loop pump on and pump off are pretty simple for pump on we do a digital write to the relay and we write it high to turn it on we change our status text to on and we change pump status which is the cloud variable to true pump off does exactly the opposite thing print oled as you've seen all the different parameters you put on its input is basically the same as what we saw when we built the soil moisture meter instead of using fixed values we're just using the variables that we pump in so we set the top cursor text size to 2 and we set its position to whatever this value is on the first line line zero and we print the top text and then we set the main cursor on line 40 to whatever position main cursor is set the text ties text size to three and then we print the main text and then at the very end of it we do a display we delay by that delay time and then clear the display and so this just saves me from having to write this four times over and then finally on trigger level change this is something the cloud added because every time that i change that trigger variable this function will be activated so when i move the slider it will activate this function and all the function is doing is it's taking the trigger level variable and it's setting the local pump trigger value to that trigger level variable and that's basically it so that's a sketch it may look a little overwhelming at first but actually when you break it down it's quite simple and once you set that up with the cloud and your control panel you're all set to go now in order to demo this i found that it was a little bit awkward in the workshop to actually use soil and a pump and so i've used a different method but i wanted to show you the kind of pump that you could use with a system like this now this is a tiny pump that would be perfect for this the pump is actually submersible and it runs on four and a half to 12 volts dc and it actually pumps a surprising amount of water which is one of the reasons i refrain from doing this on my workbench because i didn't want a surprising amount of water all over my workbench but this is a fairly inexpensive unit that is typical for the kind of thing that would work perfectly in this application now instead what i have done is i've just wired my relay over to this lamp over here and so the lamp will come on in lieu of the pump and instead of soil i've just got a cup of water and of course if i immerse this into the cup of water then it's going to go to 100 percent moisture and at 100 moisture that's obviously not going to trigger my pump and we can take a look at the display yes it's at a hundred percent right now and so is the display up on the uh on the control panel now i'll take this out and if i wipe it completely dry of course i'm down to zero percent moisture and my light has come on right now to indicate that the pump would normally be coming on and it says pump on over here on my display and um on the display over here it's also dropped down to zero percent on the control panel and you can see that my pump is on but what i can do to simulate levels in between zero and a hundred is i can dip it in the water and then just take it out and leave a little bit of water sitting on the probe and what that tends to do is it tends to make the moisture somewhere in between like right now it's saying it's at 16 percent and so that allows me to simulate everything without again having to use actual soil now you've already seen the control panel and you've seen how it works and it seems to work pretty well now one thing i've noticed is there is a latency between when things happen like for example right now the pump has turned itself off because the moisture's actually gone up to 21 but it took a moment for the display to register that and that seems to be pretty typical i think it's the time that it takes for everything to travel through the internet and then to go over and register at arduino's cloud and then to come back to the microcontroller also when you first turn this on there's almost a delay of about two minutes before it starts to collect data which is one of the reasons that i put those constants in for things like the pump trigger level because i didn't want it sitting there without any values when it first powered up but otherwise it's actually a pretty effective system i've been running it for a few days and it seems to work pretty well and i think this is something that either by itself or a variation of this would work well for watering your own garden automatically using the internet of things so that brings us to the end of the video i hope that you enjoyed it and i want to assure you that no plants were harmed during the making of this video now if you're at all curious about my ability to kill plastic plants all i can say in my defense is that they really should print warnings on those things to say that they aren't dishwasher safe but my horticultural inabilities aside if you'd like to learn more about soil moisture sensors or get the code that you'll need in order to build the projects i showed you today you will find all of that on the article accompanying this video on the dronebotworkshop.com website there's a link to that article right below the video while you're on the website please consider signing up for my newsletter it's not a spam letter and it's certainly not a gardening advice letter it's just my way of keeping in touch with you to let you know what is happening here in the workshop and it's free to join all i need is your email address if you want to discuss soil moisture sensors more or if you want to brag about your own gardening abilities head on over to the dronebot workshop forums where you'll find a bunch of like-minded individuals who love to discuss technology and all sorts of things and the forum of course is free to join and of course if you haven't yet please subscribe to the youtube channel i make videos about technology and microcontrollers and things that don't have to do with gardening and all you need to do is click on the red subscribe button and when you do that also hit the bell notification and as long as you've enabled your notifications on youtube you'll get notified every time i make a new video so until next time please stay safe please take good care of yourself and i'll see you again very soon here in the dronebot workshop goodbye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 283,239
Rating: undefined out of 5
Keywords: soil moisture sensor, smart irrigation system, arduino iot cloud, raspberry pi pico
Id: pgGpuws7f9o
Channel Id: undefined
Length: 54min 45sec (3285 seconds)
Published: Sun Mar 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.