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);
}
-
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– Majenko2019年03月13日 15:55:44 +00:00Commented Mar 13, 2019 at 15:55
-
So isn't there a way to get more accurate results ?Al Ma– Al Ma2019年03月13日 15:57:23 +00:00Commented Mar 13, 2019 at 15:57
-
1Define "accurate"... There's nothing accurate about that sensor in the first place.Majenko– Majenko2019年03月13日 15:57:51 +00:00Commented Mar 13, 2019 at 15:57
-
I just want it to sense that it is in the water that's allAl Ma– Al Ma2019年03月13日 15:59:24 +00:00Commented 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?Majenko– Majenko2019年03月13日 16:00:42 +00:00Commented Mar 13, 2019 at 16:00
1 Answer 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.
-
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– Duncan C2019年03月13日 19:16:57 +00:00Commented Mar 13, 2019 at 19:16
-
1It has a transistor as an amplifier.Majenko– Majenko2019年03月13日 19:21:15 +00:00Commented 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)?Al Ma– Al Ma2019年03月13日 19:30:52 +00:00Commented Mar 13, 2019 at 19:30
-
reading multiple analog signals on an ESP8266 is simpleJaromanda X– Jaromanda X2019年03月14日 03:51:11 +00:00Commented Mar 14, 2019 at 3:51