|
A Microcontroller-based Watering System: Description and Build Notes
It is odd to say, but this project is very boring. However, I get ahead of myself…
This is a different kind of project that I've done in the past—rather than a woodworking project, this is a microcontroller design.
I've been growing tomatoes in planters for many years. These planters are the kind with a reservoir in the bottom that should be full of water. I found that during the peak growing season in the heat of the summer, I sometimes needed to fill the planters twice each day.
I switched to using a drip irrigation system on a timer. The problem with this, however, is that the different plants and hence planters used water at different rates. If the timer was set to keep the thirstiest plants wet, then the planters for the smaller plants would overflow, and if I adjusted the timer for those plants, then the thirsty plants wouldn't get enough.
This is a problem because when the planters overflow, they end up leaching the fertilizer out of the planters, and the plants suffer.
Two years ago, I got some planters that had an automatic watering system. This controlled each planter separately using a pneumatic tube to push a flexible membrane against an orifice to turn off the water when the planter was full. They worked fine the first year, but not so well the second year. I wasn't sure that I wanted to try a third year on them. They were too expensive to just replace every year.
Part of the problem was that they were not adjustable, in terms of how deep the water would be when they turned off. So if I had a planter that was overflowing, I couldn't easily just lower the sensor and have it turn off sooner.
Then I realized that a solenoid operated valve was not too expensive, and I decided to make an automatic watering system from scratch. There were two main questions:
1. How to sense the water level?
2. How to control the valves?
I thought about various sensor schemes. A float hitting a switch? A float with a magnet triggering a reed relay? A float on an arm? A float bobbing in a tube? My main concern was reliability. I didn't want a system that could get stuck one way and flood the planter, or stuck the other way and leave the planter dry.
In the end, I decided on something with no moving parts: I would just use a pair of wires, where the water would “short them out”. I did some tests, and it looked like the water had a resistance of roughly 1MΩ, so if I used a 10MΩ pull-up, I should see the voltage change noticeably.
That left the issue of control. I wanted some form of hysteresis, so that I wouldn't be bouncing the voltage on the valve. Ideally, I would want a high and low sensor: when the low goes dry, open the valve, and when the high goes wet, close the valve. But this would double the complexity of the sensor.
In the end, I decided on temporal hysteresis: if I sampled the state of the sensor periodically, then after it got wet, the valve would stay open for a short time longer. I hoped that this would ensure that it stayed wet for an extended time and didn't keep flipping the valve open-close-open-close.
The desire for temporal hysteresis, and the interest in minimizing the circuit complexity led me to decide on using an Arduino Nano. It seemed ideally suited for my needs. I happened to have 8 planters that I needed to water, and the Nano just happened to have 8 analog pins (which I could use as inputs) and 8 digital pins (which I could use as outputs to the valves). Finally, the input impedance on the analog pins was sufficiently high that my sensor scheme should work.
I found that even with a long run of quarter-inch tubing, the water came out too fast and too strong. I thought about kludgy solutions, like putting a screw into the end of the hose and drilling a tiny hole in the side of the tubing, but then I found a much better solution. I found a 10psi pressure reducer online that had a garden hose connection on each end. When I put that between the garden hose and the tubing, the water came out at a reasonable rate.
I did some prototyping, and it seemed to work fine, so I started on the real version.
To minimize noise and power draw, I wrote the software to cycle through the planters one at a time, so that there would never be two valves open at the same time. I also set a max watering time, so that if the valve was open longer than the limit, the microcontroller would turn off that valve and go on to the next planter. In the event that a planter was “stuck dry”, this would keep it from hanging the system (and thus not watering the other planters) and would reduce the amount of water wasted.
I could have probably driven the valves with just a transistor, but to be on the safe side (and to minimize the discrete components I needed to fiddle with), I got an 8-relay board designed to interface with the Arduino.
However, my discrete components mushroomed on the input side. Originally, I was just going to use a single pull-up resistor per input, but then I figured that I should probably add some ESD protection (particularly as I use an electric fence to keep out pests, and the fence uses pulses of high voltage). This added two clamp diodes to each input. Finally, I added a capacitor and resistor to each input to low-pass filter it. My thought was that if there was a high-voltage spike on an input wire, the voltage on the capacitor would rise relatively slowly and give the diodes more time to bleed away charge.
The solenoid valves are an inductive load, so I also added a fly-back diode across them to try to prevent arcing at the relay contacts. I read later that these should probably have been Schottky diodes, but I just used regular p-n silicon diodes.
This is what the final product looked like:
|
The upper central portion is the main circuit board.
|
The center is the Arduino Nano. The left side is the input circuitry: the ESD and pull-ups that I described earlier. The right side are the fly-back diodes and a voltage regulator. At the top is a DIP switch that shorts the input the ground. This is used to enable/disable a particular planter. When the switch shorts the input to ground, it looks like that sensor is “wet”, and so the valve is never energized.
One of the challenges was how to arrange the plumbing.
|
If I put all 8 tee-connectors next to each other, it would have made a wide box even wider. In the end, I decided to do two levels of tee-connector and place the valves relatively close to each other.
The terminal strip on the right is for power in, and the terminal strip on the left is for the sensor wires.
I made a box for it with an overhanging top, and a Plexiglas front that ended with a gap at the top. My hope is that if it gets hot inside, the hot air will vent from the top, drawing cooler air in from the bottom. At the same time, the bottom of the gap is higher than the bottom of the roofline, so hopefully rain won't get in.
This required running a pair of wires and hose to each planter.
|
For the probes, I wanted something that I could embed inside a ½ inch PVC tube, so that I could put one down the watering tube, but so that I could easily remove it if it needed adjusting. Initially, I just pushed the end of the wire through a dowel with a snug hole through it
|
And then drilled and tapped a hole through the PVC so that I could use a set-screw to hold it in place.
|
After these sat in the water for a week, I got some very strange results. Some sensors seemed to be shorted, even when they were out of the water. Others seemed to be open, even when under water. When I pulled a few out to see what was going on, I found small globs of “gunk” on the wires—apparently some form of corrosion. (Later, I noticed that the wiring wasn't “copper”, it was “copper-clad aluminum”. I wonder if there was some sort of galvanic reaction between the copper and aluminum.)
Plan-B was to use Romex house wiring cable. This was solid copper, and it didn't seem to corrode when I did my tests earlier. So I just cut a short length of it and soldered it to the end of the wires.
|
If I was fancy, I would have used some heat-shrink over the connections, but since I'm not, I just wrapped it up in electrical tape.
After I got all eight reworked, I found that the 8th one was shorted, so it always seemed “wet”. I was terrified that the short was in the main circuit board, but when I removed the wire from the terminal strip, I found that the short was on the wire side. The obvious culprit was my ace electrical tape work, so I undid the tape and put new tape on, being more careful. After that, it seemed to work normally.
I spent a little time adjusting the heights of the sensors. This involved just pulling the PVC tube out of the planter's fill tube, loosening the set-screw, moving the wire/dowel up or down a little, and tightening the set screw. I noticed that the set screws didn't like being wet, and they were beginning to rust, but hopefully they'll work OK.
I noticed a strange behavior, which I can't really explain. I wanted the water level (when “full”) to be a short distance below the drain hole. When I first tried it, the valve would turn off about a half inch lower than I wanted, so I moved the sensors up. What I found after more work is that when they were initially filling, they would often turn off when the water level was low, but then on the next cycle, they would turn on again. For some of them, there was about 4 or 5 cycles before they stopped filling. The only thing I can think of was that perhaps the filling was causing waves (at least within the fill tube) and this would short out the wires, but then when the waves died out, the wires would be dry.
Eventually, I got the levels set properly, and I could then fill the planters with mix and plant my tomatoes.
This is the final result.
|
At this point, it is totally boring. I go out to look at it, the water levels are fine, and it is doing nothing other than blinking its LED. Later, I go out again, the water levels are at about the same place, and it is just sitting there blinking. The next day I go out and…the same! Nothing seems to change. The water levels are fine, and it just sits there blinking at me.
As I said at the beginning—this project is very boring. (But then again, here, “boring” is good.)
Update: August 3, 2022
Version 2.1
After using the watering system for one season, I would say that the overall system was successfull. On the other hand, my "probes" were less so.
When I took the system apart at the end of last season, I found that the copper wires were noticably corroded. At first, I couldn't figure out why, because I had previously tested immersing such wires with no ill effects. The clue was that the corrosion was not symmetric on both wires. Apparently, even though there was a tiny current flowing through the probe (on the order of 1 micro-amp), it was enough over time to do some sort of electro-chemical mayhem on the copper wires.
|
I wanted to improve things, but the question was how? My first thought was to replace the copper probe wires with either stainless steel or gold-plated pins. This might have worked better, but I wasn't really that comfortable with pumping a small but steady current through the water. The other option was to use a small float switch. The danger with that was since it was a mechanical system, it had the risk of getting stuck. On the other hand, it would avoid having the current flow through the water.
I found some float switches on Amazon that had a 0.70 inch diameter float. I found that 1 inch thin-walled PVC tube had an inside diameter of about 0.9 inch, and it would still fit down the watering tubes of my planters. This should give me enough clearance between the float and the side of the tube. I would have preferred more clearance, but this was the best I could do.
To mount the switches, I cut off some pieces of 7/8-inch dowel, and then drill and tapped a hole down the center. I polyurethaned it in an attempt to keep the water away from the wood. This was slightly loose in the tube, so put a layer or two of electrical tape around the ends to make them thicker. The net result was this:
|
I also had to rewrite the software. With the copper probes, an open circuit meant "dry", and a shorted one meant "wet". These float switches, however, were shorted when the float was low, and they opened when the float rose. So I had to modify the programming to invert the sense of each test, i.e. so that open meant "wet" and shorted meant "dry".
This, however meant that my DIP switch to turn off various channels no longer worked. Closing the switch would short that channel, which now meant "dry", so rather than disabling a channel, the switch would now make it always ON. I didn't want to have to take the circuit boards out, given all of the wires to other components, so instead, I just put a separate toggle switch inline with each screw on the terminal strip. That way, I could open each circuit and thus make it look "wet" to disable that channel. This came in very useful later on.
During testing, I found that when I powered up the system, it would always fire off one of the valves, even if the planter was full. I eventually figured out the cause. The capacitor on the sense wire would initially be discharged. When the power came on, it would start charging through a 10 meg-ohm resistor. Before the voltage could rise enough to be sensed as "wet", the software would read a low voltage, consider it as "dry", and open the valve. The solution was to put a few second delay at the start of the program.
In my old probes, I used normal set screws to hold the sensor in place. Even though these were above the water line, they were in a damp environment, and they rusted terribly. For the new probes, I ordered stainless steel set screws.
I got all of the probe units rebuilt just in time to plant the tomatoes. About a week or two later, I found that one of them seemed to be adjusted a tad high, so the planter was on occasion overflowing. I tried to readjust the position of the switch, and I found a major flaw with my design.
I had made sure that the dowel/tape was just about equal in size to the tube, so that the float switch would be well centered. It was a somewhat snug fit. However, being in a damp environment, despite the polyurethane, the wooden dowels absorbed water and swelled. The net result was that it was utterly impossible to move the dowels, even with the set screw removed.
An additional problem was that the dowel formed a fairly good seal across the tube, trapping air under it. This meant that as the water rose in the tube, it would compress the air pocket, with the net result that the water level inside the tube (where it was being measured) was lower than the water level outside in the planter. The net result was again, an overflow.
As a quick hack to address the second problem, I drilled a small vent hold through the tube just above the float. The first problem, however, required a rebuild.
This time, I ordered a 7/8 inch UHMW plastic "dowel". In theory, this should not swell, and so it should not get jammed in the tube. I also cut four slots the length of the plastic, to provide vents, so that I wouldn't get an air pocket forming. This resulted in:
|
I also found that the plastic was slightly larger than 7/8 diameter, and the inside of the PVC tubing was slightly smaller than my test piece. (Apparently the OD of the tubing is fairly accurate, but the ID is more variable.) The net result was that I had to use a block plane to shave down the outside of the plastic to get it to fit.
I was concerned with "stuff" getting up in the tube and jamming the float, given that the tolerances were not very generous. So I cut some squares of old nylon stockings, and taped these over the ends of the tubes to act as filters.
This led to Version 2.1 of the watering system. (Actually, about half the probes are still the Version 2.0 wooden dowel version, and half the newer 2.1 plastic version.) The other change was that over the course of the previous season, the wooden sides of the box warped, so that the spacing on the bottom was wider, and the plexiglass had trouble staying in. To address that, I installed a threaded rod to hold the spacing of the sides.
|
So far, with all of the above-mentioned tweaks, it seems to be working reliably. I ended up replacing 3 or 4 of the wooden dowels with new plastic ones. I'll have to see by the end of the season if it continues to work reliably.
Update: May 29, 2023
Version 2.2
I just finished tweaking and installing Version 2.2 of my watering system. While it essentially worked the previous year, there were two annoying issues with it. I could put them both under the category of "visibility", or more particularly, the lack thereof.
The first instance of this wss actually with the newer Earthbox planters. Their drainage holes are 5/16 inches in diameter. This lets excess water flow out, but it doesn't really allow for observation in. If the water level is too high, I could tell because water flowed out. But if the water was not flowing out, I had no idea whether it was full and almost flowing out, full but a short distance below the hole, or if it was essentially empty.
The other instance of a lack of visibility was with the float switches themselves. Particularly with the nylon stocking "filter" over the end, I had no idea what was going on inside the tube. If there were issues with the sensor, I would have no idea whether there was crud inside jamming the float. If I needed to readjust the location of the sensor, it is very hard to tell where it is.
The obvious solution to the first problem was to make a bigger hole. The main issue is that these planters do not have a very big reservoir as it is, and so making the hole extend lower would cut into the capacity. On the other hand, making the hole extend higher puts it above the partition between the dirt and the reservoir.
I decided to raise the hole. This was easily done with a hole saw. I've had issues in the past with Forstner bits or Spade bits trying to cut through thin plastic. The hole saw worked very easily and left a very clean hole.
|
I then needed to cut out a matching notch in the partition to provide access through the hole into the reservoir. I needed something that would hold the dirt up and keep it from falling through the notch. I also needed something to provide some rigidity to the partion. partition.
This "hood" over the notch needed to be made of some material that wouldn't rust. I got a strip of 2-inch 1/8-inch thick aluminum at my local big box hardware store. It would have been trivial to bend it up with a sheet-metal brake, but of course I don't have one. I had to make due with a vice, some pliers, and a big hammer. The brackets I made weren't very pretty, but they should be functional. I attached them with stainless steel fasteners.
|
The only other modification was that the partitions were supported on two opposite corners, and the access tube sat on top of the partition. The weight of the soil tended to make the partion look like a potato chip, sort of saddle shaped. This lowered the access tube until it was barely above the soil level. For each planter, I cut a piece of PVC tubing and attached that to the bottom of the partition next to where the access tube sat. This kept that corner from sagging.
My solution for visibility into the float switch was to cut some slots in the side of the tube. This was easily done with a router, a quarter inch spiral bit, and a small jig.
|
I wanted to put something over the slots to keep crud out, but which would still allow me to see inside. I thought that a piece of clear plastic might be hard to see through. Another piece of nylon stocking would be a good filter, but I probably couldn't see through it.
I eventually decided on a piece of window screen. I figured that it would be transparent enough, but it would still keep at least the bigger pieces of crud from getting inside.
|
Putting the nylon stocking material over the end, completed the float switch tube. I would have preferred something that was easier to put on and remove, but I couldn't think of any easy way to do that.
|
The other annoyance that I wanted to deal with was the wiring. I had essentially a continuous wire from the float switch to the main control box. This made it a pain to troubleshoot when it was misbehaving. I had to attach my multimeter to the ends of the wire at the control box, and then I would be fiddling with the float switch tube perhaps ten feet away.
The solution was to add a connector at the tube end. I got some water resistant connectors that were small enough to fit inside the tube. These would allow me to unplug the switch from the main circuit, and so troubleshoot things at the planter in question. I also switched from using kludgy electrical tape connections to doing it the right way--with heat shrink tubing. This resulted in:
|
The final change was the top end of the tube. I had previously covered it with electrical tape, but this was a pain to remove and reapply if I needed to tweak things. I replace the tape with PVC end caps. These should keep water out of the tube, but then are easy to remove and replace.
|
The final result looks like this.
|
|
I'm hoping that this is my final version--that it will work well, and that any issues are reasy to fix. If this is the case, then I won't have to fiddle with things next year--just take it down in the fall (so it doesn't freeze), and then reinstall it the following spring. Only time will tell, however, whether I'll need to go to version 2.3 next year.
Update: June 23, 2024
Version 3.0
This year, I did a major upgrade of the system. I almost completely rebuilt it from scratch. The main reason was that I wanted Wifi connectivity, and that required going to a fancier processor. That processor, not surprisingly, came on a bigger card, and that card would not fit on the same circuit board as my original processor.
I ended up building a new "mother board", and to reduce the wiring, I moved the clamp diodes on the solenoid valves to the solenoids themselves.
I used 8 digital channels to drive the solenoid valves. I then had to use 6 analog channels and 2 digital channels to read the sensors. This Arduino has an 8x12 LED "display", so I added some graphics indicating what watering channel was active, and how much longer it was until the next cycle. On the old design, there were a number of times when I was working on it and didn't want to wait for the next cycle, so on this one, I added a button that ends the wait and immediately starts the next cycle.
The “front-end” consists mostly of a pull-up and ESD protection. There are 8 copies of the following, one for each channel. There is also a button, to be described shortly.
|
The main loop checks each channel in turn, and if it is "dry", it energizes the solenoid value for that channel. It turns the valve off if the channel reads as "wet", or if the on-time exceeds some threshold. This threshold is to keep a channel that is "stuck dry" from starving the other channels. After it has looped through all 8 channels, it pauses for a few minutes before starting the checking again. This is to limit the amount of water that would be dumped through a "stuck dry" channel. The intent is that the on-threshold is significantly longer than a plant will use during one cycle.
There are times when I might be working on the system, I am trying to fill a mostly empty planter, and when that channel times out and stops filling, I don't want to have to wait several minutes for it to continue. That is what the button is for. When the unit is delaying, waiting to start the next cycle, pushing the button ends the delay, and starts the main cycle immediately.
I debated whether to try to make a custom circuit board for the front end. I've never done this before, and I thought that it might be a good excuse to learn how. But in the end, I just used a perf board and point-to-point wiring. Here is the bulk of the new design. The new board on the left (next to the column of switches), and the new Arduino on the right.
|
For simplicity, the Wifi connection uses UDP packets. I structured it to respond to requests. That is, periodically, it checks to see if there were any incoming UDP packets on a particular port. If there is, it responds to that IP address with a UDP packet containing the values of counters. These count how many seconds each channel's solenoid valve was open. I then wrote a perl script to periodically poll the system to retrieve the counters. This allows me to capture the data into a file and to create graphs like:
|
Eventually, I hope to augment the system to try to detect if a channel becomes stuck open or stuck close and raise some sort of alert on my PC.
I also changed the physical protection on the water sensors. I decided that the old way wasn't really working. It was hard to be able to see inside and to keep debris from getting in and possibly jamming the float. When something did happen, it was a real hassle to fix, because everything was taped up thoroughly, so I would have to spend a lot time removing the tape, cleaning out the junk, and then taping everything back up again.
For the new approach, I got a bunch of ankle-length nylons. I just slipped them over the water end of the sensor, and held them up with a rubber band. The top of the stockings should be well above the water line, so the mesh of the nylons acts like a filter to keep the junk out of the tube. If something does get in and jams the float, then it would be very easy to slide the stocking off, clean it out, slide it back on, and reapply the rubber band.
|
Here is the new unit, before I hooked up the sensors and the tubing. Here is the new unit, after being fully hooked up.
|
|
|
|