May 27, 2021 by chibatman

Computer, Water My Plants While I'm Away

In Fair Chiba, where we lay our scene, from ancient seeds break new fruits, where urban gardening makes civil hands unclean.

It was 2015, in suburban Tokyo, a west-facing balcony, on the 3rd floor with sufficient light and the Tokyo SkyTree in the foggy distance. Hand-carried shallow planters, bags of dirt and some seedlings. I was creating an oasis of life and flavor in the center of a concrete jungle — fresh and exotic western herbs — inconsistently available and overpriced in the local supermarkets — within my reach.

And especially living in these urban hellscapes, and with all this technology around — I started asking myself: “How can we use technology to make our lives better? By helping us grow plants, of course.”

Wars have been waged — fortunes won — and nations built — for the sake of good food

Now, for all the good food in Japan, do remember — it is an island nation between subtropical and temperate, and was economically isolated until quite recent history. Just as with all island ecosystems, it’s natural diversity is a bit more limited, and, thanks to the Tokugawa’s isolation, missed out on most of the spice trade. You have soy sauce, wasabi, shiso, and…welp, that’s about it. Even with the introduction of chili peppers in the 1600s by the Portuguese, they never caught on like they did in the Korean peninsula.

So, I focused my balcony gardening efforts on the small, pungent, rare and expensive necessities for some good ol' western cooking - parsley, rosemary, thyme, oregano, chili peppers — CHILI PEPPERS!! (more on that later), and occasionally some mint or other herbs.

Terraforming

Now, for those who have never had the joy (or hellscape) of balcony gardening — it’s like an extreme microclimate — If it’s north-facing (in the northern hemisphere), just stop reading and give up right now. If facing south, you have truly been blessed, by your hang-dry-laundry and gardening spirits — and have paid a heavy price to the real estate dealers for it.

Your daylight will be limited by your own walls and balcony dividers, and of course the surrounding buildings. Your shallow planters will dry out under the 30+ degree celsius summers and full sun. you’ll end up watering twice a day to keep everything moist. You are the drip IV keeping these plants on life support.

There was once a fully-automated microclimate up here.

Call me Whetus: Cause I’m just a teenage drip-bag, baby.

Now, I was doing a great job as the drip-bag. But called away to Australia on business for a week — at the end of July, when Japan is at its hottest — surely a death sentence for my little herb patch.

Come on, it just needs water! Can’t you just throw it all in a fish tank?

Nope, need soak and dry cycles, and enough moisture to live through the daylight.

As is customary here, I didn’t know my neighbors, and didn’t have any friends this far out of the big city to give them water even once a day, let alone twice.

So, I took a page out of the book of Self-Reliance, huffed, and said “Nevermind, I’ll do it myself!”

Nature’s Requirements:

Give me:

  • Water
  • Right amount of water
  • At the right intervals

My requirements:

  • Don’t flood mine, or the neighbors' balconies
  • Don’t leave a tap open when traveling overseas

Here’s where it gets a bit technical, so here’s the TL;DR:

Built a system managed by a microcontroller, using an old storage box and pump to water plants when sensor says they need it.

Here’s the dirty, nerdy details:

The Brains

You may have heard of the Arduino platform. It is a homebrew-kind of computer board with a little microcontroller chip. It’s really flexible, and thus, massively overbuilt for most projects — at its core is the $3 ATMega 328 microcontroller chip with 10+ inputs and outputs, built in resistors and LEDs, and most importantly for ease of use, a USB-serial adapter — this makes it very easy to load and reload experimental code with lots of updates.

It’s great as an all-purpose experimental board, especially for beginners - but at $25 a pop, a bit pricey to use for an embedded application. Afterall, once the program is perfected, I just need to burn it to the chip once, and leave it be.

You can use an Arduino to burn programs to other chips, and aside from the big long centipede ATMega 328 chip, there are smaller arachnid-variants, with fewer features, ins and outs, but enough for my application. The $2 ATTiny85 chip was perfect.

Japanese 500 yen coin for comparison.

But a Brain in a jar is exactly that — full of great ideas, but not up for any heavy lifting. As with any computer, it works through a series of inputs, logic, and outputs. The chip is the brain, and we still need to write the logic for it, but let’s get into the BRAWN.

BRAWN Part 1 – Inputs

For the inputs, we need to know WHEN the watering system should activate.

Top and middle - the moisture sensor fork and it’s controller chip

It works like this:

There are two prongs connected to a chip by 2 wires (not pictured), which generates a value and then communicates this to our ATTiny85 chip.

Soil moisture can be measured by resistance — just like water causes a short-circuit, varying levels of purity can increase the resistance, so we can measure the resistance (and thus, the moisture level in the soil) by using this fork-shaped moisture sensor. The higher the resistance, the lower the moisture:

  • Completely dry is completely disconnected - and therefore a value of “0” (0%).

  • Completely submerged in water, and it would be a max value of about “950” (100%).

Since soil is a poor conductor and we’re relying on the water in between, every level of dampness in between returns a value somewhere between.

Brains revisited – Logic

Back to Brains for a moment, now that we’ve got inputs, and a brain to think about it, we need to write logic that can understand it. I am a terrible code writer, and never studied it or anything, so I settled on a basic IF, THEN code. Essentially:

IF soil moisture level is less than 40% OF MAX, THEN turn on a water pump for 45 seconds.”

Side note: Following that cycle: Go to sleep for 12 hours, repeat cycle. (The 12 hour sleep delay was to keep the pump from burning itself out if the tank went dry and it kept trying to pump a dry pot — this could be amended with a tank level sensor, but I didn’t have a good solution at the time, so I settled on a crappy hack.)

BRAWN Part 2 – Outputs

How to get water, but to pump it. And how to turn it on or off? I got a cheap fish tank submersible pump, and one that could run on 5V-12V (since the control chip can run on 5V.) I planned to run everything off a simple USB-charger.

Chips are smart, but weak — they can turn things like an LED on or off, it can’t put out enough power to run a pump — just like your brain signals your muscles to move your arm, but cannot move your arm itself. We needed a muscle.

Enter: The Relay

Relay at bottom

A relay is basically a simple switch, just like you’d have to control the lights in your house. But, a relay isn’t flipped by hand, it is activated by an electromagnet, which is light enough to be activated by a chip.

This means I can connect the relay directly to the chip, and the chip can turn the relay on and off — and the relay in turn, can turn on the power to the water pump. Of course, this on/off is determined by the water sensor telling the chip when to switch it on or off.

Everything Worked!

But in my setup, I had drilled some plastic tubing I set along my planters, and found that 5V wasn’t enough to pump water all the way through the end.

Get Pumped

Thankfully, my pump could run on 5-12V, but this would mean I need 2 separate power supplies (5V for the chip, moisture sensor and relay, and 12V for the pump), or another solution, since that would fry the control chip.

I settled on running the entire device off of a single 12V wall charger, and put a voltage converter inline to step-down the 12V→5V for the chip, sensor and relay.

I also put in those tube-type fuses for the 5V and 12V respectively, and even mounted blue LEDs below them, because heck, at this point, it just looked cool.

Totally NOT a bomb.

Somehow building this didn’t get me put on a no-fly list, and I was able to go on my business trip, and come back to a perfectly-manicured herb garden.