Introduction: Arduino Soil Moisture Sensor

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of 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.

Picture of Your Done.

Connect up the Arduino and load the code;

Code On Github

Innovative Tom

Buy the Kit:

eBay Link

Comments

JohnG623 made it! (author)2017-04-17

awesome, worked like a charm.

here's the photos, didn't use a resistor, but did use a arduino uno.

https://github.com/johnantoni/arduino/tree/master/moisture-5-leds

her

So_FARAway (author)JohnG6232017-10-28

Hey! Thanks for posting the pictures, I just have a question, did you use the code provided or did you change it?

JohnG623 (author)So_FARAway2017-10-30

I modified some of the code, have all the source code on github

https://github.com/johnantoni/arduino

documented it all to make it easy, let me know if you get into any problems

happy to help

twitter.com/johnantoni

amit_123456 (author)2017-10-30

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

DavidP788 made it! (author)2017-04-16

Had to modify it slightly as I have an uno, rather then the board the OP used. Tried to make the connections for the leds and the resistors a little clearer for the people new to this. Hope the photos help.

So_FARAway (author)DavidP7882017-10-23

Hey! Thank you for posting the pictures, I'm new to this and was very cofused as I also only have a adruino uno. I was just wondering if you used the code provided or if you wrote your own, if you did, would it be possible for you to post it, as my code somehow doesn't seem to be working.

WirelessGuyN (author)2017-07-23

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);

}

DougL56 (author)WirelessGuyN2017-09-27

> 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

nk23 (author)2017-06-06

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

Thanks in advance.

NK

karpada (author)2017-05-31

// 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);

Daniel Morales made it! (author)2017-04-05

Don't have the moisture sensor yet, havent gotten it in the mail, but played around with Blynk APP, and got it to work, changed the code to work with it using "virtual" LCD display and leds! PS, I'm a NOOB at this.. code here:

#include <SoftwareSerial.h>

SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial

#include <BlynkSimpleStream.h>

char auth[] = "YOUR AUTH CODE FOR BLYNK GOES HERE!";

WidgetLCD lcd (V0);

WidgetLED led1(V1);

WidgetLED led2(V2);

WidgetLED led3(V3);

WidgetLED led4(V4);

WidgetLED led5(V5);

int a;

int humiditya;

void setup()

{

// Debug console

DebugSerial.begin(9600);

// Blynk will work through Serial

// Do not read or write this serial manually in your sketch

Serial.begin(9600);

Blynk.begin(Serial, auth);

}

void loop(){

Blynk.run();

a = analogRead (A0);

humiditya = map (a, 0, 1023, 0, 100);

lcd.print(0,0, a);

lcd.print(0,1, humiditya);

if (a >= 820)

{

led1.on();

led2.on();

led3.on();

led4.on();

led5.on();

}

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

{

led1.on();

led2.on();

led3.on();

led4.off();

led5.off();

}

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

{

led1.on();

led2.on();

led3.off();

led4.off();

led5.off();

}

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

{

led1.on();

led2.off();

led3.off();

led4.off();

led5.off();

}

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

{

led1.off();

led2.off();

led3.off();

led4.off();

led5.off();

}

delay(2000);

lcd.clear();

}

SteveB15 (author)2015-04-12

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

}

MaddisonA1 (author)SteveB152017-03-23

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

ABHINAV REDDY (author)SteveB152015-05-23

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

SteveB15 (author)ABHINAV REDDY2015-05-23

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.

ABHINAV REDDY (author)SteveB152015-05-23

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

if you use a relay module like this then it has the transistor and diode already

http://img.dxcdn.com/productimages/sku_319093_1.jp...

I found with mine that it was a LOW signal that caused the relay to engage. (not a high signal as I expected). It also needed just under 5v to work. Read the datasheet info on whichever module you use.

froKo (author)JasonEdinburgh2017-01-01

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

OleP1 (author)SteveB152016-11-24

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

AdeV (author)SteveB152016-02-28

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!

DaveS101 (author)SteveB152016-01-06

hey steve instead of using delay would it not be better to use a blink without delay method as not to tie up the processor if it is in the middle of a delay you lose all other functions till the delay is over, way better to time it with millis(); function then you can respond to interrupts like button pushes or sensor inputs it is always best to not use delay

setup

long previousMillis = 0;

long interval = 900000; // 15 minutes

main loop

{

unsignedlong currentMillis = millis();

if(currentMillis - previousMillis > interval)

{

previousMillis = currentMillis;

}

else

{

get value from sensor once every 15 minutes

}

}

DaveS101 (author)DaveS1012016-01-06

//working program you can use more millis(); timers for the sensor delays i //just //did not have time to work it up yet i can post it later but the odds of //hitting the delay during a button push are 1 in 500 with 2ms delay i consider //this acceptable i have not noticed any trouble from it

//By Dave Soderbloom
//Quick sketch for water sensor reading without delay to tie up processor for other tasks like interrupts
//pins I used on msp432

int led1 = 2;

int led2 = 3;

int led3 = 4;

int led4 = 5;

int led5 = 6;

int probe = 8;

int moistureSensor = A7;



int sensorValue = 0;

long previousMillis = 0;

long interval = 10000; // 15 minutes900000



void setup()
{


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);

//get and display startup reading before main loop to the serial monitor
digitalWrite(probe, HIGH);
delay(1000);
int sensorValue = analogRead(moistureSensor);
Serial.println(sensorValue);
digitalWrite(probe, LOW);




}

void loop()
{


unsigned long currentMillis = millis();

int sensorValue = analogRead(moistureSensor);

if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;


digitalWrite(probe, HIGH);
delay(2); //super small delay may need to be higher depending on your arduino or launchpad board you are using some take 200 miliseconds to get accurate analog reading



int sensorValue = analogRead(moistureSensor);


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

//may have to adjust ranges depending on a few factors
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);
}
}

SteveB15 (author)DaveS1012016-01-06

Thanks for taking the time to make, not only constructive criticism, but also posting your solution. This is exactly the kind of input we like!

abdullahh52 (author)2017-02-27

Where would you get those specific resistors from?

mohini jena (author)2017-02-05

Can anyone make water sensor without using transistor??

ChhitK (author)2017-02-02

Can you tell me about the unit of sensorValue? Do you know about the ISO unit of soil moisture?

Breadbored (author)2016-11-21

Hello Tom

The AC and DC you refer to are in fact AO and DO (Analogue Output and Digital Output).

AlexanderS230 (author)2016-11-17

I want to built this Project and connect it with a little monitor, but i wonder what the Lifespan of the Sensor is. If you check the Moisture every half an hour.

PekY1 (author)2016-11-10

Hello everyone mind helping out this amateur here, does anyone know how much power it needs to run this set-up? I wanted to power this set-up using a micro hydro generator, i have calculated the amount of watts my generator is able to produce but i have no idea if its enough to power it up =(

JonathanM257 (author)2016-11-07

Hi !! My sensor is showing 870 value when not in the water :s Someone knows why plz? :s

emirbyildiz made it! (author)2016-10-22

Finished and tested. It is working good but solar panel is not enough to charge 18650 battery pack because weather is cloudy todays. I should add two or three solar panel to make it working well.

emirbyildiz made it! (author)2016-05-28

Nice project. I will add a dc water pump motor, 6x18650 battery and a 12V solar panel to make it permanent. Thank you!

PekY1 (author)emirbyildiz2016-10-20

Hello, do you mind showing a photo with the water pump motor and solar panel?? my group is having a project based on it and it would greatly help alot if we could see it!

emirbyildiz (author)PekY12016-10-21

Dear PekY1, it is my pleasure. I will upload photos today.

PekY1 (author)emirbyildiz2016-10-21

Thank you very much!!!

Jorge DominicG (author)2016-07-31

what is the range of this soil sensor

CarlG50 (author)Jorge DominicG2016-09-20

0 to 1000 idealy but i had 250 - 1023 (1000+ means disconnected)

CarlG50 made it! (author)2016-09-20

Ok so i've been doing a fairly similary projet and if you read your
sensor each second then the sensor will rust leading to unprecis
reading's and short life span of the sensor (max 1-2 week).

I was using at the time the sensor YL-69 which comes with the relay YL-38

Gideon Rossouwv (author)2016-08-30

Hi, thanks for this. I needed to know how to use the sensor. Thanks to you I do now!

Steven_Smarty (author)2016-08-21

Can I make this project with Arduino Uno?

If yes please share circuit diagram

rekcah2222 made it! (author)2016-06-25

Nice project. I just made it :-) Thaks you

MattW37 (author)2016-06-12

Hi, mostly new to this but I think some errors posted in the code when you copied, unless its just the uno that has problem but it appears that html paragraph code was placed throughout which causes problems in the compiler (<p>)

mojoj1 (author)2016-05-24

hello, what is the function of those LED? why is there too many? and if the plant is lack of water, what signal will the arduino gives and what if there is too much water or the water is enough already? please help me, im new in this arduino.

An account (author)2016-04-22

I'm using a moisture sensor for a project. Thanks for the awesome tutorial!

d_jack (author)2016-04-13

Hi all, great setup you have here. Firstly I am new to a lot of this and have forgotten a lot of what I used to know so hopeing someone can help with a problem I have. I have built a watering system using this .... http://www.ebay.com/itm/391289602060?_trksid=p2055119.m1438.l2649&ssPageName=STRK%3AMEBIDX%3AIT I have kept it basic just using this board to switch solenoids directly. I have also used one of these boards as a floatless switch to turn on a drain pump in a sump in case of flooding however as soon as the level drops past the probe it starts a nasty echo pulsing the pump on and off till the level drops clear enough not to retrigger the sensor. What I want to do as add an electrolytic to the output transister on the board that switches the relay to give me 2-5 seconds of run on to let the water clear the probe. Could someone please suggest a simple way to do this and component values? Thankyou in advance! Dave

hunsnowboarder (author)2016-04-13

Nice instructable. Maybe switch case would be more practical instead of if/elseif.

flpsoares made it! (author)2016-03-26

Very helpful, thanks!

ngreen17 made it! (author)2016-03-04

slightly modified but still i made it

joyang (author)2016-02-26

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?

swapnilm16 (author)2016-02-22

can u help me to create code for turning dc water pump on and off using soil moisture sensor and arduino nano

About This Instructable

251,754views

227favorites

License:

Bio: 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 ... More »
More by innovativetom:WiFi Bathroom Humidity Sensor W/Fan Control, App & AutomationTable Top Tiki Torch! Recycle Your Beat Up Tiki Torch.How to Mount a Poster or Print to Plywood Panel
Add instructable to: