In this project, we are going to interface Resistive Soil Moisture Sensor with NodeMCU ESP8266 & OLED Display & Monitor Soil Moisture Data on Thingspeak Server.
Overview
In this post, we are going to interface a Resistive Soil moisture sensor FC-28 with Nodemcu ESP8266 Board & 0.96″ OLED Display. This sensor measures the volumetric content of water inside the soil and gives us the moisture level as output. The sensor is equipped with both analog and digital output, so it can be used in both analog and digital modes. Here we will use the sensor in Analog mode and measure the soil moisture in percentage.
The measured soil moisture can be sent to any IoT cloud platform. In this case, we will use Thingspeak Server. Thinspeak server is an open data platform and API for the Internet of Things that enables you to collect, store, analyze, visualize, and act on data from sensors. The soil moisture data from Soil Moisture Sensor can be monitored online from any part of the world.
If you want to learn about another type of soil moisture sensor, i.e Capacitive Soil Moisture Sensor v1.2, you can check here: Capacitive Soil Moisture Sensor
Bill of Materials
Following are the components required for making this project. All the components can be purchased from Amazon. The components purchased link is given below.
| S.N. | Components Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | NodeMCU ESP8266 | 1 | Amazon | AliExpress |
| 2 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
| 3 | Connecting Wires | 10 | Amazon | AliExpress |
| 4 | Breadboard | 1 | Amazon | AliExpress |
| 5 | Soil Moisture Sensor | 1 | Amazon | AliExpress |
Resistive Soil Moisture Sensor
The soil Moisture sensor FC-28 consists of two probes that are used to measure the volumetric content of water. The sensor works between the input voltage range of 3.3V to 5V. The output voltage given by it is 0 – 4.2V. The output signal appears both in analog form and in digital form.
The soil Moisture sensor FC-28 has four pins
VCC: For power
A0: Analog output
D0: Digital output
GND: Ground
The Module also contains a potentiometer that will set the threshold value and then this threshold value will be compared by the LM393 comparator. The output LED will light up and down according to this threshold value.
Working of Resistive Soil Moisture Sensor
The soil moisture sensor consists of two probes that are used to measure the volumetric content of water. The two probes allow the current to pass through the soil and then it gets the resistance value to measure the moisture value.
When there is more water, the soil will conduct more electricity which means that there will be less resistance. Therefore, the moisture level will be higher. Dry soil conducts electricity poorly, so when there will be less water, then the soil will conduct less electricity which means that there will be more resistance. Therefore, the moisture level will be lower.
Interfacing Resistive Soil Moisture Sensor with Nodemcu & OLED Display
Here is a circuit diagram for interfacing Resistive Soil Moisture Sensor with Nodemcu ESP8266 Board. The connection is fairly simple.
We are using the soil moisture sensor in analog mode. So, connect the analog output pin to A0 of Nodemcu. Similarly, OLED Display is an I2C Module. So, connect its SDA SCL pin to D2 & D1 of Nodemcu. Both the OLED & Soil Moisture Sensor work at 3.3V. So connect their VCC pin to 3.3V of Nodemcu.
Monitoring Soil Moisture Sensor Data with Nodemcu on Thingspeak
In order to Monitor the Sensor Data on Thingspeak Server, you first need to Setup the Thingspeak. To setup the Thingspeak Server, visit https://thingspeak.com/. Create an account or simply sign in if you created the account earlier. Then create a new channel.
Copy the API key and use it in the code below.
Source Code/Program
Find the source code/program below for the Resistive Soil Moisture Sensor with OLED Display. You need to add two libraries for the code compilation.
1. Adafruit_SSD1306 : https://github.com/adafruit/Adafruit_SSD1306
2. Adafruit_GFX : https://github.com/adafruit/Adafruit-GFX-Library
Make sure to change the Wifi SSID & Password. Also, change the API key here.
|
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 |
#include <ESP8266WiFi.h> #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #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) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); String apiKey = "14K8UL2QEK8BTHN6"; // Enter your Write API key from ThingSpeak const char *ssid = "Alexahome"; // replace with your wifi ssid and wpa2 key const char *pass = "12345678"; const char* server = "api.thingspeak.com"; const int sensor_pin = A0; // Connect Soil moisture analog sensor pin to A0 of NodeMCU WiFiClient client; void setup() { Serial.begin(115200); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64) display.clearDisplay(); delay(10); Serial.println("Connecting to "); Serial.println(ssid); display.clearDisplay(); display.setCursor(0,0); display.setTextSize(1); display.setTextColor(WHITE); display.println("Connecting to "); display.setTextSize(2); display.print(ssid); display.display(); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); display.clearDisplay(); display.setCursor(0,0); display.setTextSize(1); display.setTextColor(WHITE); display.print("WiFi connected"); display.display(); delay(4000); } void loop() { int moisture_percentage; moisture_percentage = ( 100.00 - ( (analogRead(sensor_pin)/1023.00) * 100.00 ) ); Serial.print("Soil Moisture(in Percentage) = "); Serial.print(moisture_percentage); Serial.println("%"); display.clearDisplay(); display.setCursor(0,0); //oled display display.setTextSize(1); display.setTextColor(WHITE); display.println("Soil Moisture Monitor"); display.setCursor(40,20); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.print(moisture_percentage); display.setTextSize(1); display.setTextColor(WHITE); display.println(" %"); display.display(); if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com { String postStr = apiKey; postStr += "&field1="; postStr += String(moisture_percentage); postStr += "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); Serial.println("Data Send to Thingspeak"); } client.stop(); Serial.println("Waiting..."); delay(2000); // thingspeak needs minimum 15 sec delay between updates. } |
Results & Observations
Once the code is uploaded you can open the serial monitor and see the following.
Now you can dip the soil moisture probe in mud and see the value on OLED Screen or Serial Monitor. The more wet the soil the more the soil moisture percentage. Drier the soil, less the soil moisture percentage.
Now you can log into your thingspeak account. Then go to the private view. In the private view, you can see Thingspeak data getting uploaded after the interval of 15 seconds.













5 Comments
Is there a way to use more than 1 of these sensors to monitor several different plants?
With nodemcu its not possible as it only 1 analog pin. You can use esp32 for that purpose
Can we access data in Python for analyzing
it gives error me has no such directory
it gives me error Adafruit_I2CDevice.h: No such file or directory,how can i solve it