Overview: IoT Based Smart Agriculture & Automatic Irrigation System
In this project, we will learn about the IoT Based Smart Agriculture & Automatic Irrigation System with Nodemcu ESP8266. Agriculture plays a vital role in the development of agricultural countries. Some issues concerning agriculture have been always hindering the development of the country. Consequently, the only solution to this problem is smart agriculture by modernizing the current traditional methods of agriculture.
Hence the method is making agriculture smart using automation and IoT technologies. Internet of Things (IoT) enables various applications of crop growth monitoring and selection, automatic irrigation decision support, etc. We proposed ESP8266 IoT Automatic irrigation system to modernize and improve the productivity of the crop.
This post explains how to make IoT Smart Agriculture with Automatic Irrigation System using some simple sensors that are available in the market. We will use Capacitive Soil Moisture Sensor to measure moisture content present in the soil. Similarly to measure Air Temperature and Humidity, we prefer DHT11 Humidity Temperature Sensor. Using a 5V Power relay we will control the Water Pump. Whenever the sensor detects a low quantity of moisture in the soil, the motor turns on automatically. Hence, will automatically irrigate the field. Once the soil becomes wet, the motor turns off. You can monitor all this happening remotely via Thingspeak Server online from any part of the world.
Bill of Materials
We will need the fllowing electronics components for making this project.You can purchase all the components online from Amazon. The component purchase link is given below.
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | NodeMCU ESP8266 Board | 1 | Amazon | AliExpress |
2 | Capacitive Soil Moisture Sensor | 1 | Amazon | AliExpress |
3 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
4 | DHT11 Sensor | 1 | Amazon | AliExpress |
5 | 1 Channel 5V Relay Module | 1 | Amazon | AliExpress |
6 | 5V DC Motor Pump | 1 | Amazon | AliExpress |
7 | Connecting Wires | 10 | Amazon | AliExpress |
8 | Breadboard | 1 | Amazon | AliExpress |
Capacitive Soil Moisture Sensor
This is an analog capacitive soil moisture sensor which measures soil moisture levels by capacitive sensing. This means the capacitance is varied on the basis of water content present in the soil. You can convert the capacitance into voltage level basically from 1.2V minimum to 3.0V maximum. The advantage of Capacitive Soil Moisture Sensor is that they are made of a corrosion-resistant material giving it a long service life. The sensor can be used to make Automatic Plant Watering System.
Features & Specifications
- Supports 3-Pin Sensor interface
- Analog output
- Operating Voltage: DC 3.3-5.5V
- Output Voltage: DC 0-3.0V
- Interface: PH2.0-3P
- Size: 99x16mm/3.9×0.63″
To learn more about Capacitive Soil Moisture Sensor you can follow this post: Capacitive Soil Moisture Sensor Basic Interfacing Guide
DHT11 Humidity Temperature Sensor
The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air. It spits out a digital signal on the data pin.
It’s fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds. So when using the library, sensor readings can be up to 2 seconds old. In this project, we will use this sensor to measure the air temperature and humidity.
To learn more about DHT11 Humidity/Temperature Sensor you can follow this post: DHT11 Basic Interfacing Guide
DC 3-6V Micro Submersible Mini Water Pump
The DC 3-6 V Mini Micro Submersible Water Pump is a low cost, small size Submersible Pump Motor. It operates from a 2.5 ~ 6V power supply. It can take up to 120 liters per hour with a very low current consumption of 220mA. Just connect the tube pipe to the motor outlet, submerge it in water, and power it.
Features & Specifications
- Operating Voltage : 2.5 ~ 6V
- Operating Current : 130 ~ 220mA
- Flow Rate : 80 ~ 120 L/H
- Maximum Lift : 40 ~ 110 mm
- Outlet Outside Diameter: 7.5 mm
- Outlet Inside Diameter: 5 mm
Circuit Diagram & Connection
Let us see the schematic of the IoT Smart Agriculture & Automatic Irrigation System project. I use Fritzing to make an schematic for most of my projects. All you need is to place and connect a component that is super easy.
Connect the soil moisture sensor to A0 of Nodemcu and DHT11 to D4 Pin. The motor connects to Relay. To control the relay, we use the D5 Pin of NodeMCU. Connect the OLED display to the I2C pin of NodeMCU. You can power the Motor and Relay using the 5V pin of NodeMCU. The DHT11 Sensor, Capacitive Soil Moisture Sensor, and OLED Display require a 3.3V Supply only.
Project PCB Gerber File & PCB Ordering Online
If you don’t want to assemble the circuit on breadboard and you want PCB for the project, then here is the PCB for you. The PCB Board for the Smart Agriculture & Automatic Irrigation System is designed using EasyEDA online Circuit Schematics & PCB designing tool. The front side and back side of the PCB looks something like this.
The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from ALLPCB at 1$ only.
You can use this Gerber file to order high quality PCB for this project. To do that visit the ALLPCB official website by clicking here: https://www.allpcb.com/.
You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.
After filling all details, select your country and shipping method. Finally you can place the order.
Setting Up Thingspeak Server
Now we need to setup the Thingspeak Account. To set up Thingspeak follow the following Steps:
Step 1: Visit https://thingspeak.com/ and create your account by filling up the details.
Step 2: Create a New Channel by Clicking on “Channel” & fill up the following details as shown in the image below.
Step 3: Click on API Key, you will see the “Write API Key“. Copy the API Key. This is very important, it will be required in Code Part.
Step 4: You can click on the “Private View” & customize the display window as you want.
So, that’s all from the Thingspeak Setup Part. Now let us move to the programming Part.
Source Code/Program
The Source Code for IoT Smart Agriculture & Automatic Irrigation System with ESP8266 is very simple. The code can be directly uploaded to the NOdeMCU Board. But before that make sure to add OLED Display Library from the below link.
In the below code part, change the Thingspeak API Key, WiFi SSID & Password.
The most important part of this code is the calibration of the Soil Moisture Sensor Value like AirValue and WaterValue. To learn how to calibrate and get the correct reading follow this post: Calibrating Soil Moisture Sensor Value. Please!, do not skip this step. This may result in getting the wrong soil moisture value and sometimes may result in continuous turning ON of the motor.
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
#include <ESP8266WiFi.h> #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> // Including library for dht #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define DHTPIN D4 //pin where the dht11 is connected DHT dht(DHTPIN, DHT11); String apiKey = "C25ICK6FHOR7PST4"; // Enter your Write API key from ThingSpeak const char *ssid = "MySmartHome"; // replace with your wifi ssid and wpa2 key const char *pass = "nRF52840"; const char* server = "api.thingspeak.com"; Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); const int AirValue = 790; //you need to replace this value with Value_1 const int WaterValue = 390; //you need to replace this value with Value_2 const int SensorPin = A0; int soilMoistureValue = 0; int soilmoisturepercent=0; int relaypin = D5; WiFiClient client; void setup() { Serial.begin(115200); // open serial port, set the baud rate to 9600 bps display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64) display.clearDisplay(); pinMode(relaypin, OUTPUT); dht.begin(); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); delay(4000); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); Serial.print("Humidity: "); Serial.println(h); Serial.print("Temperature: "); Serial.println(t); soilMoistureValue = analogRead(SensorPin); //put Sensor insert into soil Serial.println(soilMoistureValue); soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); if(soilmoisturepercent > 100) { Serial.println("100 %"); display.setCursor(0,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.print("Soil RH:"); display.setTextSize(1); display.print("100"); display.println(" %"); display.setCursor(0,20); //oled display display.setTextSize(2); display.print("Air RH:"); display.setTextSize(1); display.print(h); display.println(" %"); display.setCursor(0,40); //oled display display.setTextSize(2); display.print("Temp:"); display.setTextSize(1); display.print(t); display.println(" C"); display.display(); delay(250); display.clearDisplay(); } else if(soilmoisturepercent <0) { Serial.println("0 %"); display.setCursor(0,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.print("Soil RH:"); display.setTextSize(1); display.print("0"); display.println(" %"); display.setCursor(0,20); //oled display display.setTextSize(2); display.print("Air RH:"); display.setTextSize(1); display.print(h); display.println(" %"); display.setCursor(0,40); //oled display display.setTextSize(2); display.print("Temp:"); display.setTextSize(1); display.print(t); display.println(" C"); display.display(); delay(250); display.clearDisplay(); } else if(soilmoisturepercent >=0 && soilmoisturepercent <= 100) { Serial.print(soilmoisturepercent); Serial.println("%"); display.setCursor(0,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.print("Soil RH:"); display.setTextSize(1); display.print(soilmoisturepercent); display.println(" %"); display.setCursor(0,20); //oled display display.setTextSize(2); display.print("Air RH:"); display.setTextSize(1); display.print(h); display.println(" %"); display.setCursor(0,40); //oled display display.setTextSize(2); display.print("Temp:"); display.setTextSize(1); display.print(t); display.println(" C"); display.display(); delay(250); display.clearDisplay(); } if(soilmoisturepercent >=0 && soilmoisturepercent <= 30) { digitalWrite(relaypin, HIGH); Serial.println("Motor is ON"); } else if (soilmoisturepercent >30 && soilmoisturepercent <= 100) { digitalWrite(relaypin, LOW); Serial.println("Motor is OFF"); } if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com { String postStr = apiKey; postStr += "&field1="; postStr += String(soilmoisturepercent); postStr += "&field2="; postStr += String(h); postStr += "&field3="; postStr += String(t); postStr += "&field4="; postStr += String(relaypin); postStr += "\r\n\r\n\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); } client.stop(); } |
Testing & Results
This water pump need to be fully submerged in water. The outlet pipe is kept in a field for irrigation. Similarly soil Moisture sensor is dipped in soil.
As soon as you power on the device, the OLED will start displaying the Soil Humidity, Air Humidity, and also Air Temperature. It shows the real-Time Data. When the soil moisture content is reduced the water pumps turn on and irrigate the field until the required moisture is achieved. You can check its full working here in this video below.
You can monitor the data online from any part of the world using Thingspeak Server. To do that, go to the private view of the Thingspeak server. You can check the soil Moisture, Humidity, and Temperature as well as relay status.
Video Tutorial & Guide
In case, you don’t need a WiFi connection and want to make this project using LoRa. Then you can follow our LoRa Based Smart Agriculture project. The LoRa wireless connectivity will basically remove all the WiFi Boundations.
38 Comments
given air value as 790 and water value as 390 and in comments its given to change as value 1 and value 2 where are value 1 and value 2 taken from in the code.
why didn’t you use temperature and humidity to map soil moisture percentage
Dear Mr. Alam!
I want to build your project, but no success sadly.
I have uploaded your code on my lolin board, but nothing happens, maybe i’ve messed up something in the arduino ide. Tried with nodemcu 1.0 (esp-12e), and with generic esp8266 module settings, but both of the are a failure. Wiring is ok, but nothing happens when is upload the code.
Hello , i ve not try the project , but have you change this with your parameter ?
String apiKey = “C25ICK6FHOR7PST4”; // Enter your Write API key from ThingSpeak
const char *ssid = “MySmartHome”; // replace with your wifi ssid and wpa2 key
const char *pass = “nRF52840”;
Yes replace the ssid password and api key with yours.
What if air RH and temp displays “nan”?
Change your pin number from 5 to 2 and see if it works. (See my other answers today, below, for why.)
What if air RH and temp displays “nan”? What should I do?
Either DHT11 Sensor is damaged or connected improperly.
Change your pin number (for the DHT11) from 5 to 2 and see if that works – see my other answers today, below, for why.
Is it okay if we used dht22?
Modify the code for dht22. See the dht22 example and change some declaration lines in the code.
Hi ! Powering the water pump directly with the esp isn’t it dangerous ? Can the esp32 deliver enough power to supply the water pump ? I mean 130-220 mA is a lot for this board, no ? Do you have a doc or something where this info is written ?
Thanks
Power the pump directly via relay. The relay can be activated by digita signal from ESP32. To isolate high voltage from lower voltage you may use optocoupler.
You write this “The Motor and Relay are powered using 5V via Vin pin of NodeMCU.” but I think the water pump ask for to much amp from the esp32 and that can damage you esp32. I think the best option is to use separate alimentation.
Hi! About values 1 and 2, are we going to copy 790 and 390 or should we change it?
You should change according to what your device shows.
What device, sir?
motor isnot turning off
can you just brief about it, what values need to be given
the motor is not turning offf
same error.
Where can I see or get the right values for value 1 and 2?
Hi! Our dht11 sensor is working now. Thanks! But our waterpump is not working. What must be the problem?
Where to find these values? and can you help with the motor not turning off
i have done almost all connections but relay part i’m getting error please help me
I’m also having trouble with the relay — it could be that the relay is 5v and isn’t getting enough power from the chip (especially if you’re powering it off any of the VCC pins which are 3.3V!). Only the Vin pin actually outputs 5V (the name is misleading here: in this case it means that the input voltage from the USB port is being passed through to this pin).
It’s still actually probably closer to 4.6V and you may need to provide an external 5V source either directly to the relay (I think) or to the common GND and VCC rails (the chip will handle 5V->3.3V internally).
(I’m not SURE of all this, so don’t hold me responsible, but I believe it’s accurate – corrections welcome!)
słuszna uwaga
projekt do poprawki bo zaraz się posypią komentarze w stylu “upaliłem płytkę”
Hello Alam,
Can this be used to work with LCD and in a Master Slave format where all sensors send data to single master over 2.4Ghz?
what software is suit to simulate soil npk sensor?
is this a successfull project??
can we use esp32 instead of esp8266?
plss ans it
why does my soil rh is always 100% and air rh with tempt showing “nan” ? please help 😦
Soil RH always 100% – don’t know, but check your wiring, and also maybe add delay to check it less often (see below).
Air RH + Temp showing “nan” – if you have a generic ESP8266, your pin # is probably wrong. Change the number from 5 (it’s pin D5) to 2 (pin D5 is GPIO 2 on some chips!). Like this: #define DHTPIN 2.
Also, change the delay to 4000 (in 3 places) — if you read the DHT11 too often (1 second or less), not enough time passes between reads.
i wanna ask… even though the moisture over 30%, the pump still on. how do i fix it?
NOT NECESSARILY! I had that problem – it turns out that for some generic ESP8266 chips, the GPIO -> D(pin number) mapping is NOT 5=5 (in this case). I changed the pin number in the code to 2 (#define DHTPIN 2) which is the correct GPIO number for pin D5. It works perfectly then!
how to delay abit from sending data to thingspeak?
The soil moisture percentage that appears on my OLED is significantly higher than the soil moisture percentage shown on my Thingspeak channel, any idea what I did wrong?
The VIN pin on this bord (NedeMCU V3, 0.9, LoLin) t isn’t functional. for me it worked conected on 3v pin. On some board goes even on VU pin. The VU pin output the power of 5V if the board is conected to an USB port or 5v source output!