In this project, we are going to interface Capacitive Soil Moisture Sensor with Arduino & 16×2 LCD Display & display the soil moisture value in percentage (%) both in Serial Monitor and LCD Display & OLED Display. We will learn the calibration method for displaying the correct value.
Overview
This project is about Arduino and Capacitive Soil Moisture Sensor & LCD/OLED Interfacing Tutorial. 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 onboard 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.
Bill of Materials
The following are the components that you need for making this project. You can purchase all the components from Amazon.
S.N. | Components | Quantity | Purchase Links |
---|---|---|---|
1 | Arduino UNO Board | 1 | Amazon | AliExpress |
2 | Capacitive Soil Moisture Sensor | 1 | Amazon | AliExpress |
3 | 16x2 LCD Display | 1 | Amazon | AliExpress |
4 | 0.96" 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 Arduino which represents the humidity in the soil.
To learn more about the sensor, check the Capacitive Soil Moisture Sensor Datasheet.
Interface Capacitive Soil Moisture Sensor with Arduino
Now lets interface the Capacitive Soil Moisture Sensor with Arduino 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 Arduino and GND to GND. Similarly, connect the Analog output pin to A0 pin of Arduino.
Basic Source Code/Program
Here is a simple source code for Interfacing Capacitive Soil Moisture Sensor with Arduino. Copy the code from below and upload it to your Arduino 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 |
const int AirValue = 620; //you need to replace this value with Value_1 const int WaterValue = 310; //you need to replace this value with Value_2 int soilMoistureValue = 0; int soilmoisturepercent=0; void setup() { Serial.begin(9600); // open serial port, set the baud rate to 9600 bps } void loop() { soilMoistureValue = analogRead(A0); //put Sensor insert into soil Serial.println(soilMoistureValue); soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); if(soilmoisturepercent >= 100) { Serial.println("100 %"); } else if(soilmoisturepercent <=0) { Serial.println("0 %"); } else if(soilmoisturepercent >0 && soilmoisturepercent < 100) { Serial.print(soilmoisturepercent); Serial.println("%"); } delay(250); } |
Once the code is uploaded you can click on serial monitor & check 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.
Interface Capacitive Soil Moisture Sensor with Arduino & 16X2 LCD Display
Now lets interface the Capacitive Soil Moisture Sensor with Arduino & LCD 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 Arduino and GND to GND. Similarly, connect the Analog output pin to A0 pin of Arduino. Connect 1, 3, 16 pin of LCD to GND & connect 2, 15 to 5V VCC. Similarly connect pin 4, 6, 11, 12, 13, 14 of LCD to Arduino 12, 11, 5, 4, 3, 2 of Arduino.
Source Code/Program
Here is a simple source code for Interfacing Capacitive Soil Moisture Sensor with Arduino & 16×2 LCD Display. Copy the code from below and upload it to your Arduino 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 |
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); const int AirValue = 600; //you need to replace this value with Value_1 const int WaterValue = 350; //you need to replace this value with Value_2 int soilMoistureValue = 0; int soilmoisturepercent=0; void setup() { Serial.begin(9600); // open serial port, set the baud rate to 9600 bps lcd.begin(16, 2); } void loop() { soilMoistureValue = analogRead(A0); //put Sensor insert into soil Serial.println(soilMoistureValue); soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); if(soilmoisturepercent >= 100) { Serial.println("100 %"); lcd.setCursor(0, 0); lcd.print("Soil Moisture"); lcd.setCursor(0, 1); lcd.print("100 %"); delay(250); lcd.clear(); } else if(soilmoisturepercent <=0) { Serial.println("0 %"); lcd.setCursor(0, 0); lcd.print("Soil Moisture"); lcd.setCursor(0, 1); lcd.print("0 %"); delay(250); lcd.clear(); } else if(soilmoisturepercent >0 && soilmoisturepercent < 100) { Serial.print(soilmoisturepercent); Serial.println("%"); lcd.setCursor(0, 0); lcd.print("Soil Moisture"); lcd.setCursor(0, 1); lcd.print(soilmoisturepercent); lcd.print(" %"); delay(250); lcd.clear(); } } |
Once the code is uploaded you can LCD starting to display 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.
Interface Capacitive Soil Moisture Sensor with OLED Display & Arduino
Now lets interface the Capacitive Soil Moisture Sensor with OLED Display & Arduino 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 Arduino and GND to GND. Similarly, connect the Analog output pin to A0 pin of Arduino. The I2C OLED Display has 4 pins as VCC, GND, SDA & SCL. So Connect VCC to 5V of Arduino & GND to GND. Connect its SDA pin to A4 of Arduino & SCL to A5.
Source Code/Program
Here is a simple source code for Interfacing Capacitive Soil Moisture Sensor with OLED Display & Arduino. Copy the code from below and upload it to your Arduino 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 |
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); const int AirValue = 620; //you need to replace this value with Value_1 const int WaterValue = 310; //you need to replace this value with Value_2 int soilMoistureValue = 0; int soilmoisturepercent=0; void setup() { Serial.begin(9600); // 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(A0); //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 Arduino 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 values that can be mapped to percentage values from 0 to 100% as per the 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.
Video Tutorial: Interface Capacitive Soil Moisture Sensor with Arduino & LCD
You can check the application of this sensor here: IoT Based Smart Agriculture System.
19 Comments
Hi .Thanks a lot for the project.Its my first project, although i have arduino over 10 years now.I had a small issue :When moisture is 0% or 100% lcd doesn’t print “Soil Moisture 0%” or “Soil Moisture 100%” , it starts printing from 1 to 99 .Add symbol “=” after symbol “>” and “<” to these lines ” if(soilmoisturepercent > 100)” “else if(soilmoisturepercent <0)” and its ok.
So, you have ” if(soilmoisturepercent >= 100)” “else if(soilmoisturepercent <=0)”
Thanks for the correction and for letting me know my small mistake.
I forgot to add ” if(soilmoisturepercent >= 100)” “else if(soilmoisturepercent <=0)”.
Hey,
nice work! i was seachring for a shematic of the 555 Timer circuit used in this sensor. Now i found yours, and simulated it using Falstad. However is there a connection Between RST Pin 4 and VCC missing?
The simulated circuit only works if I connect RST and VCC, but they are not connected in you shematic right?
Hi,
Have you monitored the readings over time when the sensor is dipped in soil? I am using it for a irrigation project, but the sensor readings are hard to interpret since there is no visible pattern showing the soil getting dry.
Thanks,
Manuel
Is the code above for oled is with changes?
I used the same code. But it shows only soil and moisture in the display. Percentage is not displaying in the display
Hi
Did u find a solution for these soil sensors reliability, as I am experiencing something similar to what u just explained.
Thanks
I think it is because the output is not buffered. I would suggest very short (and shielded) leads from the sensor to a unity gain amp (buffer) before connecting it to the analog input. Also be aware the Vref for the internal ADC is not regulated, so power fluctuations in the supply will cause both the 555 output and the Analog input to vary. This can partly be smoothed out by taking multiple samples and averaging them.
The 555 circuit is not correct. R3 is 330 ohms, not 330k. Pin 4 (RST) should be tied to Vcc
Could you please explain where Value_1 and Value_2 are obtained. There seems to be no mention of them elsewhere?
I think the analog read function will help you – read the values from a given pin in air and water
You left out some really important information…
The value of “AirValue” (or “Value_1”) is the value of the moisture sensor when dry in air, and “WaterValue” (or “Value_2”) is the value of the moisture sensor when submerged in water.
He forgot a critical component, see my comment below.
Thank you!
we tried to set this up and run it… LED gave us garbage lines on line 1, and never worked. Also – you never really explained, listed or showed how the potentiometer is a part of this or what it does. Are we missing something here?
What does VCC stand for ?
Quellcode für die Schnittstelle des kapazitiven Bodenfeuchtesensors mit Arduino und einem 16×2 LCD Display hat Fehler.Muss ich jetzt mit 75 Jahren noch Linux lernen.
MfG. H. Lutz
Thank you so much! Your finding really helps me when I am still wondering why my serial print value keeps showing 0%.
Thank you so much! I used this as part of my auto irrigation system, and it worked great. Thank you!