Controlling a soil moisture sensor

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
kwiley
Posts: 67
Joined: Wed May 16, 2018 5:53 pm
Contact:

Controlling a soil moisture sensor

Post by kwiley » Thu Jun 06, 2019 1:48 am

I working with an ESP-01S with the intent of creating (many) houseplant monitors. The ESP-01S will connect to one of those fairly ordinary soil moisture sensors (unfortunately the resistive types for now, corrosion issues notwithstanding), including the ADC-potentiometer board in between the sensor and the ESP. It's a fairly common device. To save power and minimize probe corrosion, I don't want to power the sensor continuously. I want it to power it for a second once or twice a day. Once the moisture level drops below a threshold, the ESP will stop testing the moisture, set the ESP's blue LED, and send some wifi data to my computer.

I'm currently using GPIO-0 to control power to the sensor, via a 2n7000 FET. GPIO-2 reads the sensor value. Since GPIO-2 is also connected to the ESP's blue LED, I was hoping to switch that GPIO from input to output when the soil dries out. At that point in time, I would stop using GPIO-0 to periodically power the sensor and resort to using GPIO-2 to control the LED.

The problem is the dual role assigned to GPIO-2. Assigned as an input, it properly reads the sensor's value. Assigned as an output, it properly enables control of the LED. The funny thing is that when I convert GPIO-2 to an output, suddenly the sensor starts getting power! This despite the fact that I am no longer power the sensor through GPIO-0 and the FET and despite the peculiar fact that GPIO-2 is only connected to the sensor's digital out line.

How on Earth is a GPIO connected to the sensor's digital output signal line powering the sensor on?

It's very strange.

I suppose the best solution to my problem is to use an additional GPIO from the ESP. It only offers two in its serial connected configuration, debugging at the computer for example, but once deployed in the field, no longer connected to a serial connection, I can obtain two additional GPIOs, the serial TX/RX lines. That would enable me to separate this dual-use I am trying to get away with on GPIO-2.

User avatar
Roberthh
Posts: 1569
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Controlling a soil moisture sensor

Post by Roberthh » Thu Jun 06, 2019 6:11 am

Instead of fighting with the restrictionsof the ESP01S, better use a different ESP8266 module with more GPIO's broken out, available for a few USD, like the Wemos LOLIN D1 mini.

kwiley
Posts: 67
Joined: Wed May 16, 2018 5:53 pm
Contact:

Re: Controlling a soil moisture sensor

Post by kwiley » Thu Jun 06, 2019 7:10 am

Yeah, but this is what I have lying around, so I'm going to work with it.

User avatar
Roberthh
Posts: 1569
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Controlling a soil moisture sensor

Post by Roberthh » Thu Jun 06, 2019 8:03 am

This despite the fact that I am no longer power the sensor through GPIO-0 and the FET and despite the peculiar fact that GPIO-2 is only connected to the sensor's digital out line.
That should be caused by the internals of the sensor. if you feed an output while the sensor is not powered, most likely the ESD protection diode of the output pin will feed current into Vdd. Use a diode (Anode to Sensor, Cathode to ESP) to stop this backfeeding. That will also block GPIO2 from pulled low during reset.

Post Reply