Dismiss

Automatic Garden Watering System – Controlling A Solenoid Valve with Arduino by A Plantmate Capacitive Soil Moisture Sensor

In this tutorial, we will show how to use an Arduino to DIY a Garden Automatic Watering System.
We will be controlling a water solenoid valve with an Arduino, to turn the water valve ON/OFF by detecting the percentage of the soil moisture through the soil moisture sensor. Once the percentage of the soil moisture is set up, the system will turn on the water valve automatically to water your garden while the soil moisture is under the moisture percentage.

Things Used In This Tutorial:

  1. Arduino UNO board
  2. Plantmate DC 5V to 3.3V, 5V, 6.6-12.6V converter & Water Comparator Module
  3. Plantmate Soil Moisture Sensor
  4. DC 12V Water Solenoid Valve
  5. Jumper wires
  6. 100 Feet Telephone extension cable
  7. 4 Pin Mini-USB to open-end cable
  8. Breadboard
  9. Duct tape
  10. Heat Shrink Tubing
  11. Zip Ties
1. Arduino UNO board2. Plantmate DC 5V to 3.3V, 5V, 6.6-12.6V converter & Water Comparator Module3. Plantmate Soil Moisture Sensor4. DC 12V Water Solenoid Valve
5. Jumper wires6. 100 Feet Telephone extension cable7. 4 Pin Mini-USB to open-end cable8. Breadboard
9. Duct tape10. Heat Shrink Tubing11. Zip Ties

Tools:

  1. Soldering iron
  2. Wire stripper cutter
  3. Small Flat Head Screwdriver
  4. Multimeter (option)
Soldering ironWire stripper cutterSmall Flat Head ScrewdriverMultimeter (option)

Software:

Arduino IDE V1.6.5 or later.
Download link: https://www.arduino.cc/en/software

Get Things Ready:

Solder the 6 pins to the module pinholes if needed.
Set the output voltage to
12.6V by turning the
potentiometer clockwise
all the way.
If you have a multimeter,
make sure the output
voltage on the
pin 6 is 12.6V.
Connect the Jumper
Cap on the 5V jumper
pinhead while using a 5V MCU

Extend the moisture sensor cable to 100 feet:

Because the Micro-USB cable of the Plantmate Soil Moisture Sensor is about 3 feet only, it is long enough for indoor plant use. but if you are going to use it for the outdoor garden, we have to extend the length of the cable. Not sure what the max length of the cable can be. But today we are going to use a 100 feet telephone extension cable for this project. any 3 or 4  conductor wires should work. 

Cut off the 2 adapters and the sensor cable.

Strip the wires and solder them to match the colors as below picture:

I like to cut them in different lengths so they won’t be short-circuiting when they bond together.

Then use the heat-shrink tubing to seal them.

Now the soil moisture sensor with a 100 feet long cable is ready!

Solder the solenoid valve to a mini-USB cable:

The solenoid valve didn’t come with any cable or wires. To connect the solenoid we need 2 wires( a positive wire and a negative wire).

The DC Converter that we use can converter the DC 5V up to 12.6V, however, if we want to control the current on/off as a relay does. the connection has to go through the mini-USB port on the module. so we chose the mini-USB male to 4 wires open-end cable.

we only use the black and red wire of the mini-USB cable. abandon the other 2 or 3 wires.

Cut the white and gray wires to different lengths.

Use duct tape to tighten them.

The best way to connect the solenoid is using those 1/4″ quick connects(female spade terminals). I just soldered the wires to the tabs as shown in the pictures below.
The connections on the solenoid do not matter, the coil does not care which side is positive or negative.

To secure the cable, I used the zip-tie to tie the cable.

Now, the solenoid valve with a mini-USB cable is ready!

The Schematic:

The power:

Connect the Type-C USB to a 5V power source.

On the converter module, the output pin 5 delivers the 5V power to supply the Arduino Uno via the 5V red line.

The 3.3V red line from the Arduino Uno 3.3V pin to the micro-USB VCC pin is the power supply for the moisture sensor.

The solenoid 12V power is from the mini-USB adapter VCC pin on the module.

The Controlling:

The Arduino Uno receives the soil moisture data from the moisture sensor via the Analog pin A2.

The Arduino Uno compares the moisture data to the percentage of the moisture we set in the codes.

Once the percentage is lower than the setting in the codes. the Arduino Uno will send a ‘HIGH’ signal to the Digital pin 2, the yellow pin 2 connects to the pin 3 on the module, If Digital pin 2 is ‘HIGH’, the circuit (12V from the mini-USB VCC pin to the solenoid valve then back to GND on the mini-USB pin) is closed, the solenoid valve is triggered to open and the water will go through the valve.

Otherwise, the circuit is open and the solenoid valve is closed

Connect all the hardware:

This is the picture of the finished wiring.

Upload The Code:

/* Setting:

==========================================================================================================
==========================================================================================================
*/

int moisture =90;                    // Moisture level 0-100, change this number that you want to keep the moisture level at.
int loopTime=5000;                   // Time in microsecond for the solenoid valve open period, 5000=5 seconds, change it to what your prefer.
 

int dry_Val = 554;                   //Replace this value with the value from your soil moisture sensor when it is on air dry.
int wet_Val = 284;                   //Replace this value with the value from you soil moisture sensor when it is completely in water.

/*
============================================================================================================
============================================================================================================
*/
 
#define moistureSensor A2            // pin that moisture senosor sends data to Analog pin A2.
#define waterControl 2               // pin that controls the solenoid valve to Digital pin 2.
 
void setup() {   
  pinMode(moistureSensor, INPUT);
  pinMode(waterControl, OUTPUT);
  
  digitalWrite(waterControl, LOW);   // keep the water valve off.
  
  Serial.begin(9600);
  
}

void loop() {
 
 int sensorVal = analogRead(moistureSensor);
 
 int moisturePercent = map(sensorVal, dry_Val, wet_Val, 0,100);
 
 if (moisturePercent > 100){moisturePercent = 100;} //correct the percentage to 100% if read over 100.
 if (moisturePercent < 0){moisturePercent = 0;}     //correct the percentage to 0% if read less than 0.

 if (moisturePercent < moisture){

     Serial.println("Watering...");
     digitalWrite(waterControl, HIGH);
     delay(loopTime);
     digitalWrite(waterControl, LOW);
    }
    
 Serial.print("Sensor Value: ");Serial.print(sensorVal);
 Serial.print("  Moisture Percent: "); Serial.print(moisturePercent); 
 Serial.println("%");  // print out water sensor value to monitor.
 
    delay(5000);
 }
 

Understanding The Code:

line 7: int moisture = 90; when the moisture on the moisture senor is under 90%, the solenoid valve will be on, change the number as you need.

line 8: int loopTime=5000; Time in microsecond for the solenoid valve open period. 5000 is 5 seconds.

line 11: int dry_Val = 554; Leave the moisture sensor on air dry, run the code on your IDE and open your IDE monitor and read the Sensor Value from the pop-up window and replace it with your sensor value data.

line 12: int wet_Val = 284; put the moisture sensor in water completely, run the code on your IDE and open your IDE monitor and read the Sensor Value from the pop-up window and replace it with your sensor value data.

Leave a Reply

Your email address will not be published. Required fields are marked *