Capacitiv Moisture Sensor

Hi Folks,

I have some Capacitive Soil-Moisture sensors laying around and some battery holders.
Is there, or does anyone know of a guide to make these things work without the ThingsOnEdge wifi module cause it’s price is not worth the effort.

Doesn’t they just have an analoge output of a voltage?

That could probably be used in ESP-Home and calibrated using some function. What does the documentations of the sensor say?

I have similar ones. I integrated them with an ESP8266 board using ESPHome.
Basically, the output of this sensor will be converted by the ADC to a value between 0 V and 1 V. To calibrate it, I captured the value on a “dry” and a “wet” (freshly irrigated) soil. The two obtained values are 0.82 V and 0.36 V, respectively.

Logs from ESP8266 (when it is placed in a dry soil)

[adc:104]: 'Soil Moisture': Got voltage=0.82V

Logs from ESP8266 (when it is placed in a wet soil)

[adc:104]: 'Soil Moisture': Got voltage=0.36V

Finally, I used the fuction calibrate_linear to convert the ADC value to a “human readable one” between 0 and 100%:

sensor:
  - platform: adc
    id: soil_moisture
    pin: A0
    name: "Soil Moisture"
    unit_of_measurement: "%"
    device_class: humidity
    accuracy_decimals: 1
    filters:
      - calibrate_linear:
          - 0.82 -> 0
          - 0.36 -> 100
    update_interval: 20s

PS: You can also use the following lambda instead of the calibrate_linear:

    filters:
      - lambda: |-
          float moisture_dry_soil_value= 0.82;
          float moisture_wet_soil_value =  0.36;
          if (x >  moisture_dry_soil_value ) {
            return 0;
          } else if (x < moisture_wet_soil_value) {
            return 100;
          } else {
            return (moisture_dry_soil_value - x) / (moisture_dry_soil_value - moisture_wet_soil_value ) * 100.0;
          }

After flashing this config to my ESP8266:

# The raw ADC value
[01:25:40][D][adc:104]: 'Soil Moisture': Got voltage=0.49V 

# The converted Value
[01:25:40][D][sensor:121]: 'Soil Moisture': Sending state 71.68818 % with 1 decimals of accuracy 

#  (0.82 - 0.49) / (0.82 - 0.36) * 100 = 71.6888.....

I know that it is not 100% accurate (and never will be the case), but at least I have an idea about the moisture level of the soil. I highly recommend you to watch the variation of this value for some weeks and re-calibrate your sensor… Then you can define the % threshold under which you have to automatically trigger the irrigation system :wink: .

Peace!

2 Likes

Just wanted to say thanks for this example, it worked great. I used esp32, which has multiple ADC, so you can put several of them on one esp board. I also added sensors for each pin both with and without the linear filter, so that I have sensors that show both the voltage read on the pin, and the moisture %.
For the esp32 board, you have to add the “attenuation: auto” to get the real voltage.
(which in my case seems to be about 1.4 in a cup of water, and 2.9 when completely dry)

I ended up with a config like so for each one:

sensor:
  - platform: adc
    pin: GPIO36
    attenuation: auto
    name: "Soil Moisture 1"
    unit_of_measurement: "%"
    device_class: humidity
    icon: "mdi:watering-can"
    filters:
      - calibrate_linear:
          - 2.89 -> 0
          - 1.46 -> 100
    update_interval: 20s
    accuracy_decimals: 0

  - platform: adc
    pin: GPIO36
    attenuation: auto
    name: "Soil Voltage 1"
    unit_of_measurement: "V"
    device_class: voltage
    update_interval: 20s
    accuracy_decimals: 2
1 Like