Soil Moisture sensor output as a Percentage

Did you manage to get it to deep_sleep? I am trying to do so via mqtt but it’s not working…

My code:

# ESPhome Soil Moisture Sensor v 0.1
esphome:
  name: garden-moisture-sensor
  platform: ESP8266
  board: d1_mini

# WIFI Credentials
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

# Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Soilsensor Fallback Hotspot"
    password: !secret wifi_password

captive_portal:

# Enable logging
logger:

mqtt:
  broker: 192.168.x.xxx
  port: xxxx
  username: xxxx
  password: xxxx
  birth_message:
  will_message:
  on_message:
    - topic: /bedford/ota_mode
      payload: 'ON'
      then:
        - deep_sleep.prevent: deep_sleep_1
    - topic: bedford/sleep_mode
      payload: 'ON'
      then:
        - deep_sleep.enter: deep_sleep_1

ota:
  password: "xxxxxxx"

# Deep Sleep Feature. Needs adjustments...
deep_sleep:
  id: deep_sleep_1
  run_duration: 10s
  sleep_duration: 60min

sensor:
  - platform: adc
    pin: A0
    name: "Soil Moisture"
    update_interval: 600s
    unit_of_measurement: "%"
    filters:
    - median:
        window_size: 7
        send_every: 4
        send_first_at: 1
    - calibrate_linear:
        - 0.85 -> 0.00
        - 0.44 -> 100.00
    - lambda: |
        if (x < 0) return 0; 
        else if (x > 100) return 100;
        else return (x);

automation.yaml:

- alias: bedford sleep after mqtt receipt
  trigger:
    - platform: mqtt
      topic: bedford/sensor/bedford_pres/state
  action:
    - data:
        payload: "ON"
        topic: bedford/sleep_mode
      service: mqtt.publish
1 Like

have you connected GPIO16 to the RST pin so that it will wake up again?

This is definitely on my ‘to do’ list as well - did you get deep sleep working?

Hi there,
(I just started with some easy automation with Home Assistant) is there any way to have a calibration feature in Home Assistant for future calibration? These capacitive sensors run out of calibration really quickly… I was wondering about an easy way to calibrate them for the UI.
Thanks!

Thanks for the snippet :slight_smile:

I’m running on a D1 mini pro.
The sensor measures 1.3V wet and 2.5V dry.

It sends values between 143% and 171%
What do I do wrong here?

Cheers! :wink:

Sensor:
  - platform: adc
    pin: A0
    name: "Soil Moisture"
    update_interval: 2s
    unit_of_measurement: "%"
    #attenuation: 11db (only on ESP32)
    filters:
    - median:
        window_size: 7
        send_every: 4
        send_first_at: 1
    - calibrate_linear:
        - 1.3 -> 100
        - 2.5 -> 0
    - lambda: if (x < 1) return 0; else return (x);
    accuracy_decimals: 0

Look at my solution here: Outdoor soil moisture sensor - #23 by hubikj

It looks like your sensor measures much lower voltage than 1.3 V for wet. How did you get your calibration? You can fix the range by using the following lambda, but you should fix your calibration first.

    - lambda: |
        if (x < 0) return 0; 
        else if (x > 100) return 100;
        else return (x);
1 Like

Could relate to the D1 mini onboard voltage divider?

I used this guide to get deep sleep working with an helper entity instead of MQTT. Seemed like a much simpler approach to getting this up and running.

1 Like

You forgot that the Wemos boards have an external converter to put the 0.0-1.0 V range and spread it between 0.0-3.3V of real voltage.

So you need first a multiply filter of 3.3.

Then there’s the question which voltage the sensor is running at: I gpt some of the 2.0 Version. They can either run at 3.3 V or 5 V. Depending on the supplied voltage for VCC on them they’ll likely have a different voltage at the analog output.

thanks a bunch this works for me using an esp32 and a capacitive soil moisture sensor. just had to remove all the stuff below attenuation line to get the raw readings to get my 0 and 100 values. I have a question about the filters - median : window_size: 7 send_every: 4 send_first_at: 1" first what do these values correspond to? is this a timing thing where it skips sending data? I am using my esp 32 in a constant power environment so it should not need to sleep or worry about updating too much.

2 Likes

What this lambda do? How I find this voltage?
How can I put this auto calibration on esphome?
auto calibration program
Instead of finding the value, can’t I get a LOW variable where it is substituted for each value below LOW and a HIGH variable where it is substituted for each value above that value?

Are these single wire sensors, similar to the DS18B20, allowing them to be chained together on a single ESP 32? Or, can you use more than one, using different GPIO pins?

You need to use one pin per sensor in this case.

You can use one sensor on every GPIO. Otherwise you could also use an add which communicates with I2C such as the ADS1115. You can hook up to 4 of the adc which can each measure 4 sensors.

This ymal is for ESP8266 or ESP32?
I think A0 pin is in ESP8266 and, attenuation: 11db is for ESP32 only
so I am confused.

please help… I have this board: ESP-12F_Relay_X2 | devices.esphome.io. As far as I can see, it has an unused ADC port (it’s the one I should use to connect the sensor?), but it is 0-1 V. Does this mean that I can’t connect the sensor directly to it?

How do you find the output voltages of the sensor? I used 1.25 and 2.8 as values in my yaml code, because these were the in the first post that I found that had yaml code for a capacitive soil moisture sensor. The sensor is working, but it reports 87% when submerged in water. The linearity may be way off because the dry voltage may be significantly higher than the 1.25v.

I’ve tried loading the ESP32’s web page at the IP address assigned to the device, but only get

Thank you.

Remove the calibrate section and then upload and watch what the voltage is when dry and then wet. Then apply those voltages to the calibration section

Thank you. I thought that it was probably something like that, but I was hoping that there was some way to display it so that the code only had to be rewritten once. Oh, well.

Is it possible with these soil moisture sensors to show both the voltage as well as the calculated percentage moisture?