# Soil/Moisture Detection System

2,722

34

Having started out as a simple two nail, 5V, moisture detection system it's grown into a slightly more sophisticated rig with LED's and less updating due to my previous oversight: electrolysis.

For this, you'll need:

• Arduino
• 1 x Red LED
• 1 x Yellow LED
• 1 x Green LED
• 1 x Blue LED
• 4 x 220 ohm resistors
• 2 x 10K ohm resistors
• 2 x hefty nails
• 1 x push button
• wires - lots of wires

The basic idea: stick +5V through one nail, and detect the resistance via the other nail.

The basic problem: the constant +5V through the nail will rust it incredibly fast.

The basic solution: only check when the button is pressed.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Layout

The layout shouldn't cause you much problem. If I can do it anyone can do it!

The red, yellow, and green LEDs are for the moisture status (bad, good, perfect). The blue LED is just a fake 'thinking' light which briefly comes on when you press the button. It's not necessary, but it looks nice.

## Step 2: Code

The code isn't anything to write home about. It's pretty basic as I'm no expert programmer by any stretch of the imagination.

```int moistureSensor = 0;            // nail to read from is pin A0int moisture_val;

const int redLEDPin = 3;           // red LED connected to digital pin
const int yellowLEDPin = 4;        // yellow LED connected to digital pin
const int greenLEDPin = 5;         // green LED connected to digital pin
const int blueLEDPin = 7;           // blue LED connected to digital pin
const int buttonPin = 9;           // button connected to digital pin

const int voltage = 8;             // the voltage nail is on pin D8

int buttonState = 0;           // initialise button as off

void setup() {
Serial.begin(9600);             //open serial port
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);   //set the LED pins as outputs
pinMode(yellowLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}

void loop() {

if (buttonState == HIGH) {
digitalWrite(blueLEDPin, HIGH);

digitalWrite(voltage, HIGH);
Serial.print("moisture level: ");          //
Serial.println( moisture_val );            //print moisture level 0-1024

delay(1000);        // fake thinking...
digitalWrite(blueLEDPin, LOW);

if (moisture_val < 170)  {
digitalWrite(redLEDPin, HIGH);      // if moisture is less than 190 have red on, others off
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
}
if (moisture_val < 200 && moisture_val > 170)  {
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);   // if moisture is less than 230 but over 190 have yellow on, others off
digitalWrite(greenLEDPin, LOW);
}
if (moisture_val > 200 && moisture_val < 240)  {
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);     // if moisture is over 230 and less than 300 have green on, others off
}
if (moisture_val > 280)  {
digitalWrite(redLEDPin, HIGH);      // if moisture is over than 300  (possibly flooded) have red on, others off
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
}
digitalWrite(voltage, LOW);          // reset button state
delay(5000);                          // wait 5 ses

digitalWrite(redLEDPin, LOW);      // if moisture is over than 300  (possibly flooded) have red on, others off
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);

}
}```

As per normal we start by assigning pins to labels.

The void setup stuff is just opening the serial port and assigning a mode to the LED pins.

The void loop is where the good stuff happens. IF the button is pressed. We light the blue LED, switch the voltage on to one nail, read the value from the other nail, pause for a bit (to let you see the blue LED on) then turn the voltage to the nail off. Now we have a result from the (non-voltage) nail we can run it through some IF statements to see which LED to light.

You'll obviously have to run some tests (by opening the serial monitor) to see which value is ideal for your plant. Then decide what values are so-so and bad.

Once one LED has shown the current result, and 5 seconds have passed, all the LEDs will go out.

The system is now reset and ready for another button press.

The problem of the nails rusting is still present, but much lesser than before.

## Recommendations

• ### Internet of Things Class

22,799 Enrolled