Home Assistant Garden Automation: Build Soil Moisture Sensor

If you’re like me, you’re always looking for ways to improve the health and productivity of your plants. One of the keys to a successful hydroponic setup is ensuring your plants get the right amount of water, and that’s where soil moisture sensors come in.

A good way to utilize a soil moisture sensor in hydroponic systems is in an ebb and flow setup. This enables regular control of flooding intervals, ensuring that as the plants grow and consume more water, flood frequency increases automatically and guarantees sufficient water supply for the plants.

This is just one idea for using a soil moisture sensor in hydroponic systems, and there are many more. In this post, I am going to walk you through the process of building your own soil moisture sensor with Home Assistant and ESPHome. You’ll have a customized, accurate, and affordable sensor by the end.

If you don’t know what Home Assistant is, then I will recommend you read my guide on home assistant for hydroponics first. Home Assistant is an open-source platform that allows you to control and monitor your connected devices, such as lights and sensors, with a single interface.

Materials for Building Soil Moisture Sensor

To build your own soil moisture sensor, you’ll need the following materials:

Circuit Diagram

Once you have your materials, it’s time to create the circuit. Here’s how to do it:

  1. Connect the VCC pin on the soil moisture sensor probe to the 5 pin on the ESP board (to be able to create a on/off switch, gonna come back to that later).
  2. Connect the GND pin on the sensor probe to the GND pin on the ESP board.
  3. Connect the OUT pin on the sensor probe to any available GPIO pin on the ESP board, such as GPIO14

Here’s a simple circuit diagram to help you visualize the connections:

ESPHome Configuration

Next, you’ll need to create an ESPHome configuration file to define the sensor and set up the pin configuration, If you are brand new to ESPhome in Home Assistant, then they have a guide here on how to get started. Else Here’s how to set up the sensor:

  1. Open the ESPHome Dashboard and click on the “Create New Node” button.
  2. Name your node and choose your ESP board type.
  3. In the “Edit Node” screen, click on the “Edit” button next to “esphome.yaml” to open the configuration file.
  4. Add the following code to define the sensor:
sensor:
  - platform: adc
    pin: 14
    name: moisture_ebb_flow_garden
    attenuation: 11db
    update_interval: never
    id: moisture1
    accuracy_decimals: 0
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
      - lambda: |-
          if (id(moisture_switch).state) {
            return (x < 1) ? 0 : ((x > 100) ? 100 : x);
          } else {
            return id(moisture1).state;

Prevent Corrosion

will create a switch to turn soil moisture sensors on and off. This helps prevent the sensor from corrosion by limiting the time it’s on.

in the same configuration file add this code:

switch:
  - platform: gpio
    pin: 5
    name: "moisture switch"
    id: moisture_switch
    on_turn_on:
      - delay: 3s
      - component.update: moisture1
      - logger.log: "moisture sensor updated"

when the switch is turned on it will wait 3 seconds and then update the sensor with the current moisture level and log it.

  1. Save your changes and click on the “Upload” button to flash the ESP board with the new configuration.

Home Assistant Integration

Now that your sensor is configured, and it should automatically appear in Home Assistant. If it doesn’t, you can manually add it by going to the Configuration tab and clicking “Integrations”.

Search for ESPHome and click on the “Configure” button next to it. Then click on the “+” button in the bottom right corner of the screen.

Choose your soil moisture sensor from the list and click on the “Submit” button.

Testing and Calibration

Before you start using your new soil moisture sensor, you’ll want to test and calibrate it to ensure accurate readings. Here’s how to do it:

  1. Fill a small container with water and place the soil moisture sensor probe in it.
  2. Check the sensor readings in Home Assistant and note the value.
  3. Remove the sensor probe from the water and let it dry completely.
  4. Recheck the sensor readings and note the value.
  5. These two values represent the maximum and minimum values for the sensor. Use these values to set the sensor reading range in the ESPHome configuration file.

In the ESPhome configuration file, add filters and a Calibrate_linear and replace them with the dry and wet values from the calibration test:

sensor:
  - platform: adc
    pin: 14
    name: moisture_ebb_flow_garden
    attenuation: 11db
    update_interval: never
    id: moisture1
    accuracy_decimals: 0
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    filters:
      - calibrate_linear:
          - "replace_value_dry" -> 0
          - "replace_value_wet" -> 100
      - lambda: |-
          if (id(moisture_switch).state) {
            return (x < 1) ? 0 : ((x > 100) ? 100 : x);
          } else {
            return id(moisture1).state;

Save your changes and upload the new configuration to the ESP board.

Test it for a few days and adjust the settings as needed to get accurate readings.

Conclusion

Congratulations, you’ve successfully built and installed an easy-to-use ESPHome soil moisture sensor for your garden! You can now check the moisture level in your garden from anywhere with ease. With a few tweaks, you can add additional sensors to measure other environmental factors such as temperature, humidity, and light intensity.

ESP Home is a great way to quickly and easily build custom IoT solutions for your home. Give it a try today!

Happy Gardening! : )

Similar Posts