Raspberry PI + Moisture Sensor with Python (wiring, code, step-by-step walk-through)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm tommy master from data36.com and this is the next episode of a series where i document a hubby project of mine called growing strawberries with a raspberry pie on autopilot in the previous episodes i showed you the concept of this project and then i got started with the first step namely how to access the raspberry pi through wi-fi it was an important step because with that i won't need a dedicated keyboard mouse or monitor for my raspberry instead of that i can do all the coding from my personal computer via an ssh connection from the terminal in this episode i will start to add the specific hardware that my automated gardener needs and the first piece will be the moisture sensor yep so as you might expect the moisture sensor will measure and report the moisture level of the soil so basically how dry or how wet it is it will send this data to the raspberry pi and then we can take action based on the information we get and either activate the water pump or not now in this video let's focus on the moisture sensor and the first thing that i have done was a lot of reading about what's the best moisture sensor that i can use and i found that basically there are two types the ones called resistive style where there are two prongs and the other version is the capacitive measurement which uses only one probe almost everyone said that there is a huge issue with the two pranks version which is oxidation basically if you use resistive moisture sensors for a long time they will oxidize and there is nothing to do about it and that's why i ordered this one i will link it to the description but it's adafruit's capacitive moisture sensor and i will just go ahead and try it out the first thing that i will do is the wiring luckily adafruit provides a tutorial where we can see the wiring itself here it is for a raspberry pi so all i have to do is to follow this model and the red cable goes to the three volt pin the black cable goes to a ground pin one two three this one the green cable goes to the s c l pin so to this third pin and the white cable goes to the sd a pin like this if you are wondering what pin does what exactly on a raspberry pi you can just google it and we'll find this image which is pretty useful or you can run a command from the command line that will return the same information but i will get back to that soon now the wiring is done so let's turn on the raspberry pi and let's hope it won't die because i messed up something with the pins okay so far so good and i will just go ahead and log back into my raspberry with the ssh pi at 192.168.1.67 command so again this is where my pi is found on my local network buys the username ssh is the command and i type my password and it seems working and as i promised here's the command to see the pins pin out see here is a visual and here is the structure of the 40 pins on your board i will get back to this in a later video but right now for the moisture sensor we use these four pins right cool i will just clear my screen and another good thing in this adafruit moisture sensor is that in the tutorial they provide the actual code to make the software part work and if i scroll down here is the code but before you think that i just have to copy paste this run it and we are all done i warned you it's not that easy and well it's never that easy if you have watched the previous video where i actually accessed my raspberry pi for the first time you will know that we haven't installed anything yet so this is pretty much a pure raspberry pi without anything installed to it that means that before we can copy paste and run this code i will have to install a few things well in fact a lot of things but don't worry i will guide you through first i will go ahead and run the standard updates that we always do when we set up a new remote server even for data science and if you think about it the pi the raspberry pi right now pretty much works like a remote server so i will just do sudo apt get update and then sudo apt-get upgrade yes i want to continue call this took quite a while like for me it was 10 minutes or something so because we will use python we will also have to to do apt-get install python 3 pip okay it's already the newest version we can move on what i will also need is a cool text editor for my code and i have to be honest here and i know that most of you won't like me for saying this but i really really hate vim and nano and all these simpler linux based text editors for coding so i will go ahead and install mcedit instead i clear my screen and i type sudo apt-get install mc yes and this installed mcedit which is another linux based but more visual text editor for coding which is a good start so for instance here we could copy paste this code copy paste and save it in python format well i won't do that right now i just won't save this i just wanted to show you this text editor and i will go ahead and install sublime text 2 which is my actual favorite text editor for coding it's user friendly it's easy to use and has a lot of cool features so i can only recommend it i won't show you the installation of sublime text in this video because it would took like another five minutes and i have already written a step-by-step tutorial article about it but you can either follow that tutorial article or here it is or just skip using sublime text and you can use mcedit instead by the way of course i will link this tutorial and everything else that i show you in this video in the description and i have just installed sublime text and now that it's done i can start to focus on installing packages for the moisture sensor and i'll be honest here while i love adafruit's tutorials the installation process for this one was a bit confusing at least for me but i spent like an hour with it and figured out what i will need and what i won't need and i will show that to you here so you will see the original adafruit tutorial on the left side of my screen and my command line on the right side of my screen and on the left side i will pretty much jump between articles to get everything done but don't get confused if you want to do everything correctly just follow the right side of my screen and do everything like i do okay so if you want to use raspberry and python like i do for this project you have to just skip through everything and go straight to this python installation of cso library and i will add this link to the description of course so you can click and find this directly but the point is that it's not as simple as running this one line of command you will actually have to do some preparation so open this article first and you will see that you will have to install a lot of things it starts with the prerequisites luckily we have done all these in the previous video then you will have to update your pi and python we have done this in this video you don't have to worry about these parts so the next step is enabling i2c and spi and since i'm a beginner with raspberry pi i'm not 100 sure what these things do but i will figure it out later the point is that first you will have to enable them the first step is enabling i2c and this short description at the beginning at least gives a hint what it does and for now let's do the installation for these two modules this one and this one i'm just copy pasting here and as i can see both were already installed so that's cool so all i have to do right now is the configuration so i will have to run rus p config and follow the screenshots here go to interface options while in this system there is no advanced but i will have to go to i2c i will have to enable it done i click finish oh i can click it so i will hit finish and i will have to reboot my pi cool i give it a few seconds i clear my screen and then i will log back in and if you are wondering what this part is in my ssh command it's for the sublime text editor so if you install sublime text you will know what this is if you didn't don't worry about it and let's see whether i managed to get this done properly should be fine for now so let's move on and let's configure the spi port i will just have to go back to this raspbi config menu and in the interfacing options choose spi enable and that's it i will click finish and if i go back to the original article i see that it was pretty much it and i will have to reboot again so i will do that and i'm logged back in again and now i can verify whether this worked it worked great move on with the article i won't need a second spi i will skip this make sure you are using python's report and instead of that i will go ahead and install the libraries that i need the first one will be this gpio library and this one will be needed to control the gpios so the pins and as i can see this one was already installed so let me install the next one the adafruit blinker which i will need for the moisture sensor and it says that that's pretty much it i'm now ready to test so i will just copy this code and open mc edit paste it here save as blink test dot bi and let's run it python 3 blinkertest.pi and it seems that everything's working so far which is awesome so i can go back to the original original article and install the last library that i will need which is the seesaw library i will again copy paste this command hit enter and it's done as well and after all these installations are done we can finally copy paste this code snippet and test it out so i will open mcedit again i will copy the code paste it here save it as i don't know moisture test dot pi cool it looks good and remember the moisture sensor is already wired to my raspberry pi and that means that the hardware part is set the software part is set and if i hit enter and start to run i will clear my screen first so if i will hit enter for this command and start to run this test script then everything will just work well hopefully at least i hit enter oh boy okay i haven't seen this coming so i'm gonna pause the video and i will try to figure out what caused this okay i found the issue and it only took like two hours by the way the problem was with one of the jumper cables and i just didn't want to edit this out from the video because there's a lesson here after pausing the video i was looking for the issue like for two hours or so and i haven't found it so i went home slept on it and came back and today i found the problem in like five minutes actually my very first action was to replace the jumper cables and it fixed the issue and i always have to remind myself and this applies to most projects i work on so when i can't fix something for hours it's better just go home and start over everything with a fresh head on the next day anyways back to the code and python 3 moisture test dot pi and hopefully yes it works now cool okay and i have a glass of water so i will put the moisture sensor into the water and there you go the value went up that's great and then i will remove it awesome okay i have just stopped this and two comments the first one is that this sensor doesn't just measure the moisture level but the temperature as well which can become handy it's a nice extra feature but what i really wanted to say the other thing is that i could have seen this problem before actually when i ran this line the i2c detect dash y one this table was all clear this 36 value wasn't here and it should have been here and basically not having this here meant that i didn't have the moisture sensor detected by my raspberry pi but now it's fixed and it's there and we can move on i'm going to clear my screen and of course copy pasting the code itself doesn't mean that we understand the code so before i finish this video let's try to go through it and modify some parts of it to see whether i understand everything correctly in it and for that i will just close this tutorial and instead of that i will open my script with sublime moisture test if you don't have sublime on your computer you can use the mcedit moisture test version but i prefer to do this with sublime so left side is the script right side is the command line and the script itself is pretty simple let's go through line by line this will import the time module that i will need for this sleep function here then from board i will import scl and sda which are this is basically the pin calibration i will need this here and this says that i get the information on these two pins and instead of that i will try out something but i will get back to that later and then i will import these two packages or libraries that will be needed these two for the moisture sensor and this part specifies the pins which means will i get the information on as far as i understand and i want to test this out because in other tutorials i've seen people using it like this they import the whole board basically let me show this pin out and here are all the pins that we can use and in the script obviously we have to tell that which pins we will use and basically this is the part where we are telling that and scl refers to this pin if you remember this is where we plugged the green cable of the moisture sensor and sda refers to this pin and this is where we used the white cable so in other tutorials they refer to these pins directly not with these scr names but with board dot d3 and board dot d2 which i like better because this is a more direct way to tell that i will use this and this pins but to see whether it works i will save this and i'm going to run the moisture test script and if it runs and it does run then this just works so indeed these are interchangeable i replaced them with this other format that i prefer i will stop the script and let's see what's happening here as i said i don't really understand this i2c underscore passport and the whole i2c module but at least because of the issue that i had i have a clue that this refers to this so this is where it detects my moisture sensor but i will read more about this and if you have to know more about this i will definitely add to my next videos but the interesting part comes here this opens an endless loop and it does two things first it reads the moisture level and saves it to the touch variable and it reads the temperature from the temperature sensor that is saved into the temp variable and the whole thing is printed here now just to make sure that i get everything right i want to add an if statement that will hypothetically evaluate whether my strawberry will need watering or won't need watering and in this case i will try simply add an if statement here and i say that if touch is greater than or no less than um i don't know 400 then it prints this and it will no actually this goes here and this goes here and if touch is less than 400 so the moisture level is less than 400 then print watering is needed and otherwise it prints watering is not needed for now save it let's run the script again watering is needed because my moisture level is less than 400 and when i put the moisture sensor into the water it says watering is not needed for now beautiful and at the moment i remove it watering is needed again okay of course this will be a bit different i will just stop this when i will have the actual plant but at least now i see how the moisture sensor works and how it will be part of the whole system again this was only a first test and i will definitely go deeper into what works how but the point is the moisture sensor works perfect and this is the end of this episode if you liked it please subscribe leave a like or a comment and if you have done projects like this and you spot any mistakes i made or you have best practices you want to share please do not hesitate to share it in the comments in the next episode i will either continue with the setup of the water pump but since it hasn't arrived yet i might continue with the automation of the lights instead we will see anyways don't forget to subscribe to my newsletter at data36.com newsletter this is the only place where i really communicate to my audience right now so this is where you can get notified about new content as well and if you do subscribe you will get access to a lot of free data science learning materials too like a python cheat sheet an sql cheat sheet or a free mini course called how to become a data scientist thank you for watching i'm tony heinstein from data36.com until next time [Music] [Applause] [Music] you
Info
Channel: Data36 - Online Data Science Courses
Views: 24,471
Rating: undefined out of 5
Keywords: online data analysis, data science, data science for beginners, startup, data analysis, analytics, online research, yt:cc=on, data36, tomi mester, data science tutorial
Id: M3RuHX6jEXI
Channel Id: undefined
Length: 25min 59sec (1559 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.