Help needed with a Soil Moisture Sensor

Hi everyone,

I´m new to Home Assistant and especially programming. I have a very big problem. I want to use the following Soil Moisture Sensor: Soil moisture sensor, moisture detector and plant moisture measuring device with corrosion-resistant probe for automatic irrigation system compatible with Arduino and Raspbery Pi. It´s a sensor you can find in many shops on the internet. But I have no idea what´s the right code for YAML File. I have the following code:

sensor:
  - platform: adc
    pin: A0
    name: "BDF_SP_M.1"
    id: moisture_sensor
    update_interval: 60s
    filters:
      - sliding_window_moving_average:
          window_size: 15  # Fenstergröße, um die Werte zu glätten
          send_every: 15

  - platform: template
    name: "BDF_SP_M.1 Prozent"
    lambda: |-
      auto sensor_value = id(moisture_sensor).state;
      int dryAnalogValue = 1023;
      int wetAnalogValue = 0;
      int moistureValuePercent = map(sensor_value, dryAnalogValue, wetAnalogValue, 0, 100);
      return moistureValuePercent;
    update_interval: 60s
    unit_of_measurement: "%"
    accuracy_decimals: 0

output:
  - platform: gpio
    pin: GPIO5
    id: led_output

binary_sensor:
  - platform: template
    name: "Pflanze trocken SP_M.1"
    lambda: |-
      if (id(moisture_sensor).state == 1023) {
        return true;
      } else {
        return false;
      }
    on_press:
      then:
        - output.turn_on: led_output
        - logger.log: "SP_M.1 Pflanze ist trocken!!"
    on_release:
      then:
        - output.turn_off: led_output

I got the code from Copilot, because I don´t find anything. The sensor measures all the time 0 V and 100 % Soil Moisture. Obviously that´s wrong. Does anyone has a solution and right code?
I use an NodeMCU V2 and a Raspberry Pi 4 with the sensor. And it´s planned that three sensors are connected with on microcontroller.
I don´t know if I can give any other important information now. I hope that´s enough.
Thanks very much in advance.

Copilot didn’t do so great job…
Anyway, you only need this:

sensor:
  - platform: adc
    pin: A0
    name: "BDF_SP_M.1"
    id: moisture_sensor
    update_interval: 1s

If you don’t get anything else than 0, your wiring is incorrect.
Also be aware that Nodemcu has only one analog input, you need Esp32 if you want to connect 3 sensors.

Oh, great thank you so much!
One additional thing: Do you maybe also have an idea how I can get the moisture in %? It wouldn´t be to bad if not, because the Voltage-Data is transmitted but the difference between very moist and dry is about 0,02 V.
Again thank you very much :slight_smile:

Your setup is not correct then.

You could post how you wired everything.

I wired it the following way:
A0 → Not used
D0 → A0
GND → GND
VCC → 3,3V
That´s according to the guide from AZ-Delivery.
I also tried A0 → A0 but the only difference is, that the measurement is about half of the voltage as before.
I also tried to change the blue potentiometer. The numbers change but not in a signifiant amount

That’s incorrect.
AO is analog output and you need to connect that to Esp A0 pin.
DO is digital output, it gives on/off according to potentiometer adjustment.
Try again, AO to A0. Leave DO unused.
The voltage difference is not high between dry/wet, but expect more than 0.02V.

You can add this to your sensor code for higher resolution:
accuracy_decimals: 4

Ok, thanks. I will try that later. :+1:

You got it working?
If you need moisture output, you heed to add some calibration filter.

Sorry, that I respond only now. I could not try it before today. Its basically working as before. The Voltage numbers are very small and the differences between them are barely enough. But for now it´s enough. I have a few other sensors that might give better data. :sweat_smile: