Raspberry Pi Plant Watering (& Time Lapse)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

As usual, Christopher Barnatt presents a great project!

Python code here:

https://www.explainingcomputers.com/pi\_watering.html

👍︎︎ 1 👤︎︎ u/jakethepeg111 📅︎︎ Jul 19 2021 🗫︎ replies
Captions
[Music] welcome to another video from explaining computers this time i'm going to set up a battery-powered raspberry pi zero so that it can water some plants and take a time-lapse video of their growth so let's go and get started right let's begin with a rundown of the hardware for this project which starts with this a raspberry pi zero which i've currently got in a pibo case i think i'll probably take it out of the pybo case as things progress and it's currently got an indicator led on a header plugged into the top there from my raspberry pi battery testing video a few videos back this might be useful as well in this project and before anybody asks i could use a raspberry pi pico micro controller to make a watering system but i've chosen to use a raspberry pi zero because it's got a camera connector and so it can handle the time-lapse photography part of this project as well next to enable this raspberry pi to supply water i've got one of these a 12-volt normally closed solenoid valve which is the kind of thing you use to turn water on and off in say a washing machine or a water dispenser and specifically as you might be able to see here this is an fpd270a which is a pretty generic component mine cost me eight pounds 89 on amazon co uk but there's lots of very similar solenoid valves around and on amazon.com i found one for 12.99 and of course i'll put links to solenoid valves you might wish to use in a project like this in the video description next i'm going to use one of these a raspberry pi relay hat to control the solenoid valve specifically this is the relay hat i used in an earlier raspberry pi automation video a few years ago and ideally i'd have a two relay relay hat rather than three and i'd have one which fitted directly on the raspberry pi zero without overhanging something like this 1299 model but this board will work fine it's just a bit bigger than the zero but i can cope with that in the in the design of the project and these relays can switch up to 30 volts at 5 amps which is perfectly sufficient for controlling our solenoid valve moving on i'm going to try out this resistive moisture sensor which i got from the pie hut which will open up with mr scissors let's get inside there we are some bits in here various parts there's the moisture sensor itself this is going to go into a pot so we can sense the moisture in the soil get that bit there and there's a little comparator board here and wires to plug things together and moisture sensors can be resistive or capacitive in nature and this resistive one is very easy to use with this comparator board because you just plug it together and it gives you a digital high low output so all we need to do is to adjust the potentiometer here to the point where we've just got enough moisture for our plants they've got enough water and we can get a single to the pie saying whether the plant's got enough water or not this said it is worth noting that resistive moisture sensors can be problematic because they can corrode very easily but i've got a cunning plan to try and stop that happening which i'll tell you about we get to that point in the video moving on again to record nature in action i'm going to use a camera connected to the pi it's going to be this zero cam for the raspberry pi zero which comes in this rather over the top box but uh hiding in there somewhere is the zero cam that'll connect to our pi which will enable us to record a time lapse video finally i'm going to use this np7 12 volt lead acid battery which can power the solenoid valve directly and which will connect to the pi zero using this buck converter as i covered in my recent raspberry pi battery power video and so here we are all the electronic electrical and computing parts we need for this project so it must now be time to get them working together greetings here i am back again and i thought i'd test out the moisture sensor which as you can see i've got connected up to its comparator board and through to the raspy pi zero and the wiring here is very straightforward i've just got 3.3 volt and ground on the pi connected through to the comparator board along with a gpio pin and as you can probably see here the raspberry pi 0 is connected up and running and led has come on the comparator board has to be a good sign so let's go across to the raspberry pi desktop here we are where i've written some test code in sony and this is a very simple bit of python initially it just imports a few libraries for time and gpio and when it sets gpio numbering to b board and it sets gpio pin 29 to be an input and then beneath that we've got a while true loop while true is always true so this will be an infinite loop and what this does is to say if gpro import 29 is a zero it'll print wet else it'll print dry and then we've got a time sleep command for 0.25 seconds just to stop this loop going completely manic and then everything here is contained inside a try finally combination there's try up there it'll go through that lot but if we crash out of this using control c it'll always execute finally which in this case will clean up our gpio pins so let's execute this code by running it up there and we'll give ourselves a bit more space to see the output which will hopefully appear in a second there we are it's printing dry running up the screen like that so let's go back to the moisture sensor and let's just bring in for example a glass of water don't be bringing one of those next to a pipe if we put that in there oh look it's wet we put it in the water and it's dry if we take it out the water and wet and dry so that's potentially useful for sensing moisture but let's give it a more hard test than that i've got over here two little pots there's pot number one and this is pot number two and pot number one has got dry soil into the stick the moisture center in there well it's still dry nothing's happened there but moment of truth what if we go into the wet soil oh look the wet soil is wet so there we are we've proved we can use our moisture center to detect the difference between dry and wet soil and we can use that in our raspberry pi watering system right here i am back again and i've now put the relay hat on top of the raspberry pi zero and the relays on this hat are controlled through pins 37 38 and 40 but there is pass-through on the gpio connector here on the relay hat so i've been able to reconnect the moisture sensor and as you can probably see i've also connected in the solenoid valve it's getting very exciting here this is connected in to be powered from the battery the leads are just looped via the butt converter which also will be powered by the battery and that this will be switched using the the first relay on the relay hat connected there to its normally open contacts so it will be turned on in theory when the relay is uh closed using the raspberry pi so let's go across the raspberry pi desktop where i've made a few changes to our code this is now a piece of test code to test out the solenoid valve as well same libraries come in anything different here is i've set up gpio pin 37 as an output to control the relay which will hopefully switch solenoid valve and in our little test loop it's not just going to print things out it's also going to turn the solenoid valve either on or off and this is of course not exactly what we'll do in the final code but it's good enough for test purposes so let's just run this code like that we'll give ourselves a bit more display there to see the output everything is dry at the moment oh just sorting itself out there anyway let's just go back to the bits of hardware and if i take the moisture sensor if i now put this into the water it should make the relay trigger so hopefully you can hear that there's also a little led you might be able to see that coming on and off so we i know that the relay is triggering so what i'm now going to do is to connect power on the battery which means in theory when we now trigger it it'll also trigger the solenoid valve and again all we can do is hear it because you can't see anything happening here but in theory yes there's quite a kick when this releases can't really hear it going on but definitely this the solenoid valve is triggering so we've progressed we've got more things working as we head towards our raspberry pi watering system right i told you i had a cunning plan to stop the moisture sensor corroding and it's this two yellow wires and what i've done is to disconnect the power going to the moisture center comparator board here from the pi from 3.3 volts and i've looped it through the second relay here ah you cried this means we can turn the moisture sensor on and off we only have to have it on only have to have power going through it when we need it and that will reduce its chance of corroding that is the idea now if we go across to the pi's desktop i've also made some changes to our code this is more like the final code i've even called it watering pi now and what i've done here is first of all to add in the gpo output to control the second relay to turn the moisture sensor on and off there we are and i've also set up a couple of variables here called interval and water and interval is going to be the period of time that pi waits before checking to see if the plants need some water and here i set it to be five seconds but it'll be more like say eighteen hundred seconds in the final version of the code something like half an hour but of course i've set it as a variable so we can experiment with it get the right value and we'll also have to experiment to get the second value here which is water this is the period of time that the solenoid valve will open to let water through to water the plants and i've realized a valve i'm using is designed to take mains water pressure and the amount of water it lets through is in some senses proportional to that pressure so we might have to open the valve for quite a reasonable period of time to let through a small amount of water to water our plants anyway we can play with that later on when we're setting things up anyway down here we've now got another tri-finally loop and this as i say is a bit more like our final code dmv i'm whistling all over the place anyway what it basically does is first of all to turn on the sensor and allow it to settle so it turns on the second relay wait for a second so electronics can settle down and then it checks to see if the sensor is dry and if so it opens the solenoid valve waits for the time defined in the water variable and then closes the valve again to obviously close the valve and then at the end of that we then turn off the sensor wait for the period of time interval and then everything repeats so let's now run this code there we are and we'll go across to the shot of the relay board which i've got slightly dimly lit so you can see hopefully it's leds which as you can see are coming on and what is going on here is first of all it's turning on relay 2 to activate the sensor there we are checking if it's dry it is turned on a solenoid valve turns it off wait for a period of time interval turns on the center again there we are wait to see if it's dry turns off but this is because i haven't got the moisture sensor which i've got somewhere over here i haven't got that in the water let's put it in the water it's now obviously wet so when we get the cycle going through it'll turn on the sensor just turns off again turns on the sensor just turns off again turns on the sensor just turned off again there we are take the moisture center out of the water turns on the sensor finds it's dry turns on the solenoid valve and then it gives it some water if there was water coming out the solenoid valve so you can see the principle is working here we're heading towards a working watering system guess what lots of things have now happened for start i've mounted the pi zero and the relay board to a baseboard and secured it to a post at the back of this makeshift rostrum and i've also wired pi zero into the butt converter which is now down here so everything here is now battery powered although i've had a problem with one of my battery terminals but do not fear everything is working just fine and so the wires coming out of the contraption here going off screen somewhere these are just for the hdmi connector for looking at things on the monitor in a second for testing purposes for coding purposes we've also got a connector here to usb to a keyboard and a mouse at the moment those will disappear as this thing becomes a completely headless setup and down the front here i've got the solenoid valve and the idea is there'll be some sort of tray down here you have to imagine a tray down here it doesn't exist at the moment but there'll be a tray down here in which pots of plants will sit and the solenoid valve will deliver water into that tray to keep the plantar watered and happy and the only reason i'm not showing you the solenoid valve in operation so far actually with water going through it is that i've had great difficulties getting hold of some flexible plastic pipe to connect on here and take to a water supply so fingers crossed that plastic pipe will arrive before the end of the video since i saw you last i've also added the indicator led to the pi zero which connects to gpio pin seven and via its current limiting resistor to a ground rail and i've also added the zero cam camera on this little wire mesh support so it can be angled to look down towards the plants and i've adjusted the focus on the camera so it's as close as it can be in terms of focusing it might still be too close to the plants though i might have to use a different camber with a longer ribbon cable to get a bit higher up but this camera this will be okay for now for testing purposes we go across to the code you'll see i've also made some big changes here in software land for start i've imported the live before the pi camera and i've also set up gpio pin 7 as an output to control the indicator led and then beneath that i've set up the camera defining camera as pi camera setting the maximum resolution and the frame rate of 15 which is required to actually access the maximum resolution if we go down to our variables i'm now defining interval in minutes for reasons which will become clear in a second i've currently set it to 0.5 minutes for our test here and i've also defined pic number which will be using capturing or sequence of time-lapse images we then go down to our loop this has also changed for start we turn on the led at the start of the loop when things are about to happen we then turn on camera preview as well as activating the moisture sensor and we have a time sleep of five seconds because it takes the camera quite a time to settle we then go on down the rest of the code there is the same as it was before but now at the end of it we've then got the camera taking a picture which it's going to save to home pipe pictures and it's going to use the pignon variable to label our frames we then stop the camera preview going on which will save some power we increment pig number to do the next number on the next part of the loop and then after that we've got our waiting for our interval period which used to be a simple sleep command but it's now a bit more complicated i've basically taken the code from my battery testing video so we're flashing an led every 30 seconds so basically this flashes the led three times at the start of every 30 second period and it goes through this loop while the count here is less than interval times two which here will be interval times two will give us one so in this test it'll go through the loop one time but normally i'm going to be setting interval to 30 minutes but i want to see an led flashing away just to show us the device is working and that's what this code is going to do and then at the end things finish off just as they did previously so let's run this code and we'll bring up a shot of the board so you can see the led turning on as the code executes there we are hopefully you can see that and we've also now got on the screen here a preview from the camera which is brought up so it can take a picture so you get a little picture of the pot there that's now a picture has been taken and it's now going to do a flashy led as it is and then it'll wait another 25 seconds and repeat the process so we'll let that go through got some warnings here coming up for gpio because we're we haven't tidied up gpio before i started this never mind doesn't matter won't stop things happening hopefully in the second we'll get to end of our 30 second period i should learn to play the harmonica to give you music at moments like this but anyway we will wait in real time for the thing to start again shouldn't be long now come on pie there we are it's brought up a preview again it's going to go through the process as it did previously checking the sensor if it's not got moisture there it'll open the solenoid valve etc and then finally it'll take a photograph there we are it's done the thing and i'll close that down and the reason i've done that is so we can open up the file manager here there we are and i'll go to pictures which is where i'm saving the pictures and you'll see we've got two pictures from what we've just done and if we open one up you'll see we've got picture one is one of the pictures we were just looking at so you can see we've got everything set up now for the pie to water some plants but also to take a time-lapse movie and so i think it's now time to move all this equipment to my greenhouse to set it up hopefully with some plastic pipe and to see if it will work in practice greetings and welcome to my rather dilapidated greenhouse where i've got everything finally up and running i have switched the camera over to the other raspberry pi camera i've got the longer ribbon cable to try and get a better shot of the plants in focus and even so i've had to move the plants a bit further away from the rostrum which is a shame but at least that will now work and i've got the valve connected up i've got it connected to a makeshift water supply my plastic pipe arrived everything here should be in working order in the code i've set the interval between testing for moisture and releasing water and taking a picture to 15 minutes and i've set the water release time to 12 seconds after quite a lot of tests and i've also removed the spurious brackets that for some reason i put in around many of my gpio commands no idea why i did that obviously my mind was going off somewhere completely different when i wrote the code earlier but everything is now working fine so i'm now going to start things off and as i do so i'll give you a shot of the plants in my little tray and as you can see what we're going to grow is crest i've got some crust there growing quite well already some crust just germinated i'm taking these out of the traders for now so you can see what's going on and they're going to take the moisture sensor out of that little pot so in theory we should get some water release when the pie gets that part of the cone there are a few drips going on there from various earlier tests but this should now be working so we wait a second the pie should release some water and there we are we have a slow release of water into the pot doesn't put in much in every time but a bit has got in there you can see the principle so what i'm going to do now is to put my moisture sensor back into these plants because they're already quite moist to put that one in there i'll put these little tubs back in here if i get a bit more water in 15 minutes it'll slowly fill up don't want to over water these plants says he not being an expert gardener at all anyway i'll now leave the plants there and in theory this is the shots i can see of them all the magic of filmmaking and and i'll leave this for a period of few days and we'll come back to see the results greetings here i am back again it's now about three days later i started this experiment about 1 on monday it's now about 2pm on thursday and as you can see the crest has grown rather substantially and it seems the watering system has worked very nicely the water levels remain consistent throughout i did start to panic about 8 p.m on monday evening i came out thought it was flooding it wasn't i tried to manually remove some water i knocked the pot that'll be bad for the time-lapse video i think i've learned the best thing is to let the pie get on with it anyway this has all worked as far as we can see here but i'm also very interested to see if i've recorded a time-lapse video so let's go inside and see what's on the pi's microsd card and here we are looking at the first frame the pi recorded and the good news is it recorded lots of other frames so i have got a time-lapse movie to show you in a second but just before i do that it's worth pointing out that some of the frames the pi recorded were corrupt they looked like this and this and this and some frames were recorded in such a corrupt fashion they couldn't be read at all and of course there were a lot of black frames recorded by the pie because it was night time and so i've removed all the black frames and so of the 274 frames the pi recorded the final film i'm about to show you is made of 132 frames which i'm playing back at 12 and a half frames a second and there we are we've seen the crest claw its way out of the ground we've seen biology in action certainly there are various issues with this time-lapse movie it's not the best time-lapse movie in the world but it has made me eager to do more with the raspberry pi doing time-lapse photography so i think i'll be making another video all about raspberry pi time-lapse photography in the future but anyway for now i think we should look at the movie again this time with some background music [Music] so there we are we've set up a raspberry pi zero so it can water some plants and i suspect it's already a better gardener than i am but now that's it for another video if you've enjoyed what you've seen here please press that like button if you haven't subscribed please subscribe and i hope to talk to you again very [Music] soon you
Info
Channel: ExplainingComputers
Views: 187,074
Rating: undefined out of 5
Keywords: Raspberry Pi, Raspberry Pi Zero, watering system, raspberry pi watering system, solenoid valve, raspberry pi solenoid valve, relay HAT, raspberry pi relay HAT, raspberry pi water control, raspberry pi time lapse movie, raspberry pi timelapse photography, Christopher Barnatt, Barnatt
Id: _NTW0npN4N0
Channel Id: undefined
Length: 23min 41sec (1421 seconds)
Published: Sun Jul 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.