As part of the Content Creation course for my MLIS degree (Our class blog: http://scumakers.wordpress.com/ ), I needed to come up with a final project and wanted to learn to use an Arduino. In the end I came up with this device to automatically sense when the soil in my garden is dry and turn on a switch to water it.

I know there are other devices out there that do the same thing and probably do it better, but other than using the little idea here and using some existing code and I came up with this method on my own.

If you are ever stuck anywhere use the Arduino Forums for help. If you have a question about something, the chances are somebody else has asked it before... no matter how simple or silly you may thing it is.

I also had to reteach myself basic electronics and the Spark Fun Tutorials were great from that.

This project consists of three circuits that when broken down individually aren't that difficult. I'm going to diagram them each and then we will take a look at everything put together.

Parts needed for the Curcuits:

NPN transistor

12 V solenoid

12 V battery

## Step 2: The Relay Circuit

This circuit allows the Arduino (which runs at 5V) to turn on the water valve (which runs at 12V). Without this type of switch, it wouldn't be possible (or would be really difficult) to get these two different pieces to work together.

Parts:

5v Relay

10K resistor

NPN transistor

12 V solenoid

12 V battery

11 Jumper wires

## Step 3: The Soil Sensor Circuit

This is the most simple circuit of the three and requires the Sunkee Soil Hygrometer. Another brand of Hygrometer will work as well, but the wiring might be a little different.

This one comes with it's own little shield that has four pins on it. The first (from left) goes to 5v on the Arduino, the second to ground, the third is the digital pin, and the fourth is the analog. We won't be using the digital pin, as that just tells the Arduino two values - High or Low, we actually want the analog pin that will give the Arduino a value from 400 to 1023. By doing this, we can be more specific about when we want the valve to turn on.

Parts:

Sunkee Soil Hygrometer

3 Jumper wires

## Step 4: LCD Circuit

This is definitely the most complex circuit of the three. I have mostly freed it from the breadboard so that I could put it in the lid of the case. The only things that are attached are the power wires and a potentiometer to adjust the brightness of the LCD.

Parts:

16 pin LCD

Potentiometer

14 Jumper Wires

## Step 5: That's All of the Circuitry

That's it for circuits, if you put those three together on one Arduino and one breadboard, we are ready to move onto the code and then the box for it!

## Step 6: Let's Do Some Code!

Thankfully we have some resources online that provide us with most of the code, we just have to get it to work together.

This is the code for the Sunkee Sensor from Steve Spence's Blog

//The (int soil=0) is the soil variable which turns the reading that comes in on the sensor into a %, this will be an important variable in this project

int soil=0;

// the setup routine runs once when you press reset, and sets up communication with the serial port:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

}

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

sensorValue = constrain(sensorValue, 485, 1023);

// print out the value you read:

//Serial.println(sensorValue);

//map the value to a percentage

soil = map(sensorValue, 485, 1023, 100, 0);

// print out the soil water percentage you calculated:

Serial.print(soil);

Serial.println("%");

delay(1000); // delay in between reads for stability, the delay is in millieseconds so this is a 1 second delay

}

The following is public domain code used from the Arduino website. This code sends whatever message we want to the LCD Screen. In this instance it displays "Hello World" on the first line and a count of the seconds since reset on the second.

#include

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("hello, world!");

}

void loop() {

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(millis()/1000);

}

## Step 7: Putting the Code Together

The next step is to modify those two pieces of code and then put a loop in it that will turn on the relay when soil moisture falls to a certain point. When that is all said and done, it looks like this:

//This first section sets up our variables and initializes the LCD screen

int soil=0;
int Relay = 7;

int relay_level=60;

#include

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600); // set up the LCD's number of columns and rows:

}

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

sensorValue = constrain(sensorValue, 485, 1023);

// print out the value you read: Serial.println(sensorValue);

// initialize the digital pin as an output.

pinMode(Relay, OUTPUT);

//map the value to a percentage

soil = map(sensorValue, 485, 1023, 100, 0);

// print out the soil water percentage you calculated to the LCD Screen:

Serial.print(soil);

Serial.println("%");

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.clear();

lcd.print("Soil Saturation:");

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(soil);

lcd.print ("%");

// delay in between reads for stability

delay(1000);

// This next loop turns on the relay for one minute if the moisture is below the level we want (in this case 60%), if the moisture is above that level, it keeps testing. If the level of moisture is below that level it turns the relay (and subsequently the water valve for as long as you would like (in this case it is set for 60000 milliseconds (1 minute)). It then waits another minute before testing the soil again in order to see if it is wet enough.

if (soil < relay_level) {

digitalWrite(Relay, HIGH);

// turn the Relay on (HIGH is the voltage level)

delay(60000);

// wait for a minute

digitalWrite(Relay, LOW);

// turn the LED off by making the voltage LOW

delay(60000);

// wait for a minute

}

}

## Step 8: We Then Compile the Code From the Arudino Window and Send It to the Board.

Here is a link for the software:

http://arduino.cc/en/Main/Software

All this code stuff is confusing, the best way to learn it is to read through the tutorials and then play around with it. Some great tutorials are here:

http://arduino.cc/en/Tutorial/HomePage

## Step 9: Installing It in the Container

I bought a Plano box for about \$15 which is more than I wanted to pay, but I did soak it with water and it stayed dry so I have no complaints.

## Step 10: Spot for the LCD Screen

I traced the screen with a pencil and then used a Dremel cutting blade to cut the hole to a rough size. I then used a small Dremel grinding wheel to get the fit perfect.I put clear silicon caulk on top of the board and installed it in the hole. I then put a small bead on the top side around the screen. Before doing this, make sure your wires are connected as you will no longer be able to see the labels on the board.

## Step 11: Drill Holes for 12V Battery and Sensor Wires

I did a small enough hole, so that only one wire lead would fit at a time, slipped them in and put caulk around the inside and outside.

## Step 12: Installing the Solenoid Valve

I drilled a hole first, and then ground it bigger with the Dremel. I then caulked the inside and outside. I was worried this wouldn't hold very well, but it has worked well.

## Step 13: Hose Extension Piece

I didn't always want to be taking a piece of hose directly off of the solenoid valve, so I secured a small hose extension to it. You will want a female connection on both sides so it works for a standard hose rig. The connections can be purchased at any home store.

## Step 14: That's Really It....

I hooked it up to my garden hose on one side and a drip hose on the other to water some hostas in my garden. All the important stuff worked great. It turned the hose on for a minute when it was too try and then waited another minute to see if it had been watered enough.

My connections leaked though.... there was too much pressure in the hose and even the factory pieces leaked. I'm ok with that though as I want to eventually have a gravity feed from a rain barrel.

That brings us to the final part...

Possible improvements:

Run the Arudino from a solar panel

A water proof housing for my 12V battery

Gravity fed system from a rain barrel (this could be a problem as sometimes the water pressure is needed to turn the valve on.

<p>Forgive me if I'm wrong, but the NPN transistor acting as a switch here would eliminate the need for the relay altogether? At first glance, seems redundant?</p>
<p>Relay is used to drive the solenoid valve. There's no way the Arduino is capable of powering a solenoid valve on its own.</p>
<p>You're right it can't provide the current or voltage to run the solenoid alone, but I actually decided to use a digital out to drive the base of a 2n222 NPN transistor. This then &quot;switches&quot; the solenoid on. I used the same method to turn the power to the sensor off when it's not time to take a reading.</p>
<p>You can definitely use a solenoid that is gravity fed with little to no water pressure. This is one that I've used with only a 1/4 inch of water which does not effect the performance of the solenoid. </p><p>https://www.amazon.com/gp/product/B007N0DN3Q/ref=oh_aui_search_detailpage?ie=UTF8&amp;psc=1</p>
<p>when i run your code it gives me an error at &quot;#include&quot;</p><p>can anyone help me?<br></p>
Most likely because it is not mentioned what to include. It should have been #include LCD.h;
<p>very nice! good explanation!</p><p>take a look of <a href="http://www.arduiot.com/u1" rel="nofollow"> www.arduiot.com/u1 </a> that I thing will help in this project.</p>
<p>can i change the valve to a pump and do i need to change the code if i change it to a pump?<br></p>
<p>I did not find the sketch on the link above,can you send to me the sketch please.thank you</p>
<p>When listing parts it is a good idea to list the value of the part you are using. i.e. resistor isn't a good idea but 10K is excellent which you did...kudos but NPN transistor needs a value... was it a PN2222, 2N3904, 2N4001? this was we can duplicate your circuit and get the same results and if it doesn't work we wont be going well did I get the right parts? otherwise a great instructable.</p>
<p>*suggestion* If you were to put a shut off valve in line with your hardware you could limit the pressure coming in by closing the valve partway and still have enough pressure to turn the device. </p>
Great project. Thinking of starting an automated gardening system wanted to use grav system also. You mentioned earlier they need minimum pressure this is pricey but seems to be a solution http://www.adafruit.com/product/996
<p>Hi there. The project sounds very interesting indeed but can you help by putting a wiring diagram up ? also is there a sketch I can download for better import into the arduino? i am having difficulty writing the sketch as presnted</p><p>thanks</p><p>Mohan</p>
<p>First of all, this is a great project! I'm going to adapt it to something similar for my garden.</p><p>@Benadski - How would you recommend turning on and off the sensor using the Arduino code that he has here? I'm new to coding with Arduino, so I know I'm in a little over my head. But I'd love to see how that function is written.</p>
I don't have the sensor turn off and on at this point, so it is always on. When I get a chance, I want to write the code for that part. I'll let you know when I have it done.
Nice project! Try to turn the sensor power on just before measuring and turn it off just after that. This way the sensor lasts months instead of hours.<br><br>For my own projects I use AC on the sensor to make it last years, but it makes things a bit more complex.
<p>Interesting.... thanks for the tip I'll try that!</p>
<p>interesting project. made something similar :-) ( </p><p><a href="https://www.instructables.com/id/Arduino-for-Greenhouse-Garden-or-Growbox/" rel="nofollow">https://www.instructables.com/id/Arduino-for-Greenh...</a> ) will read yr article and see if I can improve mine.</p><p>With regard to the sensor, I indeed implemented to switch that off. I however use the analogue input to read the sensor, whereas you I presume read the digital output of the Sunkee Hygrometer, so you would have to switch off th eentire Sunkee circuit.<br><br>Indeed, using AC makes it more complex, but just briefly switching it on already makes a big difference.<br>Anyway, great project, let's see if i can steal some of yr ideaś :-)</p>
<p>My mistake... u are using the analog output of your Sunkee :-)<br>I have one of those things as well (different brand) and I got the impression that the analog output actually directly connects to the sensor itself... ergo... you could use the sensor and dump the board</p>