Hooked it up to the UNO3 and works perfectly as expected, but when I try to get the same result on a NodeMCU using espHome, it never responds to a change in signal. The only way I have been able to get it to register a change is if I disconnect and re-connect the data pin, and oddly enough when I do that it toggles between WET and DRY (moisture class). As in initial reading is constant DRY, disconnect, reconnect, reading is constant WET.
Here is a variation of code. I have tried multiple different ways, What am I missing?
- platform: gpio
name: "Tank A Water Level"
pin: GPIO13
device_class: moisture
filters:
- delayed_on: 100ms
- delayed_off: 100ms
The code supplied by the manufacturer for Arduino IDE is here and works perfect:
int ledpin=13;// initialize pin 13
int inpin=7;// initialize pin 7
int val;// define val
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”
}
void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to val
Serial.println(val); // print the data from the sensor
delay(100);
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
{ digitalWrite(ledpin,LOW);}
else
{ digitalWrite(ledpin,HIGH);}
}
There’s a dip switch on the sensor that allows to switch to a 3.3V HIGH signal instead of 5V, when I tried that, still would not read and additionally would not change state even when I disconnected and re-connected data pin…
Yeah, when sensor does not detect liquid, outputs a constant 1.5 mV, when liquid is detected outputs a constant 4.8V. Plus using the code supplied by the manufacturer for Arduino, works perfect…The Arduino code sets a pin as an output and reads from that, how is that achieved through esphome, or would it even be necessary?
I think you mean ‘input’, not ‘output’ (output would mean you’re driving the pin, not reading it. Regardless, the binary sensor you setup does just that: it reads the state of the pin. You could try an analog read, but that seems unnecessary.
Was talking about this code (that works) setting an output…but I guess it just makes the LED light up, sorry…
Yeah I have tried it on GPIO13, GPIO5, GPIO4 with variations of INPUT and INPUT_PULLUP and then tried GPIO16 with INPUT_PULLDOWN just for the hell of it…After doing more reading it seems that 5V is too high for the NodeMCU, so have switched the dip switch…also when I tried all of the other pins I don’t think I tried 3.3V on all of them so cycling back through them now with 3.3V set. Also attempted it on the D1 Mini pin D7.