IoT Soil Moisture Sensor using ESP8266 & Blynk with Water Pump Control
IoT based Plant watering System using Soil Moisture Sensor
Table of Contents
IoT Soil Moisture Sensor:
IoT Soil Moisture Sensor using ESP8266 & Blynk- In this article, you will learn how to convert an ordinary Soil moisture sensor into an IoT Soil Sensor or IoT Moisture Sensor using Nodemcu ESP8266 WiFi module, SSD1306 I2C supported Oled display, a relay module for controlling the water pump, and a smartphone with Blynk application for monitoring the soil moisture and for controlling the water pump.
You don’t have to be worried if you do not have this particular type of Soil Moisture Sensor. You can use other types of Soil Moisture sensors as well. Anyway, personally, I like this type of soil moisture sensor because it can dig deeper into the soil; the sensor probes are waterproofed and are made of highly corrosion-resistant material to ensure long lifetime up to 6 months after being plugged into the soil. Previously I used it with the Arduino and Oled display module. And I practically demonstrated how to make a low-cost yet highly efficient Soil moisture meter. So, if you are just getting started with the Soil moisture sensor and want to learn the extreme basics then I highly recommend you should read my article on Arduino and Soil Moisture Sensor.
Here is my prototype model. As usual, I am using my designed Nodemcu ESP8266 development board along which a Soil Moisture Sensor, Oled display module, a 12V Relay module, and 220Vac light bulb are connected.
I am using a bulb instead of a water pump so that at the time of carrying out a practical demonstration, you can clearly see for yourself how do I turn this bulb ON or OFF. You can also use a 220 Vac water pump and you can also use a DC water pump; it all depends on your choice. If you have to control a large water pump, then you better use a power relay. I have written a separate article on IoT Power Relay, in which I have practically demonstrated how to control a large water pump. Let’s kick off our practical demonstration and afterward I will explain everything else.
You can also ready my article on IoT based Water level monitoring and automatic water pump control system using the ESP32 WiFi + Bluetooth Module, Waterproof Ultrasonic Sensor, and the New Blynk V2.0 more…
My IoT Soil moisture monitoring Application and Nodemcu ESP8266 WiFi module are connected to the internet.
You can view the same soil moisture value on the Oled display module and Blynk application. The SV or Set value is basically the soil moisture threshold value that can be adjusted using this slider on the Blynk application.
Since different plants need a different percentage of soil moisture so using this button on the Blynk application you can turn ON and turn OFF the Water Pump at any time from any part of the world. Previously, I made it fully automatic but this time; I decided to give full control to the user.
This IoT soil moisture monitoring system also sends a notification when the moisture drops below the set value or threshold value.
As you can see (watch video given at the end of this article), as I move the slider the SV value on the Oled display module also changes. If the soil moisture is less than the set value then the buzzer is also turned ON. And at the same time a notification message is also sent to the Blynk application. This buzzer will only turn OFF when the soil moisture will increase above the set value. So, after selecting the threshold value. Next, I can go ahead and start watering the plant.
Now, if I press the button, the light will turn ON, which will indicate that the motor has turned ON.
After adding the water, Soil moisture increased above the SV value, I stopped receiving the notification messages and also the buzzer turned OFF. Finally, I pressed the button on the Blynk application to turn OFF the Water Pump( presented by a Blub). It is so simple, isn’t it? All we need is to read the soil moisture value on the Blynk application and then accordingly control the water pump on the remote side.
I am sure by now, you might have got an idea of how does this system work. So, without any further delay let’s get started!!!
Note: this old version of the Blynk app is no more functional. For the blynk mobile App setup and Blynk.cloud dashboard setup ready my article on the New Blynk V2.0. In this article I have explained how to migrate your projects from Blynk 1.0 to the new Blynk V2.0. You can also watch the video.
Amazon Links:
ESP32 WiFi + Bluetooth Module (Recommended)
Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Soil Moisture Sensor:
Here is my Soil moisture detection kit. Let’s first take a look at the interface circuit. The board is provided with this blue color potentiometer which is used to adjust the sensitivity. This board is also provided with the most stable LM393 comparator chip and with On-board power and signal indicator LEDs to display the module working status. The working voltage of this module is from 3.3V to 12 volts. Because of this wide input voltage range, this Soil moisture sensor can be used with 3.3V and 5V compatible controller boards.
All the 4 headers are clearly labeled. The first pin is the Analog output pin and it should be connected with the Analog pin on the Arduino. The 2nd pin is the digital output put pin which can be connected with any digital pin on the Arduino or any other controller board. 3rd pin is the ground pin and the last pin is the VCC pin which you can connect with 3.3V or 5V.
The sensor probes are waterproofed and are made of highly corrosion-resistant material to ensure long lifetime up to 6 months after being plugged into the soil. The Sensor Cable length is 1.3 meters.
Specifications:
Working Voltage: DC 3.3-12V
Working Current: <20m(Output Current:<30mA)
Module Size: 36X15X6mm
Sensor Pin Pitch:5mm
Sensor Cable Length:1.3meter/3.94ft
Sensor Connecting Port: XH2.54 2P
Nodemcu ESP8266:
ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.
ESP8266 has powerful on-board processing and storage capabilities that allow it to be integrated with the sensors and other application specific devices through its GPIOs with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, and the entire solution, including front-end module, is designed to occupy minimal PCB area.
Specifications:
- Serial/UART baud rate: 115200 bps
- Input power: 3.3V
- I/O voltage tolerance: 3.6V Max
- Flash Memory Size: 1MB (8Mbit)
- WiFi security modes: WPA, WPA2
IoT Soil Moisture Sensor Circuit Diagram:
The VCC and GND pins of the Soil Moisture Sensor and Oled display module are connected with 3.3V and GND pins of the Nodemcu ESP8266 WiFi module. The analog output pin of the Soil moisture sensor interface circuit is connected with the A0 pin of the Nodemcu module. The SCK or SCL and SDA pins of the Oled display module are connected with the D1 and D2 pins. D1 is the SCL and D2 is the SDA.
Digital pin D0 on the Nodemcu module is used to control the relay module which is used to control the Water pump. And digital pin D4 is used to control the 5V buzzer. The Red wire of the buzzer is connected with the 5 volts while the GND wire of the buzzer is connected with the collector of the 2n2222 NPN transistor. The emitter of the transistor is connected with the GND while the base is connected with the pin D4 through a 10K ohm resistor. The 2n2222 NPN transistor and 10k ohm resistor make the driver circuit.
On the left side is the 5V regulated power supply which is based on the LM7805 linear voltage regulator. The 7805 regulator accepts a wide range of input voltages between 7 and 28 voltages. As a precaution do not use voltages above 25 volts. So, it doesn’t matter if you power up your project using a 9v or 12v battery, or if you plan to use a solar panel, or 12 to 25 volts DC adaptor. The 7805 regulator will step down this voltage and outputs regulated 5 volts.
You can see the output of the 7805 voltage regulator is connected with the Vin pin of the Nodemcu ESP8266 WiFi module. And don’t forget to add these 470uF decoupling capacitors with the input and output legs of the voltage regulator. So, that’s all about the circuit diagram. Now, let’s work on the Blynk application follow the same exact steps.
IoT Soil moisture monitoring Blynk App:
First open the Blynk application and click on the New Project.
Enter the project name in my case “iot soil moisture”. Next, click on the CHOOSE DEVICE and select ESP8266. Finally, click on the Create button.
The authorization code will be sent on your registered email id.
Once the App is opened, click anywhere on the screen, search for the gauge in the widgets list and add it to the screen.
After adding the gauge set its perimeter give a name to the gauge as “Soil moisture” and set the pin to V1 and set the limit value from 0 to 100.
Next, add a slider in the same as you added the Gauge.
Now, click on the slider and set its properties; enter the name, set pin to V2 and set the limit from 0 to 100.
Now, again click on the screen and this time add a button. We will use this button to control a relay. Set the pin to digital GP0.
Again click on the blank area and this time select the notification widget from the list and add it to the screen.
The Blynk application is ready. If you click on the play button it will look something like this.
You can see that red error sign, it means right now it’s not connected with the Nodemcu ESP8266, because right now my Nodemcu module is OFF.
Required Arduino Libraries:
Before, you start the programming, first of all, make sure you download all the necessary libraries.
The Adafruit_GFX.h and Adafruit_SSD1306.h libraries I have been using for quite a long time and I have already explained how to download and use these libraries.
I have also a very detailed getting started tutorial on the ESP8266 WiFi module, in which I have explained how to install the ESP8266 using the Boards manager URL link. And how to use Blynk application. You will also learn how to download and install BlynkSimpleEsp8266.h.
IoT Soil Moisture Sensor Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <BlynkSimpleEsp8266.h> #define BLYNK_PRINT Serial char auth[] = "R8SYsf1UqRby1dH0KR6Ev9ctB0oEeLui"; /* WiFi credentials */ char ssid[] = "AndroidAP3DEC"; char pass[] = "electroniclinic"; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); int msensor = A0; // moisture sensor is connected with the analog pin A0 of ESP8266 int msvalue = 0; // moisture sensor value int setmoisture; int mspercent; // moisture value in percentage int Relay = D0; // you can connect a dc or ac water pump int buzzer = D4; void setup() { Serial.begin(115200); Blynk.begin(auth, ssid, pass); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); delay(2000); display.clearDisplay(); display.setTextColor(WHITE); // initialize the sensor pinMode(msensor, INPUT); pinMode(Relay, OUTPUT); pinMode(buzzer, OUTPUT); } void loop() { Blynk.run(); msvalue = analogRead(msensor); mspercent = map(msvalue,0,1023,100,0); // To display the soil moisture value in percentage display.clearDisplay(); display.setCursor(10,0); display.setTextSize(2); display.setTextColor(WHITE); display.print("SM:"+String(mspercent)+"%"); display.setCursor(10,30); display.setTextSize(2); display.print("SV:"+String(setmoisture)); display.display(); Blynk.virtualWrite(V1,mspercent); if (mspercent<setmoisture ) { Blynk.notify("Soil moisture decreased!!!"); digitalWrite(buzzer,HIGH); } else { digitalWrite(buzzer,LOW); } delay(1000); } BLYNK_WRITE(V2) // To set soil moisture { setmoisture = param.asInt(); // assigning incoming value from pin V2 to a variable } |
IoT Soil Sensor code explanation:
1 |
char auth[] = "R8SYsf1UqRby1dH0KR6Ev9ctB0oEeLui"; |
This is the authorization code that is sent while making the blynk application. So, simply copy your authorization code and paste it over here.
1 2 3 4 5 |
/* WiFi credentials */ char ssid[] = "AndroidAP3DEC"; char pass[] = "electroniclinic"; |
Next, enter your WiFi credentials, your WiFi router name or your smartphone Hotspot name, and the password.
1 2 3 4 5 6 7 8 9 |
#define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); |
These lines of codes are used with the Oled display module.
1 2 3 4 5 6 7 8 9 10 11 |
int msensor = A0; // moisture sensor is connected with the analog pin A0 of ESP8266 int msvalue = 0; // moisture sensor value int setmoisture; int mspercent; // moisture value in percentage int Relay = D0; // you can connect a dc or ac water pump int buzzer = D4; |
Next, I defined some variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void setup() { Serial.begin(115200); Blynk.begin(auth, ssid, pass); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); delay(2000); display.clearDisplay(); display.setTextColor(WHITE); // initialize the sensor pinMode(msensor, INPUT); pinMode(Relay, OUTPUT); pinMode(buzzer, OUTPUT); } |
Inside the setup() function, I activated the Serial communication, Blynk, and Oled display, and also told the controller which pins are going to be used as the input and output. The rest of the code is pretty straightforward.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Blynk.run(); msvalue = analogRead(msensor); mspercent = map(msvalue,0,1023,100,0); // To display the soil moisture value in percentage display.clearDisplay(); display.setCursor(10,0); display.setTextSize(2); display.setTextColor(WHITE); display.print("SM:"+String(mspercent)+"%"); display.setCursor(10,30); display.setTextSize(2); display.print("SV:"+String(setmoisture)); display.display(); Blynk.virtualWrite(V1,mspercent); |
We simply run the Blynk, read the soil moisture sensor, map the moisture value to display the soil moisture value in percentage. Finally, we print the values on the Oled display module and also send the values to the Blynk application through virtual pin V1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
if (mspercent<setmoisture ) { Blynk.notify("Soil moisture decreased!!!"); digitalWrite(buzzer,HIGH); } else { digitalWrite(buzzer,LOW); } |
We use this if condition to check if the soil moisture is dropped below the setmoisture value which is the set value and then the controller accordingly sends the notification message and controls the buzzer.
1 2 3 4 5 6 7 |
BLYNK_WRITE(V2) // To set soil moisture { setmoisture = param.asInt(); // assigning incoming value from pin V2 to a variable } |
If you remember, I was using a slider on the Blynk application to set the moisture level. The value from the slider is sent to the Nodemcu ESP8266 WiFi module through this virtual pin V2. So, that’s all about the programming.
Thank you sir…