In this project, we are going to interface Capacitive Soil Moisture Sensor with NodeMCU ESP8266/ESP32 & OLED Display. We will display the soil moisture value in percentage (%) both in Serial Monitor and 0.96″ OLED Display. We will learn the calibration method for displaying the correct value as well.
Overview
This project is about NodeMCU ESP8266 or ESP32 and Capacitive Soil Moisture Sensor with OLED Display. Soil moisture is basically the amount/content of water present in the soil. This can be measured using a soil moisture sensor either resistive or capacitive. Here we will use Capacitive Soil Moisture Sensor v1.2. This sensor measures the volumetric content of water inside the soil and gives us the moisture level as output.
This soil moisture sensor measures soil moisture levels by capacitive sensing rather than resistive sensing like other sensors on the market. It is made of corrosion-resistant material which gives it excellent service life. Insert it into the soil around your plants and monitor the real-time soil moisture data. This module includes an on-board voltage regulator which gives it an operating voltage range of 3.3 ~ 5.5V. It is perfect for low-voltage microcontroller with both 3.3V and 5V power supply.
You can go through our previous post to learn more about this sensor:
1. Interfacing Capacitive Soil Moisture Sensor with Arduino
2. IoT Based Smart Irrigation System
3. LoRa Based Smart Agriculture
Components Required
The following are the components required for making this project. All the components can be easily purchased from the Amazon. The components purchase links are given below as well.
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | NodeMCU ESP8266 Board | 1 | Amazon | AliExpress |
2 | ESP32 Board | 1 | Amazon | AliExpress |
3 | Capacitive Soil Moisture Sensor | 1 | Amazon | AliExpress |
4 | 0.96" I2C OLED Display | 1 | Amazon | AliExpress |
5 | Connecting Wires | 10 | Amazon | AliExpress |
6 | Breadboard | 1 | Amazon | AliExpress |
Capacitive Soil Moisture Sensor v1.2
Overview
This is an analog capacitive soil moisture sensor which measures soil moisture levels by capacitive sensing, i.e capacitance is varied on the basis of water content present in the soil. The capacitance is converted into voltage level basically from 1.2V 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.
Features & Specifications
1. Supports 3-Pin Sensor interface
2. Analog output
3. Operating Voltage: DC 3.3-5.5V
4. Output Voltage: DC 0-3.0V
5. Interface: PH2.0-3P
6. Size: 99x16mm/3.9×0.63″
Capacitive Soil Moisture Sensor Schematic
The Hardware Schematic for Capacitive Soil Moisture Sensor is given below.
There is a fixed frequency oscillator that is built with a 555 Timer IC. The square wave generated is then fed to the sensor like a capacitor. To a square wave signal that capacitor, however, has a certain reactance, or for argument’s sake a resistance that forms a voltage divider with a pure ohm type resistor (the 10k one on pin 3). The greater is the soil moisture, the higher the capacitance of the sensor. Consequently, there is a smaller reactance to the square wave, thus lowering the voltage on the signal line. The voltage on the Analog signal pin can be measured by an analog pin on the ESP8266 which represents the humidity in the soil.
0.96″ I2C OLED Display
This is a 0.96 inch blue OLED display module. The display module can be interfaced with any microcontroller using SPI/IIC protocols. It is having a resolution of 128×64. The package includes display board, display,4 pin male header pre-soldered to board.
OLED (Organic Light-Emitting Diode) is a self light-emitting technology composed of a thin, multi-layered organic film placed between an anode and cathode. In contrast to LCD technology, OLED does not require a backlight. OLED possesses high application potential for virtually all types of displays and is regarded as the ultimate technology for the next generation of flat-panel displays.
Capacitive Soil Moisture Sensor with ESP8266 & OLED Display
Now lets interface the Capacitive Soil Moisture Sensor with ESP8266 & OLED Display and display the analog value or soil moisture value in percentage. The circuit diagram for this is very simple.
Connect the VCC pin to 3.3V of NodeMCU and GND to GND. Similarly, connect the Analog output pin to A0 pin of NodeMCU ESP8266. The I2C OLED Display has 4 pins as VCC, GND, SDA & SCL. So Connect VCC to 3.3V of NodeMCU & GND to GND. Connect its SDA pin to D2 of NodeMCU & SCL to D1.
Source Code/Program
Here is a simple source code for Interfacing Capacitive Soil Moisture Sensor with NodeMCU ESP8266 OLED Display. Copy the code from below and upload it to your ESP8266 board.
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 |
#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); 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; 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(); } void loop() { 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(45,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.println("Soil"); display.setCursor(20,15); display.setTextSize(2); display.setTextColor(WHITE); display.println("Moisture"); display.setCursor(30,40); //oled display display.setTextSize(3); display.setTextColor(WHITE); display.println("100 %"); display.display(); delay(250); display.clearDisplay(); } else if(soilmoisturepercent <0) { Serial.println("0 %"); display.setCursor(45,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.println("Soil"); display.setCursor(20,15); display.setTextSize(2); display.setTextColor(WHITE); display.println("Moisture"); display.setCursor(30,40); //oled display display.setTextSize(3); display.setTextColor(WHITE); display.println("0 %"); display.display(); delay(250); display.clearDisplay(); } else if(soilmoisturepercent >=0 && soilmoisturepercent <= 100) { Serial.print(soilmoisturepercent); Serial.println("%"); display.setCursor(45,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.println("Soil"); display.setCursor(20,15); display.setTextSize(2); display.setTextColor(WHITE); display.println("Moisture"); display.setCursor(30,40); //oled display display.setTextSize(3); display.setTextColor(WHITE); display.println(soilmoisturepercent); display.setCursor(70,40); display.setTextSize(3); display.println(" %"); display.display(); delay(250); display.clearDisplay(); } } |
Once the code is uploaded the OLED Display will start showing the soil moisture value in percentage(%). Then test the soil moisture value by dipping the soil moisture sensor probe in water or in soil or any liquid.
Capacitive Soil Moisture Sensor with ESP32 & OLED Display
Now lets interface the Capacitive Soil Moisture Sensor with ESP32 & OLED Display and display the analog value or soil moisture value in percentage. The circuit diagram for this is very simple.
Connect the VCC pin to 3.3V of ESP32 and GND to GND. Similarly, connect the Analog output pin to the GPIO15 pin of ESP32. The I2C OLED Display has 4 pins as VCC, GND, SDA & SCL. So Connect VCC to 3.3V of ESP32 & GND to GND. Connect its SDA pin to D21 of ESP32 & SCL to D22.
Source Code/Program
Here is a simple source code for Interfacing Capacitive Soil Moisture Sensor with ESP32 & OLED Display. Copy the code from below and upload it to your ESP32 board.
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 |
#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); const int AirValue = 3620; //you need to replace this value with Value_1 const int WaterValue = 1680; //you need to replace this value with Value_2 const int SensorPin = 15; int soilMoistureValue = 0; int soilmoisturepercent=0; 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(); } void loop() { 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(45,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.println("Soil"); display.setCursor(20,15); display.setTextSize(2); display.setTextColor(WHITE); display.println("Moisture"); display.setCursor(30,40); //oled display display.setTextSize(3); display.setTextColor(WHITE); display.println("100 %"); display.display(); delay(250); display.clearDisplay(); } else if(soilmoisturepercent <0) { Serial.println("0 %"); display.setCursor(45,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.println("Soil"); display.setCursor(20,15); display.setTextSize(2); display.setTextColor(WHITE); display.println("Moisture"); display.setCursor(30,40); //oled display display.setTextSize(3); display.setTextColor(WHITE); display.println("0 %"); display.display(); delay(250); display.clearDisplay(); } else if(soilmoisturepercent >=0 && soilmoisturepercent <= 100) { Serial.print(soilmoisturepercent); Serial.println("%"); display.setCursor(45,0); //oled display display.setTextSize(2); display.setTextColor(WHITE); display.println("Soil"); display.setCursor(20,15); display.setTextSize(2); display.setTextColor(WHITE); display.println("Moisture"); display.setCursor(30,40); //oled display display.setTextSize(3); display.setTextColor(WHITE); display.println(soilmoisturepercent); display.setCursor(70,40); display.setTextSize(3); display.println(" %"); display.display(); delay(250); display.clearDisplay(); } } |
Once the code is uploaded the OLED Display will start showing the soil moisture value in percentage(%). Then test the soil moisture value by dipping the soil moisture sensor probe in water or in soil or any liquid.
Capacitive Soil Moisture Sensor Calibration
While talking about the accuracy, the capacitive soil moisture sensor is not so much accurate as expected. But you can do the calibration to get the closest accurate reading. Just upload the simple code to ESP8266/ESP32 and check the sensor analog reading when the sensor is in dry air and when the sensor is in water. From here you can find the maximum and minimum analog value that can be mapped to percentage value from 0 to 100% as per program.
1 2 |
constintAirValue = 600; //you need to replace this value with Value_1 constintWaterValue = 350; //you need to replace this value with Value_2 |
From above change the AirValue and WaterValue with the value you got.
If you want to learn about the Resistive Soil Moisture Sensor you can following the following post: Monitor Resistive Soil Moisture Sensor Data with Nodemcu on Thingspeak
8 Comments
Great (and timely) project for the season. I wonder if this can be made more useful by incorporating multiple sensors? I also wonder how far from the sensor the ESP8266 can be?
Great project, though!
-SR
Hi, thanks for the valuable feedback. I am thinking to incorporate automatic irrigation system on the basis of soil moisture analysis. I will do that in coming week. Cheers
Could you please quantify :
How much do the calibration numbers vary from one individual sensor to another?
How long does Vcc have to remain stable before the analog output is stable?
How does analog output vary with Vcc slowly (over days) declining from 5V to 3..5 volts?
Thanks.
Great tutorial!! Just one quick question.. Everything has been connected correctly but when not in water, the screen is outputting 100% moisture and the value is not changing – regardless if in water or not. Any idea why this is happening?
Great tutorial!! Just one quick question.. Everything has been connected correctly but when not in water, the screen is outputting 100% moisture and the value is not changing – regardless if in water or not. Any idea why this is happening?
Is it possible to obtain the dielectric permittivity with this sensor?
I had the same issue but resolved it by swapping the around the values in the map function. So it looks like this:
soilmoisturepercent = map(soilMoistureValue, WaterValue, AirValue, 100, 0);
hi,Can you tell me how the analog circuit used in your article is drawn, I want to write an undergraduate thesis and want to use this beautiful illustration. Thank you so much for being able to tell me