0

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);
 pinMode(ledPin,OUTPUT);
 Serial.begin(115200);
 }
 void loop()
 {
 sprintf(printBuffer,"PIN %d level is %d\n",pin, digitalRead(pin));
 Serial.print(printBuffer);
 if (digitalRead(pin)==HIGH)
 {
 tone(buzzerPin, 100,1000);
 digitalWrite(ledPin, HIGH);
 delay(100);
 noTone(buzzerPin);
 digitalWrite(ledPin, LOW);
 delay(100);
 Serial.println("----------- ALARM ACTIVATED -----------"); 
 }
 else{
 noTone(buzzerPin);
 digitalWrite(ledPin, LOW);
 }
 delay(500);
 }

enter image description here

VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Mar 13, 2019 at 14:51
13
  • 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. Commented Mar 13, 2019 at 15:55
  • So isn't there a way to get more accurate results ? Commented Mar 13, 2019 at 15:57
  • 1
    Define "accurate"... There's nothing accurate about that sensor in the first place. Commented Mar 13, 2019 at 15:57
  • I just want it to sense that it is in the water that's all Commented Mar 13, 2019 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? Commented Mar 13, 2019 at 16:00

1 Answer 1

1

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.

answered Mar 13, 2019 at 17:53
4
  • 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. Commented Mar 13, 2019 at 19:16
  • 1
    It has a transistor as an amplifier. Commented Mar 13, 2019 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)? Commented Mar 13, 2019 at 19:30
  • reading multiple analog signals on an ESP8266 is simple Commented Mar 14, 2019 at 3:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.