I realized I was missing some pieces, so I made a quick shopping list. Luckily, I was able to find all of them at a nearby electronics shop.
Shopping List
12V 5A Battery
16-14 AWG female disconnect
I spend the next few days finishing the wiring process.
One of the biggest challenges was operating the system using an external power source, a 12V battery. Normally, I would just use my computer or barrel jack to power the microcontrollers.
I ended up frying a NANO board, so I had to use UNO, which was available at the Echo campus 🥲
I then secured the boards on a clipboard that wasn't being used.
The valve closes (you can hear it click in the video!) when the sensor is in a glass of water and opens when it is in the air.
Next, I calibrated the sensor using two cups of soil, one wet and one dry. To explain how the system works, it is divided into two phases: drying and watering. When the moisture level is below 60%, it enters the watering phase, opening the valve. Subsequently, when the moisture level gets above 60%, it enters the drying phase. The valve is closed until the moisture level gradually drops to 40%, in which the watering phase begins again. The system iterates through these phases continuously.
There are several reasons behind this decision. First, this method prevents the saturation of soil by spacing out the watering phases. Soil saturation and erosion are detrimental to plant growth, as the nutrients may wash away from the soil with water. Second, farmers can save resources, such as electricity and water, because the valves would open less frequently.
Below is how I designed the code!
Code
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int AirValue = 585;
const int WaterValue = 85;
const int mosfetPin = 2;
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
pinMode(mosfetPin, OUTPUT);
}
void displayMoisture(int num, int per, bool state) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
if(!state) display.println(F("Drying"));
else display.println(F("Watering"));
display.print(per);
display.println(F("%"));
display.print(num);
display.display();
}
void loop() {
int soilMoistureValue = analogRead(A0);
int soilMoisturePercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if(soilMoisturePercent > 60)
{
digitalWrite(mosfetPin, LOW);
while (soilMoisturePercent > 40)
{
displayMoisture(soilMoistureValue, soilMoisturePercent, false);
delay(1000);
soilMoistureValue = analogRead(A0);
soilMoisturePercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
}
}
else
{
digitalWrite(mosfetPin, HIGH);
displayMoisture(soilMoistureValue, soilMoisturePercent, true);
}
delay(1000);
}
Comments