Arduino Soil Moisture Sensor

271,408

229

63

About: As a young lad Tom spent most of his days at the heels of his father, working in their shop, also known as the basement. His dad was an extraordinary cabinet maker and while working on their 1850’s hom...

Intro: Arduino Soil Moisture Sensor

***Edit***

Please use resistors when connecting the LEDs to your Arduino!

This Instructable is old. I have not had the time to update any of the information. You will find a lot of GREAT information in the comments please read them after reading the instructions.

Intro

In this instructable I will show you how to connect the an Arduino Nano and a moisture sensor. The information will then be displayed with 5 LEDs. This is very easy build and I would class it as a beginner project.

Step 1: Things You Will Need

1 - Breadboard (http://www.ebay.com/itm/171705651308?ssPageName=ST...

5 - LEDs

5 - 1k resistors

1 - Soil Moisture sensor kit (http://www.ebay.com/itm/171705525756?ssPageName=ST...

1 - Arduino Nano (http://www.ebay.com/itm/171728876932?ssPageName=ST...

~8 - Assortment of jumpers.

Step 2: Arduino

The Code:

<p>/* </p><p>Innovativetom.com
Flower Pot Soil Mosture Sensor</p><p>A0 - Soil Mosture Sensor
D2:D6 - LEDS 1,2,3,4,5</p><p>LED1 - Green
LED2 - Green
LED3 - Green
LED4 - YELLOW
LED5 - RED</p><p>Connect the Soil Mosture Sensor to anolog input pin 0, 
and your 5 led to digital out 2-6</p><p>*/
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;</p><p>int mostureSensor = 0;</p><p>void setup() {
  // Serial Begin so we can see the data from the mosture sensor in our serial input window. 
  Serial.begin(9600);
  // setting the led pins to outputs
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}</p><p>// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(mostureSensor);
  // print out the value you read:</p><p>  Serial.println(sensorValue);
  
 if (sensorValue >= 820)
 {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 digitalWrite(led4, HIGH);
 digitalWrite(led5, LOW);
}
else if (sensorValue >= 615  && sensorValue < 820)
 {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
}  
else if (sensorValue >= 410 && sensorValue < 615)
 {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, HIGH);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
}    
else if (sensorValue >= 250 && sensorValue < 410)
 {
 digitalWrite(led1, HIGH);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
}
else if (sensorValue >= 0 && sensorValue < 250)
 {
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
}
  delay(1000);        // delay 1 second between reads
}</p>

Step 3: LEDs

***Edit***
Please use resistors when connecting the LEDs to your Arduino!

***Edit***

Connecting the LEDs;

Digital Pin 2 Green.

Digital Pin 3 Green.

Digital Pin 4 Green.

Digital Pin 5 Yellow.

Digital Pin 6 Red.

Connect the cathode or (-) lead from the LED to the Arduino.

**Here you must put a 1k resistor between anode (+) and the positive rail.

Connect the anode or (+) lead from the LED to the + positive rail of the beadboard. **

Step 4: Wiring

In this step we connect the power and ground rails.

From the arduino ground pin connect a short jumper to the blue rail on the breadboard.

From the arduino 5v Pin we connect a short jumper to the red rail on the breadboard.

Bond both rails together.

Step 5: The Moisture Sensor

The moisture has very well defined pin out.

Connect the ground to the ground rail, power to the power rail.

Connect the "AC" Labeled pin on the moisture sensor to analog input 0 pin on the Arduino.

*note, the moisture sensor I have has two outs one labeled "AC" and one labeled "DC". The "AC", is a serial signal 0-5 volts that when fully dry it outputs 5 volts, when fully wet, 0 volts. The "DC", is configured with the trim pot and is brought high when the moisture level reaches a desired point.

Step 6: Your Done.

Connect up the Arduino and load the code;

Code On Github

Innovative Tom

Buy the Kit:

eBay Link

12 People Made This Project!

Recommendations

  • Fix It! Contest

    Fix It! Contest
  • Metalworking Contest

    Metalworking Contest
  • Audio Contest 2018

    Audio Contest 2018

63 Discussions

0
None
SrikarD1

8 months ago

I am getting different values while I am using Arduino Uno and Arduino Nano for the same water content and level. Can anybody plz tell me why? and also how could I fix it? For example when its dry I am getting a value of 400 in Arduino Uno and 0 in Arduino nano through soil moisture sensor. When its wet, its almost the same(~850)

0
None
tinus26

8 months ago

Hi,

I was wondering - can I use a moisture sensor like this in a setup powered by a CR2032 or 9V battery?

0
None
puffykarl99

8 months ago

Hi. i just want to ask what is the output of your soil moisture value. Because were developing a device that measure a soil condition that is: dry, enough water, and drowned your answers will help us alot thankyou somuch

0
None
AniqaE

10 months ago

What are the units of output readings ? Help please

0
None
joyang

2 years ago

Hi! Nice work. How did you select the sensor value ranges? And what is the unit of measurement? Are the output values voltages or some capacitance values?

1 reply
0
None
AniqaEjoyang

Reply 10 months ago

Did you find your answer ?

0
None
amit_123456

10 months ago

can anyone help me in uploading this program in arduino as this is showing some error

0
None
WirelessGuyN

1 year ago

Trying to lean on the experience of others who've been through this before...

Does anyone know if there are samples outthere for interfacing the TE215 sensor with the analog input of something like the NodeMCU?

I'd like ot take continual readings and ship it up to a server for collection.

I have somehting of a sample for doing this with a temp sensor but not something like this one. I'll include the temp info below. I guress I'm kind of lost with the mV value to temp conversion, although I guess I could just pick somehting random for dry versus wet.

Any thoughts for a beginner here?

Sample:

const int ANALOG_INPUT = A0; const int LED_PIN = D1;

void setup() { // Configure ADC pin as input pinMode(ANALOG_INPUT, INPUT);

// The pin our LED is connected to is a digital output pinMode(LED_PIN, OUTPUT);

// Turn the LED off digitalWrite(LED_PIN, HIGH);

// Open serial line Serial.begin(115200);

}

void loop() { // Read the analog input int value = analogRead(ANALOG_INPUT); // Convert the value into a temperature T = (value/1023) * 3300mV / 10(mV/degF) float temperature = ((float)value / 1023.0) * 3300 / 10; // Blink the LED digitalWrite(LED_PIN, LOW); delay(100); digitalWrite(LED_PIN, HIGH);

// Write temperature to the Serial line Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" degrees F.");

// Wait 3 seconds before starting over delay(3000);

}

1 reply
0
None
DougL56WirelessGuyN

Reply 11 months ago

> Does anyone know if there are samples outthere for interfacing the TE215 sensor with the analog input of something like the NodeMCU?

Sort of. I'm using a similar sensor attached to A0 on a NodeMCU board.

Sensor: https://www.banggood.com/Soil-Hygrometer-Humidity-...

The analog output from this particular sensor only varies from about 4.5v dry to 2.5v immersed in a glass of water, so the corresponding analogRead values from A0 have a more limited range than 0..1024.

The only change in your code would be to include the NodeMCU pin definitions:

#ifndef D1

#define D0 16

#define D1 5

#define D2 4

#define D3 0

#define D4 2

#define D5 14

#define D6 12

#define D7 13

#define D8 15

#define D9 3

#define D10 1

#endif

SoilSensor.jpg
0
None
nk23

1 year ago

Just want to know ,how can we transmit this data from the sensor ?what are the communication options available?

Thanks in advance.

NK

0
None
karpada

1 year ago

// Simpler led control

digitalWrite(led1, sensorValue >= 0 ? HIGH : LOW); // Always high

digitalWrite(led2, sensorValue >= 250 ? HIGH : LOW);

digitalWrite(led3, sensorValue >= 410 ? HIGH : LOW);

digitalWrite(led4, sensorValue >= 615 ? HIGH : LOW);

digitalWrite(led5, sensorValue >= 820 ? HIGH : LOW);

0
None
SteveB15

3 years ago on Introduction

Nice job but it does need a few simple refinements to make it practical...

Add 1K resistors to the LEDs and correct the code so that the red LED will light.

The probe only needs to be on while a reading is taken, so poll the probe for one second every half hour by driving it from a digital pin. In my example code I used D7. This greatly improves power consumption and extends the life of the probe by several orders of magnitude since damage by electrolysis will be insignificant. If you want to check after watering, just press reset for a new reading.

Here's my modified code with probe polling:

/*

Innovativetom.com
stevebrace.co.uk

Flower Pot Soil Mosture Sensor

A0 - Soil Mosture Sensor

D2:D6 - LEDS 1,2,3,4,5

LED1 - Green

LED2 - Green

LED3 - Green

LED4 - YELLOW

LED5 - RED

Connect the Soil Mosture Sensor to anolog input pin 0,

and your 5 led to digital out 2-6

*/

int led1 = 2;

int led2 = 3;

int led3 = 4;

int led4 = 5;

int led5 = 6;

int probe = 7;

int mostureSensor = 0;

void setup() {

// Serial Begin so we can see the data from the mosture sensor in our serial input window.

Serial.begin(9600);

// setting the led pins to outputs

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(led5, OUTPUT);

pinMode(probe, OUTPUT);

}

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

void loop() {

// Power-up the probe and pause for the driver

digitalWrite(probe, HIGH);

delay(1000);

// read the input on analog pin 0:

int sensorValue = analogRead(mostureSensor);

// print out the value you read:

Serial.println(sensorValue);

if (sensorValue >= 820)

{

digitalWrite(led1, HIGH);

digitalWrite(led2, HIGH);

digitalWrite(led3, HIGH);

digitalWrite(led4, HIGH);

digitalWrite(led5, HIGH);

}

else if (sensorValue >= 615 && sensorValue < 820)

{

digitalWrite(led1, HIGH);

digitalWrite(led2, HIGH);

digitalWrite(led3, HIGH);

digitalWrite(led4, HIGH);

digitalWrite(led5, LOW);

}

else if (sensorValue >= 410 && sensorValue < 615)

{

digitalWrite(led1, HIGH);

digitalWrite(led2, HIGH);

digitalWrite(led3, HIGH);

digitalWrite(led4, LOW);

digitalWrite(led5, LOW);

}

else if (sensorValue >= 250 && sensorValue < 410)

{

digitalWrite(led1, HIGH);

digitalWrite(led2, HIGH);

digitalWrite(led3, LOW);

digitalWrite(led4, LOW);

digitalWrite(led5, LOW);

}

else if (sensorValue >= 0 && sensorValue < 250)

{

digitalWrite(led1, HIGH);

digitalWrite(led2, LOW);

digitalWrite(led3, LOW);

digitalWrite(led4, LOW);

digitalWrite(led5, LOW);

}

// Power-down the probe

digitalWrite(probe, LOW);

delay(1800000); // wait half an hour

}

8 replies
0
None
MaddisonA1SteveB15

Reply 1 year ago

COULD YOU PLEASE SEND ANY PHOTOS YOU HAVE OF THIS SETUP. thanks

0
None
ABHINAV REDDYSteveB15

Reply 3 years ago

Can u give me the code if I want to place relay instead of led.plz. // ........ Is that the part of coding

0
None
SteveB15ABHINAV REDDY

Reply 3 years ago

Abhinav, just use the code as is but drive the relay from your desired digital pin through a transistor. Don't forget to place a reverse bias diode across the relay coil to protect against back EMF.

0
None
ABHINAV REDDYSteveB15

Reply 3 years ago

Thanks
//power the probe etc
Is that the part of code,which transistor should I use

0
None
froKoJasonEdinburgh

Reply 1 year ago

Yep, and "active low" relays are way more common than "active high" ones.

0
None
OleP1SteveB15

Reply 1 year ago

It's abselutt critical to turn off the moisture sensor.

If you only have 1 or 2 sensor you can get away with using a digital pin as power.

I have several and ended up using a MOSFET.

Im new to both coding and arduino, but for anyone intereseted her is my setup

https://github.com/oleost/WaterSystemArduino

0
None
AdeVSteveB15

Reply 2 years ago

You could save a bunch of code by using a switch() statement with "fall-through" to set your LEDs... I haven't tested this code, so it might need debugging, but here's what I'd do:

// Reset all LEDs
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);

switch(sensorValue) {
case >= 820:
digitalWrite(led5, HIGH);
// normally we'd put "break;" here to exit the select
case >= 615:

digitalWrite(led4, HIGH);
case >= 410:
digitalWrite(led3, HIGH);
case >= 250:
digitalWrite(led2, HIGH);
case >= 0:
digitalWrite(led1, HIGH);
}

By excluding the break; after each case statement, the code "falls through" and runs all the other cases as well. But it only runs from the first case that matches, so if the sensor value was 255 for example, the first matching case is "case >= 250", so LED2 comes on. The code falls through from there & also turns led1 on. Since all LEDs were turned off at the start, leds3, 4 and 5 remain off.

HTH!