ESP8266 Soil Moisture Sensor With Arduino IDE




Introduction: ESP8266 Soil Moisture Sensor With Arduino IDE

About: Will write code for food. :) If you need help with any of your embedded Arduino applications please send me a message.

The ESP8266 is a great piece of electronics. If you like Arduino, you'll also like the ESP8266. They are small, only require 3.3 volts and best of all they have full WiFi capabilities. The ESP8266 is perfect if you want to web enable just about any device. That's right. IoT. It's all the rage.

With new IoT and ESP8266 development platforms emerging rapidly, where do I get started? Already familiar with Arduino devices I decided to use the Arduino IDE to program my ESP8266. Being my first ESP8266 project I wanted to create a simple device that would perform a real world task. So, I decided to build a web enabled moisture sensor to let me know when my house plants need water. My house plants will be part of the Internet of Things!

The ESP8266 is used to read the analog signal produced by a soil moisture sensor probe. The data is then used to determine the color of a blinking RGB LED. If the sensor detects low moisture the LED is red, medium is green and high moisture is blue. The data can also be accessed via a web browser where the data is displayed as a web page with a Google Chart.

The following video demonstrates the finished project and explains the source code for the Arduino IDE sketch. The source code for the sketch can be downloaded here:

Step 1: What You Need


  • If you don't already have the Arduino IDE you will need to install this before you can begin programming your ESP8266 board. You can download the IDE from the Aduino website here


  1. First you will need an ESP8266. I'm using an ESP8266-12E. These can be purchased online for less than $10 each.
  2. Second a moisture sensor module with a probe. I purchased mine online for under $10.
  3. A RGB LED with common cathode. These are cheap, 50 for about $5 online.
  4. A breadboard and some jumper wire to setup and test the connections.

If you plan on creating the finished circuit shield you will also need the following:

  1. A small printed prototype board. This is the base board used for soldering connections. Less than $5 online for a pack of 10.
  2. Also some female 16 pin headers. I'm using part number EK1417. A bag of 50 for about $15. You only need two.
  3. Soldering equipment to solder the circuit connections.

Step 2: Arduino IDE Setup

Now let's connect the ESP8266 to a computer via the USB port and open the Arduino IDE.

Since we aren't using an Arduino UNO, MEGA or other hybrid, we need to make some changes to the IDE before it will communicate with the ESP8266 board.

This video will get you connected!

Step 3: ESP8266-12E Breadboard Setup

Before attempting to solder the circuit connections we are going to test them out with a breadboard.

Depending on the size of your ESP8266 you may need a clever breadboard configuration (see photo above).

Follow the photo progression above for illustrations of how to wire up the breadboard connections.

RGB LED Setup:

  • Pin ~D2 connects to the RED lead
  • Pin ~D5 connects to the BLUE lead
  • Pin ~D6 connects to the GREEN lead
  • GND connects to the GND lead

Moisture Sensor Module Setup:

  • Pin A0 of the moisture sensor module connects to pin A0 on the ESP8266
  • The GND pin on the moisture sensor module connects to a GND pin on the ESP8266
  • The VCC pin on the moisture sensor module connects to a 3v3 pin on the ESP8266

(Note: we are NOT using the D0 pin on the moisture sensor. This is a digital "On or Off" output. The sensitivity of the digital output is controlled by the potentiometer on the moisture sensor module. Turning the screw on the potentiometer will change how much moisture is required to turn off/on the output.)

(Note: typically resistors would be used on the RGB leads of the LED. However, the ESP8266 only provides 3.3 volts, so there is little chance of burning out the LED. In this situation resistors are not needed.)

Step 4: Soldering the Circuit

Now that we've tested our circuit with a breadboard and confirmed that everything is working we are ready to put it together on a prototype board. The photos above show the brown colored prototype board I used. I highly recommend using some 16 pin headers as pictured. These little socket strips allow you to plug your ESP8266 onto the circuit. There's several advantages to using these headers. First, if you decide you want to use your ESP8266 for another project, it won't be hardwired to a circuit. You just unplug it from the headers and you're on to your next project. Also, if you mess up the soldering job (oh yes, it happens), you won't destroy the ESP8266 module. If you have never soldered a circuit before, this will be good practice.

Now connect the ESP8266 to the header sockets and position it on the prototype board. I spaced mine to one side and left a blank row of open holes. On the other side I left enough space to easily plug in the RGB LED. Then I cut the board to size using a straight edge and razor knife. At this point I flipped the board over and flatted the pins against the bottom of the board. This helps hold the headers in place for soldering. I then soldered some of the header pins to the board - making sure NOT to solder any of the pins I intended to use.

Now I decided to solder the moisture sensor module to the prototype board, in between the two headers. This way, the moisture module sits nice and tidy underneath the ESP8266. I cut some jumper wire to length and soldered the pins of the moisture module to the appropriate pins of the header using the jumper wire to make the connections. Check the previous breadboard setup step for the wiring diagram (one of the photos).

Now do the same thing with the RGB LED. Poke the leads of the LED through the prototype board and bend them flat against the board to secure them temporarily. Now, where you can, bend the LED leads to meet the appropriate pins on the header. If you cannot make the connection without touching and shorting another connection, use jumper wire. Solder the LED connection points, snip off any excess wire protrusions and viola, you done. You are now ready to plug it in and test it out.

If it doesn't work, chances are something went wrong during the soldering. Either the wrong pins were connected or excess solder bled over to another connection. Either way, you can start over with a fresh prototype board, headers and LED while preserving the more costly ESP8266 board.

If everything works, there's one last thing. This is not required but it will certainly help keep your circuit in solid working condition. I like to hot glue the bottom. This will seal the solder connections and wiring, offering some protection against accidental shorts. I do this by first putting a strip of masking tape sticky side up on my work bench. The same kind of tape used by painters. Then, hot glue the bottom of the circuit generously and squish it onto the tape. After the glue cools, peel it off the tape, cut away any excess glue and you have a nice flat surface that seals and insulates the circuit wiring!

2 People Made This Project!


  • Tiny Home Contest

    Tiny Home Contest
  • Fix It! Contest

    Fix It! Contest
  • Water Contest

    Water Contest

22 Discussions

am unable to reach the webpage through the ip address provided by the controller or even by static ip address
can anyone plssssss help

are there any pre-requisites for performing this exercise...

1 more answer

Your device may not be connecting to your wireless network. You will need to make sure you have the subnet mask and default gateway set correctly, as well as the SSID and password. By default, the code is configured for the OP's network. The default gateway for my router was different, so I had to change that, and I wanted a specific IP for each board, so I had to specify static IPs in the code for each individual board. I'm currently in the process of building a solar garden light with the ESP8266 and moisture sensor built in. The board also controls the RGB that is built into this garden light, which happens to be common anode, so I had to change that code as well. You will have to monitor serial output from the board for indication that the board is successfully connecting to your router.

Hey, I've built this sensor, but I can't figure out how to "calibrate" it. When I put the moisturesensor in a glass of water, it only show ~70% on the gauge.

2 replies

I ran into that problem too. I think it may be that we are using a different sensor. Anyway I was able to make it work but I had to completely throw out the math in the original sketch and replaced it with the following:
//analogValue = analogRead(A0); // read the analog signal

output_value = analogRead(A0);

output_value = map(output_value,1024,510,0,100);

if (output_value > 100)


output_value = 100;


int chartValue = output_value;

In this code, I'm simply dividing output up into 100 pieces, the range being 1024 (Dry sensor) to 510, which is my sensor, submerged in a container of my local tap water. Lastly, I'm cheating to make sure the output is never above 100%, should it sometimes read higher that 510 by a little bit.

Use this instead. It's more intuitive and calibration is way faster.

As a test: Add some salt into the water or simply connect a short wire between the sensor legs. This should provoke a full scale reading. Otherwise you must adjust the SW settings.

You should still have a resistor on the LED to protect the pins on the ESP8266. LED Resistors are for current limiting, not voltage dropping per se


5 months ago

Awesome, just what I need for my first step... It works! Thank you~

WhatsApp Video 2018-03-05 at 4.25.01 PM.mp4_snapshot_00.06.jpg

Laat maar want wat is dit nu weer voor onzin in die comments posting. De helft is gewoon weg en het staat er nu twee keer onvolledig.

Why only the red LED works on NodeMCU 12E?
Whatever the values, all above 25, there will no LED turn on! The red LED (if (chartValue I even thought that the NodeMCU was broken so took a new one but with the same result. Changed even the breadboard a few times and the power-supply. I really start to wonder what is going one here.

Why only the red LED works on NodeMCU 12E?
Whatever the values, all above 25, there will no LED turn on! The red LED (if (chartValue I even thought that the NodeMCU was broken so took a new one but with the same result. Changed even the breadboard a few times and the power-supply. I really start to wonder what is going one here.

am unable to reach the webpage through the ip address provided by the controller or even by static ip address
can anyone plssssss help

are there any pre-requisites for performing this exercise...

It also happened to me on my private IoT WLAN without internet (security).
The gauge reappear with contact to the net.

My first instructable - and it made me :-D

Had to recalibrate the sensitivity and sensor offset so that it would fit the scale - no problem !

I'm no Google Gauge expert. In fact, until this Instructable, I didn't know they existed!

You mentioned the disparity between the colors on the gauge and those of the LED. Might this allow you to bring them in line by changing the html code for the gauge colors (I don't know much about html, either!)


Hello, small question. How do you know which pins have PWM capabilities? Do they all? Additionally, is A0 the only "analog in" pin? I assume that the pin marked 'ADC' on the bare bones ESP-12 is the A0 pin in question. Thanks!

All is done. web page is getting open with chart. but chart value always displaying 99.Also RGB led is not blinking.

1 reply

Check your wiring. Are you using a DHT22 or DHT11 ?

Well done. Just one remark. The "A0" of the sensor module is connected directly to the probe itself. Therefore if you are not using the digital output you do not need the entire module, only the probe and probably a resistor

1 reply