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.


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
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. 
  // 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

Step 3: LEDs

Please use resistors when connecting the LEDs to your Arduino!


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

<p>awesome, worked like a charm.</p><p>here's the photos, didn't use a resistor, but did use a arduino uno.</p><p>https://github.com/johnantoni/arduino/tree/master/moisture-5-leds</p><p>her</p>
<p>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.</p>
<p>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 &quot;virtual&quot; LCD display and leds! PS, I'm a NOOB at this.. code here:</p><p>#include &lt;SoftwareSerial.h&gt;</p><p>SoftwareSerial DebugSerial(2, 3); // RX, TX</p><p>#define BLYNK_PRINT DebugSerial</p><p>#include &lt;BlynkSimpleStream.h&gt;</p><p>char auth[] = &quot;YOUR AUTH CODE FOR BLYNK GOES HERE!&quot;;</p><p>WidgetLCD lcd (V0);</p><p>WidgetLED led1(V1);</p><p>WidgetLED led2(V2);</p><p>WidgetLED led3(V3);</p><p>WidgetLED led4(V4);</p><p>WidgetLED led5(V5);</p><p>int a;</p><p>int humiditya;</p><p>void setup()</p><p>{</p><p> // Debug console</p><p> DebugSerial.begin(9600);</p><p> // Blynk will work through Serial</p><p> // Do not read or write this serial manually in your sketch</p><p> Serial.begin(9600);</p><p> Blynk.begin(Serial, auth);</p><p>}</p><p>void loop(){</p><p> Blynk.run();</p><p> a = analogRead (A0);</p><p> humiditya = map (a, 0, 1023, 0, 100);</p><p> lcd.print(0,0, a);</p><p> lcd.print(0,1, humiditya);</p><p>if (a &gt;= 820)</p><p> {</p><p> led1.on();</p><p> led2.on();</p><p> led3.on();</p><p> led4.on();</p><p> led5.on();</p><p>}</p><p>else if (a &gt;= 615 &amp;&amp; a &lt; 820)</p><p> {</p><p> led1.on();</p><p> led2.on();</p><p> led3.on();</p><p> led4.off();</p><p> led5.off();</p><p>} </p><p>else if (a &gt;= 410 &amp;&amp; a &lt; 615)</p><p> {</p><p> led1.on();</p><p> led2.on();</p><p> led3.off();</p><p> led4.off();</p><p> led5.off();</p><p>} </p><p>else if (a &gt;= 250 &amp;&amp; a &lt; 410)</p><p> {</p><p> led1.on();</p><p> led2.off();</p><p> led3.off();</p><p> led4.off();</p><p> led5.off();</p><p>}</p><p>else if (a &gt;= 0 &amp;&amp; a &lt; 250)</p><p> {</p><p> led1.off();</p><p> led2.off();</p><p> led3.off();</p><p> led4.off();</p><p> led5.off();</p><p>}</p><p> delay(2000);</p><p> lcd.clear();</p><p>}</p>
<p>Nice job but it does need a few simple refinements to make it practical...</p><p>Add 1K resistors to the LEDs and correct the code so that the red LED will light.</p><p>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.</p><p>Here's my modified code with probe polling:</p><p>/* </p><p>Innovativetom.com<br>stevebrace.co.uk</p><p>Flower Pot Soil Mosture Sensor</p><p>A0 - Soil Mosture Sensor</p><p>D2:D6 - LEDS 1,2,3,4,5</p><p>LED1 - Green</p><p>LED2 - Green</p><p>LED3 - Green</p><p>LED4 - YELLOW</p><p>LED5 - RED</p><p>Connect the Soil Mosture Sensor to anolog input pin 0, </p><p>and your 5 led to digital out 2-6</p><p>*/</p><p>int led1 = 2;</p><p>int led2 = 3;</p><p>int led3 = 4;</p><p>int led4 = 5;</p><p>int led5 = 6;</p><p>int probe = 7;</p><p>int mostureSensor = 0;</p><p>void setup() {</p><p> // Serial Begin so we can see the data from the mosture sensor in our serial input window. </p><p> Serial.begin(9600);</p><p> // setting the led pins to outputs</p><p> pinMode(led1, OUTPUT);</p><p> pinMode(led2, OUTPUT);</p><p> pinMode(led3, OUTPUT);</p><p> pinMode(led4, OUTPUT);</p><p> pinMode(led5, OUTPUT);</p><p> pinMode(probe, OUTPUT);</p><p>}</p><p>// the loop routine runs over and over again forever:</p><p>void loop() {</p><p> // Power-up the probe and pause for the driver</p><p> digitalWrite(probe, HIGH);</p><p> delay(1000);</p><p> // read the input on analog pin 0:</p><p> int sensorValue = analogRead(mostureSensor);</p><p> // print out the value you read:</p><p> Serial.println(sensorValue);</p><p> if (sensorValue &gt;= 820)</p><p> {</p><p> digitalWrite(led1, HIGH);</p><p> digitalWrite(led2, HIGH);</p><p> digitalWrite(led3, HIGH);</p><p> digitalWrite(led4, HIGH);</p><p> digitalWrite(led5, HIGH);</p><p>}</p><p>else if (sensorValue &gt;= 615 &amp;&amp; sensorValue &lt; 820)</p><p> {</p><p> digitalWrite(led1, HIGH);</p><p> digitalWrite(led2, HIGH);</p><p> digitalWrite(led3, HIGH);</p><p> digitalWrite(led4, HIGH);</p><p> digitalWrite(led5, LOW);</p><p>} </p><p>else if (sensorValue &gt;= 410 &amp;&amp; sensorValue &lt; 615)</p><p> {</p><p> digitalWrite(led1, HIGH);</p><p> digitalWrite(led2, HIGH);</p><p> digitalWrite(led3, HIGH);</p><p> digitalWrite(led4, LOW);</p><p> digitalWrite(led5, LOW);</p><p>} </p><p>else if (sensorValue &gt;= 250 &amp;&amp; sensorValue &lt; 410)</p><p> {</p><p> digitalWrite(led1, HIGH);</p><p> digitalWrite(led2, HIGH);</p><p> digitalWrite(led3, LOW);</p><p> digitalWrite(led4, LOW);</p><p> digitalWrite(led5, LOW);</p><p>}</p><p>else if (sensorValue &gt;= 0 &amp;&amp; sensorValue &lt; 250)</p><p> {</p><p> digitalWrite(led1, HIGH);</p><p> digitalWrite(led2, LOW);</p><p> digitalWrite(led3, LOW);</p><p> digitalWrite(led4, LOW);</p><p> digitalWrite(led5, LOW);</p><p>}</p><p> // Power-down the probe</p><p> digitalWrite(probe, LOW);</p><p> delay(1800000); // wait half an hour</p><p>}</p>
Can u give me the code if I want to place relay instead of led.plz. // ........ Is that the part of coding<br>
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.
Thanks<br>//power the probe etc<br>Is that the part of code,which transistor should I use
<p>if you use a relay module like this then it has the transistor and diode already</p><p><a href="http://img.dxcdn.com/productimages/sku_319093_1.jpg" rel="nofollow" title="">http://img.dxcdn.com/productimages/sku_319093_1.jp...</a></p><p>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.</p>
<p>Yep, and &quot;active low&quot; relays are way more common than &quot;active high&quot; ones.</p>
<p>It's abselutt critical to turn off the moisture sensor.</p><p>If you only have 1 or 2 sensor you can get away with using a digital pin as power.</p><p>I have several and ended up using a MOSFET.</p><p>Im new to both coding and arduino, but for anyone intereseted her is my setup</p><p>https://github.com/oleost/WaterSystemArduino</p>
<p>You could save a bunch of code by using a switch() statement with &quot;fall-through&quot; to set your LEDs... I haven't tested this code, so it might need debugging, but here's what I'd do:</p><p>// Reset all LEDs<br>digitalWrite(led1, LOW);<br>digitalWrite(led2, LOW);<br>digitalWrite(led3, LOW);<br>digitalWrite(led4, LOW);<br>digitalWrite(led5, LOW);</p><p>switch(sensorValue) {<br> case &gt;= 820:<br> digitalWrite(led5, HIGH);<br> // normally we'd put &quot;break;&quot; here to exit the select<br> case &gt;= 615:</p><p> digitalWrite(led4, HIGH);<br> case &gt;= 410:<br> digitalWrite(led3, HIGH);<br> case &gt;= 250:<br> digitalWrite(led2, HIGH);<br> case &gt;= 0:<br> digitalWrite(led1, HIGH);<br>}</p><p>By excluding the break; after each case statement, the code &quot;falls through&quot; 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 &quot;case &gt;= 250&quot;, so LED2 comes on. The code falls through from there &amp; also turns led1 on. Since all LEDs were turned off at the start, leds3, 4 and 5 remain off.</p><p>HTH!</p>
<p>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</p><p>setup </p><p>long previousMillis = 0; </p><p>long interval = 900000; // 15 minutes</p><p>main loop </p><p>{</p><p>unsignedlong currentMillis = millis();</p><p>if(currentMillis - previousMillis &gt; interval)</p><p> {</p><p>previousMillis = currentMillis;</p><p>}</p><p>else</p><p>{</p><p>get value from sensor once every 15 minutes </p><p>}</p><p>}</p>
<p>//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 </p><p>//By Dave Soderbloom<br>//Quick sketch for water sensor reading without delay to tie up processor for other tasks like interrupts <br>//pins I used on msp432<br><br> int led1 = 2;<br><br> int led2 = 3;<br><br> int led3 = 4;<br><br> int led4 = 5;<br><br> int led5 = 6;<br><br> int probe = 8;<br><br> int moistureSensor = A7; <br> <br> <br> <br> int sensorValue = 0;<br><br> long previousMillis = 0;<br><br> long interval = 10000; // 15 minutes900000<br> <br> <br> <br>void setup()<br>{<br><br> <br> Serial.begin(9600);<br><br> // setting the led pins to outputs<br><br> pinMode(led1, OUTPUT);<br><br> pinMode(led2, OUTPUT);<br><br> pinMode(led3, OUTPUT);<br><br> pinMode(led4, OUTPUT);<br><br> pinMode(led5, OUTPUT);<br><br> pinMode(probe, OUTPUT);<br> <br> //get and display startup reading before main loop to the serial monitor<br> digitalWrite(probe, HIGH);<br> delay(1000);<br> int sensorValue = analogRead(moistureSensor);<br> Serial.println(sensorValue);<br> digitalWrite(probe, LOW);<br><br> <br> <br> <br>}<br><br>void loop()<br>{<br> <br> <br> unsigned long currentMillis = millis();<br> <br> int sensorValue = analogRead(moistureSensor);<br> <br> if(currentMillis - previousMillis &gt; interval)<br> {<br> previousMillis = currentMillis;<br><br> <br> digitalWrite(probe, HIGH);<br> 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<br> <br> <br> <br> int sensorValue = analogRead(moistureSensor);<br> <br> <br> // print out the value you read:<br> Serial.println(sensorValue);<br><br>//may have to adjust ranges depending on a few factors <br> if (sensorValue &gt;= 820)<br><br> {<br><br> digitalWrite(led1, HIGH);<br><br> digitalWrite(led2, HIGH);<br><br> digitalWrite(led3, HIGH);<br><br> digitalWrite(led4, HIGH);<br><br> digitalWrite(led5, HIGH);<br><br> }<br><br> else if (sensorValue &gt;= 615 &amp;&amp; sensorValue &lt; 820)<br><br> {<br><br> digitalWrite(led1, HIGH);<br><br> digitalWrite(led2, HIGH);<br><br> digitalWrite(led3, HIGH);<br><br> digitalWrite(led4, HIGH);<br><br> digitalWrite(led5, LOW);<br><br> }<br><br> else if (sensorValue &gt;= 410 &amp;&amp; sensorValue &lt; 615)<br><br> {<br><br> digitalWrite(led1, HIGH);<br><br> digitalWrite(led2, HIGH);<br><br> digitalWrite(led3, HIGH);<br><br> digitalWrite(led4, LOW);<br><br> digitalWrite(led5, LOW);<br><br> }<br><br> else if (sensorValue &gt;= 250 &amp;&amp; sensorValue &lt; 410)<br><br> {<br><br> digitalWrite(led1, HIGH);<br><br> digitalWrite(led2, HIGH);<br><br> digitalWrite(led3, LOW);<br><br> digitalWrite(led4, LOW);<br> <br> digitalWrite(led5, LOW);<br><br> }<br><br> else if (sensorValue &gt;= 0 &amp;&amp; sensorValue &lt; 250)<br><br> {<br><br> digitalWrite(led1, HIGH);<br><br> digitalWrite(led2, LOW);<br><br> digitalWrite(led3, LOW);<br><br> digitalWrite(led4, LOW);<br><br> digitalWrite(led5, LOW);<br><br> }<br><br> // Power-down the probe<br> <br> digitalWrite(probe, LOW);<br> }<br>}</p>
<p>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!</p>
<p>Where would you get those specific resistors from?</p>
Can anyone make water sensor without using transistor??
<p>Can you tell me about the unit of sensorValue? Do you know about the ISO unit of soil moisture?</p>
<p>Hello Tom</p><p>The AC and DC you refer to are in fact AO and DO (Analogue Output and Digital Output).</p>
<p>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. </p>
<p>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 =(</p>
<p>Hi !! My sensor is showing 870 value when not in the water :s Someone knows why plz? :s</p>
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.
<p>Nice project. I will add a dc water pump motor, 6x18650 battery and a 12V solar panel to make it permanent. Thank you!</p>
<p>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!</p>
Dear PekY1, it is my pleasure. I will upload photos today.
<p>Thank you very much!!!</p>
<p>what is the range of this soil sensor</p>
<p>0 to 1000 idealy but i had 250 - 1023 (1000+ means disconnected)</p>
<p>Ok so i've been doing a fairly similary projet and if you read your <br>sensor each second then the sensor will rust leading to unprecis <br>reading's and short life span of the sensor (max 1-2 week).</p><p>I was using at the time the sensor YL-69 which comes with the relay YL-38</p>
<p>Hi, thanks for this. I needed to know how to use the sensor. Thanks to you I do now!</p>
<p>Can I make this project with Arduino Uno?</p><p>If yes please share circuit diagram</p>
<p>Nice project. I just made it :-) Thaks you</p>
<p>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 (&lt;p&gt;)</p>
<p>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.</p>
<p>I'm using a moisture sensor for a project. Thanks for the awesome tutorial!</p>
<p>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&amp;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</p>
<p>Nice instructable. Maybe switch case would be more practical instead of if/elseif.</p>
Very helpful, thanks!
slightly modified but still i made it
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?
<p>can u help me to create code for turning dc water pump on and off using soil moisture sensor and arduino nano</p>
<p>You can save few lines using bitWrite instead digitalWrite :)</p>
<p>Hai can any one help me for. I need to swit 3 sensors whit 3 solenoids. i am messuring diverend spots in a greenhouse and i need to messure diverend spots.</p><p>when a spot is dry it must water whit a pump ( trou relay) when wet it has to stop.</p><p>I will need to ajust the sensor for opening ( diverend plant need diverend moister)</p><p>hard ware no problem program big problem.</p><p>This program is verry nice but how can i transform it to my needs??</p><p>pleaaasssseeee help...</p>
Hi everybody, i am new here and would like to start a diy watering plant project. Its solely for my hobby! <br>My plan goes like this: because i am using a normal moisture sensor (the squarish one), i will put that moisture sensor into the soil and try to detect whether water is present. If theres water present, the motor pump would not start. If there isnt, the motor pump would start to pump water out of the tank. I have been fidgeting with codes, but i cant seem to get the motor to work.. can anybody help me..?
<p>can we use uno board instead</p>
<p>Yes u can</p>
<p>Hey guys, </p><p>Here are a lot of pics if someone (noob like me) want to see the connections :) </p><p>I must thank Tom for the initial post and to Steve for the second version, <br>In the photos I used the second version but the first one works just fine, <br><br>I'm working now on a version where I can use an ATTINY85 chip to get rid of the Arduino so my mom plant can be more happier and I can keep my Arduino, hehe<br><br>I want to use a I2C display as well, if any of you guys can help me with a piece of code to make my life easier it'll be much appreciated! </p><p>Thanks! </p>
<p>i tried the same , but all the led glowing same time ,no need of the sensor</p><p>no change when i put in water or out of the water.</p><p>any suggetion?</p>
<p>hey there, well, if you made all the connections correct it should work, I am thinking that maybe you messed something in rush, if you look at the pics you should see cleary how to hook everything up, you can do a test with just one LED and see if it works, put the sensor in the water and the led shoul turn off, I strongly suggest to check your connections and pin assignments in the source code, if you're still outa' luck send me a private message and I'll do my best to help ya</p>

About This Instructable




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:Recycled Hardware & Parts Bin Organizer!  Control Stuff From the Internet, Create a WIFI Switch.  Wemos ESP8266 Getting Started Guide With Arduino IDE 
Add instructable to: