Saturday, 23 January 2016 08:27

DIY capacitive soil moisture sensor

Written by
Rate this item
(5 votes)
Testing the capacitive soil moisture sensor Testing the capacitive soil moisture sensor

I have a home-made automatic garden watering system at my country house. It's made on a base of 

Arduino Leonardo. It works quite properly - but there's always a place for perfectioning. So, I have thought: why water the ground when it's already wet - for example, after a rain?  And I decided to attach a soil moisture sensor to the automatic watering system.

At internet shops for a small price you can buy a soil moisture sensor, it’s operating principle is based on measuring of a resistance. More water in the soil – lesser the resistance, very simple.

 resistive soil moisture sensor

I also bought such a sensor, but it has some drawbacks: 

  • Small size. It may be useful for a flower pot, but at open ground it’s hardly to believe it’ll show the right results.  Because it’s possible to measure the moisture only of a few upper centimeters of a ground. What if, after a drought period, there happened a strong but short rain? Thin upper layer of soil will be wet (and the sensor will show it) – but at the depth of  plants’ roots it will still be dry.
  • Electrodes in the wet ground, being under constant voltage, are to corrode. This will result in sensor crash or improper readings.

Considering those flaws, I decided to make a capacitive soil moisture sensor. It’s a simple capacitor made of a double-sided PCB with a laser-iron technology. 

Capacitive soil moisture sensor

 For isolation the PCB is painted with acrylic car spray primer and paint.

It’s operating principle is based on a fact that the capacitance largely depends on a dielectric material between capacitor plates.

All materials have different relative permittivity. Permittivity of air is about 1, and the permittivity of water is 81. So, the capacitance of the sensor may multiple by 81 after pouring it from air into water.

But, measuring of a capacitance is not the easiest thing. For a microcontroller IC (on base of which I made my automated garden watering system) It’s much simpler to measure frequency. So, as the next step I made a simple oscillator based on SN74HC00 IC. It’s a quad NAND gate logic IC. Besides an IC, for this circuit are needed only 2 resistors and a capacitor – which is the moisture sensor.

 I was not completely sure that this schematic will work allright - so I made one more oscillator, that's based on a popular NE555 timer IC. It's quite simple, too.

The oscillator circuit generates pulsed square-wave voltage, which is connected to digital I/O port of the Arduino (digital pin 2). In my program external interrupt 0 is used, and it's attached to digital pin 2.  I used Arduino Duemillanove, but any Arduino will fit.


To display the result, I used Nokia 5110 LCD. In program comments it's described what Arduino pins are connected to corresponding LCD pins.


Here is the program:

#include <Adafruit_GFX.h>

#include <Adafruit_PCD8544.h>

#define  buffer 20

// pin 12 - Serial clock out (SCLK)

// pin 11 - Serial data out (DIN)

// pin 10 - Data/Command select (D/C)

// pin 9 - LCD chip select (CS)

// pin 8 - LCD reset (RST)

Adafruit_PCD8544 display = Adafruit_PCD8544(12, 11, 10, 9, 8);

volatile float freq=0.0;

volatile float frqarr[buffer];

volatile uint8_t farrptr=0;

volatile float avgfreq;

volatile unsigned long pulseDuration;

volatile unsigned long lastpulse;

float tmp;

const int analogInPin = A0;

int  AnalogSensorValue = 0;

  

void setup()   {

  pinMode(2, INPUT);

  display.begin();

  display.setContrast(50);

  display.setTextSize(2);

  display.setTextColor(BLACK);

  attachInterrupt(0, _spdint, RISING);

  pulseDuration=0;

}

  

void loop() {

 tmp=0;

  for(uint16_t i=0; i<buffer-1; i++){

    tmp+=frqarr[i];

  }

  avgfreq=tmp/buffer;

  

  if((micros() - lastpulse)>1000000) pulseDuration=0;

freq =  1000000.0/pulseDuration;

AnalogSensorValue=analogRead(analogInPin);

display.clearDisplay();   

  display.setCursor(0,0);  

  display.setTextSize(2);

  if(pulseDuration>0){

  display.print((int)freq);

  display.print(" Hz");}

  else display.print("No signal");

  display.setCursor(0,20);

  display.print((int)avgfreq); //average frequency of last 20 cycles

  display.print(" Hz");

  display.setCursor(0,40);

  display.setTextSize(1);

  display.print("Analog: ");

  display.print(AnalogSensorValue);

  display.display();

  delay(200);

}

void _spdint()

{

  unsigned long time = micros();

  pulseDuration = time - lastpulse;

  lastpulse=time;

  frqarr[farrptr]=1000000.0/pulseDuration;

  farrptr++;

  if(farrptr>=buffer) farrptr=0;

}



Then I placed the sensor into the pot and poured it with a dry soil from the garden. The soil was weighed. After that I started adding the water. Every time poured in 34 ml. of water – it’s a 2% of the weight of a soil. After every time I added water, I waited for 2 minutes to let the water evenly spread in the soil. Then the measurements of a frequency were made. 

The same actions were made with a resistive sensor. Its' analog output was connected to Arduino analog input 0.

This process you can see in a video:

 

The results were pleasant!

Capacitive soil moisture sensor test results

 I made a graph for an easier understanding: 

Capacitive soil moisture sensor testing results

It's clear that after the moment when percentage of water reached 12%, the value of the resistive sensor have not changed. And the capacitive soil moisture sensor changed the generated frequency up to the end!

To see it better - there is a graph from 12% to full saturation of the soil with water.

 

 Now all that's remaining is to solder the oscillator parts on a PCB, put it into a hermetic box - and connect it to the automatic watering system. But it'll be next time!

Read 13916 times Last modified on Sunday, 24 January 2016 21:37

Latest from DrElectr

8 comments

  • Comment Link Slobodan Wednesday, 11 April 2018 10:03 posted by Slobodan

    Hi,

    I made sensor as yours, but I have a problem in ino file with

    #include

    Can you help me?

    Report
  • Comment Link Cristiana Sunday, 11 June 2017 21:24 posted by Cristiana

    Good evening, I'm doing your humidity sensor and I'm having some doubts.
    I would like to know if you can give me your project at Proteus.

    Report
  • Comment Link mohsen mabrouk Sunday, 18 September 2016 03:31 posted by mohsen mabrouk

    hello;
    1- what do you means by laser iron to etch the circuit board copper surface?

    what a laser iron to etch the board?

    2- your code line
    if (micros() - lastpulse) > 1000 000 ) pulse duration = 0; ? or should be 1000 000
    freq = 1000 000/pulse duration; divide by zero

    thank you for the infomration on laser iron!!

    Report
  • Comment Link Mahendra Moghe Friday, 12 August 2016 18:00 posted by Mahendra Moghe

    Dear Dr. Electr, Million thanks for such a wonderful and comprehensive video on capacitive soil moisture sensor. I too am making a soil moisture sensor for my sugarcane Farm. Wanted to know what will happen if I use a PCB with copper on one side only?

    Report
  • Comment Link nipun Thursday, 19 May 2016 08:45 posted by nipun

    Hi, I am using sunrom soil moisture sensor http://www.sunrom.com/m/1282 . It gives output in UART as well as ADC. We are using in UART and It gives output like this : In water it reaches upto 140 and in wet soil it reaches upto 160 but it gives many random output. we are using this in 3.3v . I am confused in output of sensor because once i give water to plant and measure output it gives value in range of 140 then it increase in Day and decreases in night and also after 5 days it still gives same output. I wan to use this sensor for irrigation in farm and for irrigation cycle we are using tensiometer . So My questions are too many about sensors , behavior of soil in day time and night time so what to do ? Kindly answer about my questions . Sorry for too many explaination..:P..

    Report
  • Comment Link Quinones Hector Thursday, 31 March 2016 13:35 posted by Quinones Hector

    Hello. Excuse me, I was away for some days...

    I try to reproduce your circuit, but Im absolutly new in arduino (I`m agronim).
    I can not see very well the wiring from your video. Dou you a closeer photo to see the wiring?
    Or a FRITZIN diagrame?

    Thanks

    Hector

    Report
  • Comment Link Super User Friday, 18 March 2016 08:53 posted by Super User

    What kind of details can I explain?

    Report
  • Comment Link Quinones Hector Thursday, 17 March 2016 18:26 posted by Quinones Hector

    Hello,

    I`m very interested in soil moisture sensors.
    I found your sensor very interesting.

    I have not electronics knowledges. I`m agricultural engineering. I would like to reproduce your sensor in my arduino board, but I can not see very well the details from your video and I can not read very well the arduino code.

    Pleas, could you share with me somo more details ?

    Thanks you very much...

    Report
Login to post comments