Capacitive soil moisture sensor project not going as planned!

I’m having a lot of trouble with the capacitive soil moisture sensors giving me terrible readings in ESPhome. I’m using this ESP32 Esp32 Development Board Wifi+bluetooth Ultra-low Power Consumption Dual Core Esp-32 Esp-32s Esp 32 D1 V1.0.0 Similar Esp8266 - Integrated Circuits - AliExpress and these sensors Capacitive Soil Moisture Sensor Module Not Easy to Corrode Wide Voltage Wire 3.3~5.5V Corrosion Resistant W/ Gravity for Arduino|Integrated Circuits| - AliExpress. The problem is that I get around a 0.1v difference in readings from fully submerged in water and dry dusty soil which I think is not really a big enough range for an accurate reading. I see a lot of people getting over 1 volts difference on different posts which seems more realistic.

I have tried playing with the attenuation part of the code, auto seems to work the best, 11db just gives me a reading of around 3.1v which never changes (wet or dry) and I removed the attenuation line altogether and then it gives me around 1v which never changes.

I’ve tried using the 5v and the 3.3v pin for power, have tried GPIO 36 and 32 for data inputs.

I found this video Capacitive Soil Moisture Sensors don't work correctly + Fix for v2.0 v1.2 Arduino ESP32 Raspberry Pi - YouTube and found that my sensors are the type that have the correct components except for the trace not being connected between the resistor so I scratched the coating off and soldered a jumper wire across. Didn’t really find any difference between the readings after doing this and another identical sensor I have without the mod.

I have a DS18B20 temperature probe attached to the board which is reading perfectly, just can’t seem to get the moisture sensors working properly. Maybe there is a problem with my code? This is my first project and I have spent HOURS and HOURS online trying to figure out code, reading forum posts, watching videos and got nowhere, very frustrating considering how simple it should be.

Any help would be highly appreciated!

My code:

esphome:
  name: garden-sensors

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
 level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: "HDJSNMC9qQy0f6CuhjAHxUaRNxd9ooIme0DfIv7IrTY="

ota:
  password: "32198b18cdb2429c1d3d43ea6c1d5c8d"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Garden-Sensors Fallback Hotspot"
    password: "gcREuMm9bg3N"

captive_portal:

sensor:
  - platform: adc
    pin: 32
    attenuation: auto
    name: "Soil Voltage 3"
    unit_of_measurement: "V"
    device_class: voltage
    update_interval: 5s
    accuracy_decimals: 1

a 0.1v reading is not right. Your sensor should go from 0v to 3v so you need to use 11db

sensor:
  - platform: adc
    pin: 32
    name: "Soil Voltage 3"
    update_interval: 5s
    unit_of_measurement: ""
    icon: "mdi:current-dc"
    accuracy_decimals: 2
    attenuation: 11db
    filters:
###    - multiply: 0.00095238 # 3.9/4095, for attenuation 11db
    - median:
        window_size: 60
        send_every: 60
        send_first_at: 6
 
 - platform: adc
    pin: GPIO32
    name: "Soil Percentage"
    update_interval: 5s
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    attenuation: 11db
    filters:
###    - multiply: 0.00095238 # 3.9/4095, for attenuation 11db
    - median:
        window_size: 60
        send_every: 60
        send_first_at: 6
    - calibrate_linear: ## This can be used to convert volts to any value required.
          - 0.10 -> 0.0 ## Record volts 0.0 (from sensor) to 0.0 (true value) in min and max values
          - 2.89 -> 100.0 ## Record volts 0.0 (from sensor) to 0.0 (true value) in min and max values
    - lambda: |
        if (x < 0.0) return 0.0;
        else if (x > 100.0) return (x);
        else return (x);

you will need to adjust the calibrate_linear voltage to your readings dry = “0.10” & wet = “2.89”. Then the lambda is so you dont get a negative % or a % over 100%.

you need to get your sensor to read around 3v when wet and around 0 volts when dry. If it doesn’t do it then it could be faulty.

This will give you a voltage reading and a % reading.

It will sample every 5 min… works like this . update interval 5 & medin send every 60 so 5 x 60 seconds is 5 min. keep your window size and send every the same. your first reading on boot is 5 x send first 6 so it will take the ave over your first 6 readings = 30 seconds. adjust to your liking. this helps keeping your readings stable and not jumping around

2 Likes

Thanks for your reply, I tried out your code and it didn’t really change anything. Seem to get a few readings and then after about 10 mins it just shows a constant voltage of 3.134V. I tried hard wiring the sensor to the ESP32 in case connector was faulty and also tried another sensor altogether.

Here are my logs, you can see after a while it starts giving me the constant voltage. The first reading was in open air, the second in water and the next 2 in dry soil.

[07:53:52][D][sensor:125]: 'Soil Voltage 3': Sending state 2.16650  with 2 decimals of accuracy
[07:53:52][D][sensor:125]: 'Soil Percentage': Sending state 74.10394 % with 0 decimals of accuracy
[07:58:52][D][sensor:125]: 'Soil Voltage 3': Sending state 2.54050  with 2 decimals of accuracy
[07:58:52][D][sensor:125]: 'Soil Percentage': Sending state 87.52687 % with 0 decimals of accuracy
[08:03:52][D][sensor:125]: 'Soil Voltage 3': Sending state 3.13400  with 2 decimals of accuracy
[08:03:52][D][sensor:125]: 'Soil Percentage': Sending state 108.74552 % with 0 decimals of accuracy
[08:08:52][D][sensor:125]: 'Soil Voltage 3': Sending state 3.13400  with 2 decimals of accuracy
[08:08:52][D][sensor:125]: 'Soil Percentage': Sending state 108.74552 % with 0 decimals of accuracy

Could the ESP be faulty? The voltage is the same when i disconnect the sensor.

Update: I think it was my bad soldering! I stumbled across a post Capacitive soil moisture sensor v2.0 with wemos d1 mini with someone having a similar constant voltage issue which turned out to be a bad connection. In a last ditch effort I desoldered the ESP and any other connections off the prototype board I was using and reconnected one sensor. To my surprise it started giving me a reasonable reading around 1v when in the open air and 2.3v when in water. This seems like a good enough range to be able to get reliable readings. Just need to calibrate the sensors and adjust code to suit each sensor as they all give slightly different readings which I can’t see being an issue (touch wood). All up I got 3 soil moisture sensors and 2 DS18B20 temperature probes working together that will eventually go outside in the garden.

You can see the voltage difference between the 2 sensors. Voltage 4 seems to read about 0.06 lower than voltage 3, but the third sensor I connected reads about 0.06 higher. Voltage 3 is the modified sensor which reads right in the middle. All readings seem pretty linear so it seems unnecessary to modify the other 2 sensors. Will just adjust this variance in the coding.

[13:33:32][D][sensor:125]: 'Soil Voltage 3': Sending state 2.29900  with 2 decimals of accuracy
[13:33:33][D][sensor:125]: 'Soil Voltage 4': Sending state 2.25700  with 2 decimals of accuracy
[13:33:33][D][sensor:125]: 'Soil Percentage 3': Sending state 78.81720 % with 0 decimals of accuracy
[13:33:34][D][sensor:125]: 'Soil Percentage 4': Sending state 77.34766 % with 0 decimals of accuracy
[13:34:22][D][sensor:125]: 'Soil Voltage 3': Sending state 2.29500  with 2 decimals of accuracy
[13:34:23][D][sensor:125]: 'Soil Voltage 4': Sending state 2.22850  with 2 decimals of accuracy
[13:34:23][D][sensor:125]: 'Soil Percentage 3': Sending state 78.67384 % with 0 decimals of accuracy
[13:34:24][D][sensor:125]: 'Soil Percentage 4': Sending state 76.30824 % with 0 decimals of accuracy
[13:35:12][D][sensor:125]: 'Soil Voltage 3': Sending state 2.29500  with 2 decimals of accuracy
[13:35:13][D][sensor:125]: 'Soil Voltage 4': Sending state 2.22900  with 2 decimals of accuracy
[13:35:13][D][sensor:125]: 'Soil Percentage 3': Sending state 78.67384 % with 0 decimals of accuracy
[13:35:14][D][sensor:125]: 'Soil Percentage 4': Sending state 76.21864 % with 0 decimals of accuracy
[13:36:02][D][sensor:125]: 'Soil Voltage 3': Sending state 2.37100  with 2 decimals of accuracy
[13:36:03][D][sensor:125]: 'Soil Voltage 4': Sending state 2.32150  with 2 decimals of accuracy
[13:36:03][D][sensor:125]: 'Soil Percentage 3': Sending state 81.43369 % with 0 decimals of accuracy
[13:36:04][D][sensor:125]: 'Soil Percentage 4': Sending state 79.82079 % with 0 decimals of accuracy
[13:36:52][D][sensor:125]: 'Soil Voltage 3': Sending state 1.10750  with 2 decimals of accuracy
[13:36:53][D][sensor:125]: 'Soil Voltage 4': Sending state 1.01800  with 2 decimals of accuracy
[13:36:53][D][sensor:125]: 'Soil Percentage 3': Sending state 35.98566 % with 0 decimals of accuracy
[13:36:54][D][sensor:125]: 'Soil Percentage 4': Sending state 32.68818 % with 0 decimals of accuracy
[13:37:42][D][sensor:125]: 'Soil Voltage 3': Sending state 1.07450  with 2 decimals of accuracy
[13:37:43][D][sensor:125]: 'Soil Voltage 4': Sending state 1.00500  with 2 decimals of accuracy
[13:37:43][D][sensor:125]: 'Soil Percentage 3': Sending state 34.94624 % with 0 decimals of accuracy
[13:37:44][D][sensor:125]: 'Soil Percentage 4': Sending state 32.41935 % with 0 decimals of accuracy
[13:38:32][D][sensor:125]: 'Soil Voltage 3': Sending state 1.07400  with 2 decimals of accuracy
[13:38:33][D][sensor:125]: 'Soil Voltage 4': Sending state 1.00200  with 2 decimals of accuracy
[13:38:33][D][sensor:125]: 'Soil Percentage 3': Sending state 34.94624 % with 0 decimals of accuracy
[13:38:34][D][sensor:125]: 'Soil Percentage 4': Sending state 32.32975 % with 0 decimals of accuracy
[13:39:22][D][sensor:125]: 'Soil Voltage 3': Sending state 1.07300  with 2 decimals of accuracy
[13:39:23][D][sensor:125]: 'Soil Voltage 4': Sending state 1.00200  with 2 decimals of accuracy
[13:39:23][D][sensor:125]: 'Soil Percentage 3': Sending state 34.89247 % with 0 decimals of accuracy
[13:39:24][D][sensor:125]: 'Soil Percentage 4': Sending state 32.31183 % with 0 decimals of accuracy
[13:40:12][D][sensor:125]: 'Soil Voltage 3': Sending state 2.33100  with 2 decimals of accuracy
[13:40:13][D][sensor:125]: 'Soil Voltage 4': Sending state 2.22000  with 2 decimals of accuracy
[13:40:13][D][sensor:125]: 'Soil Percentage 3': Sending state 80.01792 % with 0 decimals of accuracy
[13:40:14][D][sensor:125]: 'Soil Percentage 4': Sending state 74.87456 % with 0 decimals of accuracy
[13:41:02][D][sensor:125]: 'Soil Voltage 3': Sending state 2.30250  with 2 decimals of accuracy
[13:41:03][D][sensor:125]: 'Soil Voltage 4': Sending state 2.33700  with 2 decimals of accuracy
[13:41:03][D][sensor:125]: 'Soil Percentage 3': Sending state 79.46236 % with 0 decimals of accuracy
[13:41:04][D][sensor:125]: 'Soil Percentage 4': Sending state 80.16129 % with 0 decimals of accuracy
[13:41:52][D][sensor:125]: 'Soil Voltage 3': Sending state 1.70000  with 2 decimals of accuracy
[13:41:53][D][sensor:125]: 'Soil Voltage 4': Sending state 2.26550  with 2 decimals of accuracy
[13:41:53][D][sensor:125]: 'Soil Percentage 3': Sending state 57.15054 % with 0 decimals of accuracy
[13:41:54][D][sensor:125]: 'Soil Percentage 4': Sending state 77.32974 % with 0 decimals of accuracy
[13:42:42][D][sensor:125]: 'Soil Voltage 3': Sending state 2.33600  with 2 decimals of accuracy
[13:42:43][D][sensor:125]: 'Soil Voltage 4': Sending state 2.26400  with 2 decimals of accuracy
[13:42:43][D][sensor:125]: 'Soil Percentage 3': Sending state 80.12544 % with 0 decimals of accuracy
[13:42:44][D][sensor:125]: 'Soil Percentage 4': Sending state 77.74194 % with 0 decimals of accuracy
[13:43:32][D][sensor:125]: 'Soil Voltage 3': Sending state 2.31950  with 2 decimals of accuracy
[13:43:33][D][sensor:125]: 'Soil Voltage 4': Sending state 2.25550  with 2 decimals of accuracy
[13:43:33][D][sensor:125]: 'Soil Percentage 3': Sending state 79.56989 % with 0 decimals of accuracy
[13:43:34][D][sensor:125]: 'Soil Percentage 4': Sending state 77.27599 % with 0 decimals of accuracy
[13:44:22][D][sensor:125]: 'Soil Voltage 3': Sending state 2.30800  with 2 decimals of accuracy
[13:44:23][D][sensor:125]: 'Soil Voltage 4': Sending state 2.24850  with 2 decimals of accuracy
[13:44:23][D][sensor:125]: 'Soil Percentage 3': Sending state 79.19354 % with 0 decimals of accuracy
[13:44:24][D][sensor:125]: 'Soil Percentage 4': Sending state 76.98924 % with 0 decimals of accuracy
[13:45:12][D][sensor:125]: 'Soil Voltage 3': Sending state 2.30300  with 2 decimals of accuracy
[13:45:13][D][sensor:125]: 'Soil Voltage 4': Sending state 2.24400  with 2 decimals of accuracy
[13:45:13][D][sensor:125]: 'Soil Percentage 3': Sending state 78.97849 % with 0 decimals of accuracy
[13:45:14][D][sensor:125]: 'Soil Percentage 4': Sending state 76.91756 % with 0 decimals of accuracy
[13:46:02][D][sensor:125]: 'Soil Voltage 3': Sending state 2.29900  with 2 decimals of accuracy
[13:46:03][D][sensor:125]: 'Soil Voltage 4': Sending state 2.24300  with 2 decimals of accuracy
[13:46:03][D][sensor:125]: 'Soil Percentage 3': Sending state 78.81720 % with 0 decimals of accuracy
[13:46:04][D][sensor:125]: 'Soil Percentage 4': Sending state 76.81004 % with 0 decimals of accuracy

@bigdogevan - a soldering issue, good find. Yea all sensors read different and calibration is required. You will also find that they will fluctuate and accuracy is not perfect rather a guide.

1 Like

your problem is solved ?

Yes, was a soldering issue as above.

1 Like