My DIY smart garden project for small plants — Arduino script included
I actually posted online on some social network and put the code on my GitHub before. To make sure things I have done can replicated and also just for the sake of documenting myself, this article will guide you, who is either tech geek or just looking for a way to make an automatic plant watering system, from getting the material to wiring and writing a code.
Let’s begin with how did I start all this. I got green onion (AKA Spring onion that’s what Australians call it, AKA ต้นหอม in Thai), basil (Sadly, No it’s not Thai basil, but still possible to do Phat kaphrao), and coriander (ผักชี). I found that a youtuber and fellow engineer Grady Hillhouse from Practical Engineering did something similar to what I intend to do (Arduino Garden Controller — Automatic Watering and Data Logging — YouTube). The problem is the scale: his garden is in a backyard outdoor lawn, but mine is small and indoor, and that is where the modification begin. In other words, this project is perfect for you if you just have a flower in a plant pot but often forget to water it.
For the wiring on the breadboard, there are 3 circuit (2 overlapping each other + 1 of I2C), and 2 source of electricity. The first circuit is 12 V for the pump I used, the second circuit is a parallel (not really) of the CdS light sensor (powered by my Arduino 3.3V pin) and a LED to go to a transistor (that is controlling the water pump like a switch from digital pinout) . The I2C is for the soil sensor. You might also notice I have a data logging shield stacking on my Arduino with a SD card.
Here are the list of apparatus for this section.
- Arduino Uno Rev3 — Arduino Online Shop (you can use other variation of Arduino)(or even Raspberry Pi if you are a Python-only person)(don't forget the cable+ some phone charger USB adapter)
- Adafruit Assembled Data Logging shield for Arduino : ID 1141 : $13.95 : Adafruit Industries, Unique & fun DIY electronics and kits
(+Shield stacking headers +coin battery ) - Photo cell (CdS photoresistor) : ID 161 : $0.95 : Adafruit Industries, Unique & fun DIY electronics and kits
- TIP120 Power Darlington Transistors — 3 pack : ID 976 : $2.50 : Adafruit Industries, Unique & fun DIY electronics and kits
- Adafruit STEMMA Soil Sensor — I2C Capacitive Moisture Sensor [JST PH 2mm] : ID 4026 : $7.50 : Adafruit Industries, Unique & fun DIY electronics and kits (or get something better — will explain why soon) (and you might need JST PH 2mm 4-Pin to Female Socket Cable — I2C STEMMA Cable [200mm] : ID 3950 : $1.50)
- Breadboard, some jumper wires mostly M-M, Resistors, and LED — Any electronic starter kit usually include them all.
I put it separate in a box with lid to prevent water. Speaking of water, I have water source which is just a Starbuck Venti cup raise higher from the plant pots (The higher, the better fluid flow according to Bernoulli Equation). The silicon tube then got split into 4 (which is not necessary if you have just a 1 pot of plant). In fact, I encourage you to do 1 pump to 1 outlet if your have the money, because the water sometimes does not flow thought all outlet as intend due to small push (that’s why I raise the water source as high as possible)
You can also be a plumber and connect clean water pipe with — Plastic Water Solenoid Valve — 12V — 1/2 Nominal : ID 997 : $6.95 : Adafruit Industries, Unique & fun DIY electronics and kits — which is what Grady intend before I modified the plan.
- Peristaltic Liquid Pump with Silicone Tubing — 12V DC Power : ID 1150 : $24.95 : Adafruit Industries, Unique & fun DIY electronics and kits
- T-Connector For Silicone Tubing — 5 Pack : ID 4662 : $4.50 : Adafruit Industries, Unique & fun DIY electronics and kits
- Silicone Tubing for Air Pumps and Valves — 3mm ID — 1 Meter Long : ID 4661 : $2.50 : Adafruit Industries, Unique & fun DIY electronics and kits
- Small Alligator Clip to Female Jumper Wire Bundle — 6 Pieces : ID 4304 : $3.95 : Adafruit Industries, Unique & fun DIY electronics and kits
- 12V DC 1000mA (1A) regulated switching power adapter — UL listed : ID 798 : $8.95 : Adafruit Industries, Unique & fun DIY electronics and kits
- Female DC Power adapter — 2.1mm jack to screw terminal block : ID 368 : $2.00 : Adafruit Industries, Unique & fun DIY electronics and kits
Once everything is assembled, let’s start playing with the code. The full script is here in The “test04” folder of korawichkavee/SmartGardenSmallPlantArduino (github.com). Most of the code are taken from manufacturer tutorial and documents. (Please read the manual!) There are some adjustment you will likely have to make — especially Calibration and conversion
Some of you just want to automate watering without any logging. If this is what you want, just do it without logging shield and delete any logfile print in the code. Also, you might want just the record of time pass not the actual real world clock. You can also do it without any clock hardware. But if you insist on the logging like me, I have some warning and please heed, the delay function may look useless, but without them, the new file might get created more than you intend, like it took sometime to warm up before finishing the first iteration for some reason. This Arduino phenomenon is still a mystery to me. The file is CSV and as nature of CSV, use comma to mark new column. The “println” makes new line.
RTC_PCF8523 rtc; // define the Real Time Clock object
DateTime now;// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;// the logging file
File logfile;
Light sensor datasheet will help you figure how much R you should use for your sunlight exposure. Use your smartphone with some LUX measuring app to do some linear regression.
photocellReading = analogRead(photocellPin);
sunlight = photocellReading*0.8269-80.846 ; //LUX
delay(20);
Adafruit soil sensor can measure temperate just fine, but I cannot say the same for moisture. If you are doing not for fun but other serious business, get a better soil sensor! I start calibration by poking it into super dry soil, note the reading, then add water a bit, do this until it is full of water like dirt water. Then assign value with 100 as a maximum.
//Collect Variables
float soilTemp = ss.getTemp();
delay(20);
float soilMoisture = ss.touchRead(0)*(-0.0683) + 80.512;
delay(20);
The condition here works for my garden. You can also change the timing to suit your garden and water resource. Think about how many times do you want to water each day? or how much (or how long of the duration) should the system water each time.
if ((soilMoisture < wateringThreshold) && (wateredToday < 3 ) && (sunlight > 150) && (now.hour()- recent_water_h > 4 )) {
digitalWrite(pump,HIGH); //turning on
delay(60000); //milliseconds
digitalWrite(pump,LOW); //turning off
recent_water_h = now.hour();
//record that we're watering
++wateredToday;
}
To wrap up, everything should work out just fine if you can find the material & read some datasheet. If it doesn’t work, reply to this article and I will see if I can help. Follow me on Medium please. I want 200 followers on Medium.