Soil Moisture sensor with digital pins

I have a wemos D1 mini clone and I’m trying to use a soil moisture sensor like this:
Soil Moisture sensor on Ebay
If I connect the sensor to the VCC (3.3V) and the GND to the same ground the wemos clone is getting everything works ok.
If I try to connect the VCC of the sensor to the digital pin (D2 for my example) it doesn’t work and show 0 or a number close to it…

everything else is working fine… just this part…
I checked that the sensor really gets power and it does and outputs the correct voltage on the signal pin so that is working as well.
am I doing something wrong?

int soilSensorValue = analogRead(A0);
Blynk.virtualWrite(V11,map(soilSensorValue,0,1024,0,100));

also tried that:

//Soil Sensor Reading:
int SoilSensorReading()
{
    digitalWrite(D2, HIGH);
    int CurrentRead = analogRead(A0);
    digitalWrite(D2, LOW);
}

if I do it on the first example I get the results more or less… but I can live with them…
On the second example I get nothing… always 0 or close to that.
Please help me… I’ve been trying to find the solution for the last 4 hours and got nothing. :frowning:

Thanks in advance.

Why would you want to do that?

I read somewhere that if you keep it connected all time you reduce the sensor life span… so you hook it up to a digital pin and when you want to read the moisture you put it to HIGH and read from the analog pin… and then put the digital (VCC) pin to LOW…

if you say that it is nonsense then I’ll just use it without.

@sagirokach that is correct. What does Serial Monitor show if you change it to:

//Soil Sensor Reading:
int SoilSensorReading()
{
    digitalWrite(D2, HIGH);
    delay(20);
    int CurrentRead = analogRead(A0);
    Serial.println(CurrentRead);
    digitalWrite(D2, LOW);
}

Technically the WeMos can only accept 3.2V on the analogue port, not 3.3V. Most of the time it’s not a problem but if you have a pot trim it down to 3.2V.

Have you set pinMode() as output for D2?

I tried the way you suggested… and it gave me 0.
and I did set the pinmode for D2 to output.

anything else you can suggest?

What is the exact voltage between Vcc and GND and also D2 and GND?

For testing you might want to change the delay from 20 to 2000 to give you time to take the voltage reading before it goes LOW.

the voltmeter shows 2.4-2.8 more like 2.8… which is right for the reading…
yet what I get on the blynk variable is 16… out of 1024.

I need 2 readings:

  1. Vcc to GND when your WeMos is on.
  2. D2 to GND when D2 is HIGH
  1. 3.3
  2. 2.8

It should be close to 3.3V on HIGH and the voltage range for your device is 3.3V to 5.5V, ergo nothing at 2.8V.

wait!!! should I put 5volts? I’m using the 3.3…

No 3.3V is fine. The sensor also works for 5V Arduino’s.

Now why aren’t you getting close to 3.3V on D2?

Not a bad idea on principle, however you need to take into consideration the source current limit of ESP pins… reportedly 12ma max (less if you are also drawing current from other pins as well).

If you really want to go that route, then use a transistor or optoisolator inline with the pin, to switch an external, higher current source, of 3.3v for the sensors needs.

If the sensor is drawing current beyond source capacity, the source voltage will also drop.

I think I lost you @Gunner
what is the best solution for my code snippet?

keep it on the 3.3 volt directly?
use 5 volts directly?
use that transistor thingy that I’m not sure about? :slight_smile:

As the sensor is only a $1.50 ensure you have several and keep it powered permanently from 3.3V.

That sensor expects between 3.3v and 5.5v @ between 30-50mA, so you need to hook it up directly to a 5v power source for now (EDIT use 3.3v when sensor is hooked up to ESP)… until you can understand the transistor switch solution.

@Gunner if he does that isn’t there a danger he will feed his 3.2V rated analogue port with 5V?

@Costas Ah, yes… good point… I am still thinking in Arduino mode :wink:

@sagirokach - As stated above, uses a good 3.3v source then… the 3.3v pin on the ESP may not supply enough current on it’s own. If using an external 3.3v source, make sure it’s ground is shared with the ESP ground.

hmmmm OK… LOL you guys are killing me… changed my connection just to see that I need to change them back… lol

Thank you both for the help… I’ll see what I can dig on the other way with the digital pin. again thank you!

For your pondering pleasure…