Hello Everyone,
Let me prephase my post with that I am very new to ESPHome and microelectronics in general
I managed to put together a soil moisture sensor using the following components:
- Seeed Studio Xiao-C3
- The popular Capacitive Soil Moisture Sensor v1.2 - I read a lot about the potential pitfalls of this sensor with regards to a missing voltage regulator and the wrong timer sensor, I believe I got the correct version
This is the code in ESPHome:
esphome:
name: wifi-moisture-sensor
friendly_name: Lawn Moisture Sensor
on_boot:
then:
- switch.turn_on: sensor_power
on_shutdown:
then:
- switch.turn_off: sensor_power
esp32:
board: esp32-c3-devkitm-1
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "..."
ota:
password: "..."
wifi:
ssid: "..."
password: "..."
fast_connect: on
power_save_mode: LIGHT
manual_ip:
static_ip: x.x.x.x
gateway: x.x.x.x
subnet: x.x.x.x
dns1: x.x.x.x
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Lawn Moisture Sensor"
password: "..."
captive_portal:
sensor:
- platform: adc
pin:
number: 3
allow_other_uses: True
name: "Soil Surface Moisture Voltage"
id: soil_surface_moisture_voltage
update_interval: 2s
attenuation: 11db
- platform: adc
pin:
number: 3
allow_other_uses: True
name: "Soil Moisture"
unit_of_measurement: "%"
device_class: MOISTURE
update_interval: 2s
attenuation: 11db
filters:
- calibrate_linear:
- 1.75 -> 100.00
- 2.61 -> 0.00
- lambda: |
if (x < 0) return 0;
else if (x > 100) return 100;
else return (x);
accuracy_decimals: 0
switch:
- platform: gpio
pin: GPIO10
id: sensor_power
internal: true
I did my sensor calibration and got ca. 1.75V submerged in a glass of water, ca. 2.61V out in dry air on room temperature. For now I am powering the Xiao via USB-C and using the 3v3 pin to connect the sensor’s VCC pin. This way the sensor works fine.
I am wondering about 2 things:
- As you can see in the config there is a GPIO pin configured to use as VCC input for the sensor to avoid the sensor draining on the battery whilst the Xiao is in deep sleep (that is to be configured later, I have the code for it). However, when I try to connect the VCC pin to the configured GPIO the sensor readings are no longer consistent. Going back to the 3v3 output pin, the sensor is once again working as it should. What am I doing wrong? Do I need to wire anything else?
- I would like to add some kind of a battery sensor calculation, as these sensors will be placed outdoors. II am planning to follow this wiring schematic for adding some resistors? What kind of resistors would you recommend to maximize energy efficiency?
Would this code work for me?
- platform: adc
pin:
number: 4
allow_other_uses: True
name: "Battery Life"
unit_of_measurement: "%"
device_class: BATTERY
update_interval: 2s
attenuation: 11db
filters:
- multiply: 2.0
- calibrate_polynomial:
degree: 4
datapoints:
- 3.4 -> 0.0
- 3.6 -> 8.43
- 3.81 -> 50.06
- 3.86 -> 57.63
- 3.92 -> 72.56
- 3.96 -> 90.96
- 4.05 -> 100
- lambda: |
if (x < 0) return 0;
else if (x > 100) return 100;
else return (x);
- platform: adc
pin:
number: 4
allow_other_uses: True
name: "Battery Voltage"
update_interval: 2s
attenuation: 11db
filters:
- multiply: 2.0