The Pot Plant Plot

vivianimbriotis | Feb. 25, 2020, 1:43 p.m.

Here is no water but only rock

Rock and no water and the sandy road

The road winding above among the mountains

Which are mountains of rock without water

If there were water we should stop and drink

Amongst the rock one cannot stop or think

Sweat is dry and feet are in the sand

If there were only water amongst the rock

Dead mountain mouth of carious teeth that cannot spit

Here one can neither stand nor lie nor sit

There is not even silence in the mountains

But dry sterile thunder without rain

There is not even solitude in the mountains

But red sullen faces sneer and snarl

From doors of mudcracked houses

                                      If there were water

    And no rock

    If there were rock

  And also water

  And water

  A spring

  A pool among the rock

  If there were the sound of water only

  Not the cicada

  And dry grass singing

  But sound of water over a rock

  Where the hermit-thrush sings in the pine trees

  Drip drop drip drop drop drop drop

  But there is no water

-- TS Elliot, V: What the Thunder Said



Part One: Ambition and Hubris


It is hard to kill a succulent [citation needed]. Nevertheless, when my brother’s partner made me a beautiful potted arrangement of them, it only took until the first exam period for me to fail to water it for a month - sorry succulents. Since then I’ve killed 3 other plants.


Whenever I feel sad about my personal failings, I internally recite: “I am not good at all things, but I am very good at some things,” and it turns out that if you are good at some things and bad at others, sometimes you can compensate for your failings by leveraging your strengths in creative ways. For instance, I apparently can’t remember to water plants, but I /can/ make a tiny computer servant water them for me.


At the start of this month, here’s what I knew about electronics:  a good amount of electromagnetism (thanks to volume 2 of The Feynman Lectures), a fair amount of theoretical circuit analysis (thanks to the 1978 book Basic Electric Circuit Analysis by Johnson and Hilbum, which I owned for some reason, and also to khanacademy), a solid conception of how computer chips actually worked (thanks to Ben Eater and the nand game), and a working knowledge of x86 assembly and C. Things I had never done included: touch a resistor, build any kind of circuit at all, program a microcomputer (other than a Raspberry Pi, which doesn’t count and is basically just a small PC), and grow a plant.


Initially, the plan was: program an arduino (one of these things, a user-friendly microcomputer) to supply a fixed amount of water to the plant at the same time each day. Simple.


Loop forever:

Supply water (150ml)

Wait (1 day)


One problem: how much water to supply? Supply too much and I’d get a pile of mud with some seeds in it, supply to little and I’d get dead plants (again). And the amount might change depending on the time of year, the temperature, and the humidity, all of which would influence the rate of evaporation of the water! Okay, so how about monitoring the soil moisture level (somehow) and then watering to maintain it near a certain level?


Loop forever:

if soil_moisture < SOIL_MOISTURE_TARGET:

  Supply water (10 ml)

otherwise:

Do nothing


I didn’t really want the pump to wake me up at night, so I added a handler for tracking the time.


Loop forever:

if soil_moisture < SOIL_MOISTURE_TARGET:

  Supply water (10 ml)

otherwise:

Do nothing and wait 1 day


So now the arduino will check each morning if the soil moisture is below the SOIL_MOISTURE_TARGET threshold; if it is, it will supply 10ml of water and then check again, and keep doing that until the moisture is above the threshold, then go to sleep until the next morning. Okay, but: How am I going to measure the moisture of the soil? What should my SOIL_MOISTURE_TARGET be? And how am I going to pump the water?


Part Two: Wet Dirt


There are a couple of ways to measure soil moisture. There’s water concentration (mass of water divided by the total volume of the soil), the gravimetric water constant (mass of water divided by mass of dry soil), bulk density, water density...


Since it’s the measurement that seems to be used all over the literature, I used volumetric water capacity (VWC), which is the volume of water divided by the total volume of the soil-water mixture. 


(note: it is not a slam-dunk to me that this number is bound in the closed interval [0,1], because there exist solutes that raise the mass-density of water; magnesium sulphate, MgSO4, is an example; I proceeded assuming that realistic values of VWC were in [0,1].)


“Field capacity” is the total amount of water that can be held by soil - if you leave soaked soil to freely drain while preventing evaporation, the a upmount of water left in the soil after it has reached a steady state is the field capacity of that soil. People who seem to know what they are talking about say that field capacity is the ideal amount of water for plants to grow [citation needed]. Field capacity is highly dependant on the type of soil, but for my potting mix it is theoretically about 55% VWC.



Part Three: Sensor-sational (no really that was the best title I could come up with for this section)


Alright, so I want to be able to measure the volumetric water content in a way that can be interpreted by my little arduino. How can I do this?


Well, which measurable features of the soil change as the water content of the soil changes? The soil’s density changes - so I could measure the weight of the pot to calculate the VWC. The soil’s color changes, so I could use some kind of color sensor (yikes). The soil’s electrical resistivity and permittivity, two very natural properties to measure using a circuit, also change with water content.


Since water is a better conductor than dry soil (tap water has a resistivity of about 1-5 ohm-meters, compared to 100-300 ohm-meters for dry soil, see here), why not just run a known amount of current through the soil and check the voltage drop:


ΔV = IR


Since the resistance will be a function of the amount of water, and deltaV is proportional to the resistance, that should give us a good idea of the amount of water present!


A few problems. Running current from one electrode to another through a solution of water and salts is called an electrolytic cell, and causes electrolysis. In this case, the electrodes are made of metal (I thought about doing this with iron nails as the electrodes), and the salts are whatever garbage is dissolved in the dirt-water.


Chem 101 review: atoms have an equal number of protons and electrons. When a metal atom loses some number of electrons, it becomes a cation (positively changed ion), a soluble particle that can join with an anion (negatively changed ion) to form a salt.


As electrons are pumped into the cathode, the positive terminal, they will join up with the cations floating around in the soil, so a film of metal, composed of whatever mineral ions were floating around in the soil, will form over the cathode. This is no biggie, because all metals are pretty conductive, so this won’t change the behaviour of the cathode a lot. 


At the ANODE, however, electrons are getting sucked OUT of the electrode, OUT of the iron and zinc atoms, converting the iron or zinc into their ionic forms. Iron and zinc salts (probably iron and zinc hydroxide in this case, ie rust) are terrible conductors, so if we left this resistive sensor running, as rust built up on the anode, the measured resistance would slowly rise over time, regardless of how much water was in the soil, and the readings from the sensor would start to become biased.


Another thing that will change with water concentration is the permittivity of the soil - that is, “for a unit voltage across two unit-surface-area parallel sheets of conductive material, separated by one unit distance of soil, what is the maximum amount of charge the collects on those metal plates?” Permittivity times the surface area of a plate, divided by the distance between the plates, give the capacitance of a capacitor. Measure the capacitance, do some math on it, and then you can find the VWC! And since there’s (theoretically) no current actually flowing between the capacitor plates, corrosion should be minimal.


So I decided to buy a capacitive moisture sensor instead of making one, because I am a coward and because someone better at this stuff than I am told me I should. I settled on this one, which was cheap.


This sensor outputs an analog voltage, and I knew that this voltage was tied to water content of the soil in some way, but I did not know how this voltage value related to VWC, which is what I really want to measure here. (I’m not really sure what the sensor is returning - the capacitance? The dielectric constant of the soil? Is it applying any kind of function to it’s raw measurements? None of this is in the specs, which can be found here; if you know please tell me). So I decided to calibrate the thing based off some known VWC measurements. 


The first step in calibrating my sensor was to get some dry soil, so I took some potting mix and dried it in the oven at 180C for about an hour. Then I mixed up various volumetric combinations of the dried soil and water, and measured the output of the sensor at the recommended depth. Having a bunch of dirt in my kitchen was pretty awful, so I didn’t collect as much data as I would have liked.


Vol H2O

Vol Total

VWC

Voltage

0

250

0

515

50

250

0.2

330

100

250

0.4

275

150

250

0.6

250

200

250

0.8

245

250

250

1

240



The signal had enough noise that I rounded it to the nearest 5 units after averaging several trials. Having a visual inspection of the data:



Well, that looked a lot like an exponential decay to me, but eyes can be deceiving and math is cool, so I compared 3 models: linear regression, exponential regression, and quadratic regression. You can see that each model reports the Residual Sum of Squares (a measurement of model accuracy), and AIC (the Akaike Information Criterion) and AICc (the second-order AIC). The AIC is a measure that combines model accuracy with model simplicity; it compares the performance of different models while penalising more complex ones to prevent overfitting (see: the bias-variance tradeoff). All of these statistics are lower for better models, and higher for worse ones.



(AICc is a model that supposedly adjusts for AIC’s biases when working with a low sample size, but our sample size is REALLY low, so even AICc doesn’t necessarily mean a heap.)


But anyway, my statistics agrees with my eyes, so I implemented that exponential model into C code, which looks like this:


    int level = analogRead(MOISTURE_SENSOR_PIN);

    if(level>240 && level<=510){

     level = log((double)((level-240))/274.0) * (-182);

    }

    else if (level<=240){level = 0;}

    else{level=100;}


And when I stuck the sensor into some nice, moist soil, I got a reading of 40%, which is lower than, but near to, the theoretical field capacity of my potting mix (of around 55%).



Part Four: Getting my pump on


I purchased a peristaltic pump, which works the same way your gut does - by squeezing one end of a flexible tube, pushing the water forward. The advantages of this - water never comes into contact with electrical components at all, and the flow rate can be measured perfectly.


Anyway, I went with a 6V model, even though I was going to be powering it with 5 volts. And I hooked it directly up to a 5V arduino pin with a small resistor and...it didn’t work.


I could not figure out why this was, because I was used to theoretical electronics but not real-world ones.


Theoretical voltage sources obey Ohm’s law - they supply a constant voltage, and whatever resistance they come across, they supply the necessary electrical current to maintain that voltage. This is in contrast to theoretical current sources, which supply a constant electrical current no matter what voltage that requires of them. Of course, in the real world there are hard limits on BOTH the voltage and the current that a source can supply. My pump required 3 watts, which at 5 volts is 600mA. The maximum an arduino I/O pin can supply is 40mA.


Luckily, I had a transistor handy and hooked that up to the I/O pin. Transistors are electrical switches - run a little current through the base and it “closes the switch”, allowing a large amount of current to flow through from some other source to a target. After this, my motor happily buzzed away, so I measured the amount of fluid it pumped over one minute and divided by 60 to get the volume-per-second measurement. And then I drew up the final software to water my plant when needed, and finally put seeds in soil.



Part Five: The Fruits of my Labours


Anyway, the thing worked perfectly for two weeks until I had to take it apart because I moved to Spain. View the final code on my github.

About Viv

Mid-twenties lost cause.
Trapped in a shrinking cube.
Bounded on the whimsy on the left and analysis on the right.
Bounded by mathematics behind me and medicine in front of me.
Bounded by words above me and raw logic below.
Will be satisfied when I have a fairytale romance, literally save the entire world, and write the perfect koan.