I'm trying to digitalRead from a water sensor (the one in the picture) using NodeMCU v3,when I try the sensor with an Arduino board it works fine, but when I try it with an ESP8266 NodeMCU v3 it becomes less sensitive to water with some pins or doesn't work at all with the others. My question is: Is that normal or I'm doing something wrong ?

 int pin = 14;
 char printBuffer[128];
 int ledPin = 12;
 const int buzzerPin = 13;

  void setup()
    pinMode(pin, INPUT);
    pinMode(buzzerPin, OUTPUT);

  void loop()
    sprintf(printBuffer,"PIN %d level is %d\n",pin, digitalRead(pin));
    if (digitalRead(pin)==HIGH)
      tone(buzzerPin, 100,1000);
      digitalWrite(ledPin, HIGH);
      digitalWrite(ledPin, LOW);
      Serial.println("----------- ALARM ACTIVATED -----------"); 
      digitalWrite(ledPin, LOW);

enter image description here

  • The ESP8266 GPIO pins are funny beasts at the best of times. Using an analog sensor like that on a digital pin is bound to be different with different hardware. – Majenko Mar 13 '19 at 15:55
  • So isn't there a way to get more accurate results ? – Al Ma Mar 13 '19 at 15:57
  • 1
    Define "accurate"... There's nothing accurate about that sensor in the first place. – Majenko Mar 13 '19 at 15:57
  • I just want it to sense that it is in the water that's all – Al Ma Mar 13 '19 at 15:59
  • Do you have a link to the datasheet for the sensor so we can have a clue what the schematic is on it? – Majenko Mar 13 '19 at 16:00

You are using it wrong.

That sensor (crude as it is) is an analog sensor. It's not designed to be used connected to a digital pin.

It outputs an analog voltage proportional to the resistance between the tracks in the sensing portion.

By feeding that voltage into a digital pin you are relying on the logic threshold values, plus any other components (pullup resistors, etc), to give you a "wet" or "try" signal.

Since pins differ (some have pullups, some pulldowns, some nothing, etc) and the logic thresholds are only an approximation in any case, it's doomed to failure.

You need to connect the sensor to an analog input and compare the value read to a threshold value of your choosing. However the ESP8266 is very very poor when it comes to analog - it only has one analog input, and that is only capable of sensing between 0 and 1 volt.

  • how did you determine that that water sensor is analog Since it's got a + and - input and a third sense line I would expect it to put out a logic level signal. Why would it need power if it's just a variable resistor? (I guess it could be a voltage divider, but the picture looks like it has an IC and a few discrete components. – Duncan C Mar 13 '19 at 19:16
  • 1
    It has a transistor as an amplifier. – Majenko Mar 13 '19 at 19:21
  • I wanted to use the analog input, but since I'm using it with a soil moisture sensor I decided to use this water sensor in a digital mode, should I just use an multiplexer and use them both in a analog mode and compare the value read to a threshold value as you said ? otherwise I have an arduino uno, can I use it but without changing code (since the code above is just for testing , the actual project is displaying sensors value in a esp8266 webserver)? – Al Ma Mar 13 '19 at 19:30
  • reading multiple analog signals on an ESP8266 is simple – Jaromanda X Mar 14 '19 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.