Non-Contact Water Sensor, will not read simple OFF/ON via GPIO

I’ve tried everything I know to do, it is probably something really simple I am missing, but would be very appreciative if someone could help me out.

I have a CQRSENYW001 CQRobot non-contact water level sensor
CQrobot Product Wiki Page

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);}
}

Here are two other variations I have tried:

binary_sensor:
  - platform: gpio
    id: water_sensor
    pin: 
      number: D7
    internal: true

  - platform: template
    name: "Water Level Sensor"
    lambda: |-
      if (id(water_sensor).state) {
               return true;
      } else {
               return false;
      }
binary_sensor:
  - platform: gpio
    id: input_sensor
    pin:
      number: D7
    on_state:
      - switch.turn_on: output_switch
    on_release:
      - switch.turn_off: output_switch

switch:
  - platform: gpio
    id: output_switch
    pin: D7

  - platform: template
    name: "Mirrored Output Switch"
    turn_on_action:
      - switch.turn_on: output_switch
    turn_off_action:
      - switch.turn_off: output_switch

Could it be the 5v logic level?

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…

1 Like

Have you used a multimeter to see what the ‘data’ pin is actually doing?

Have you tried another pin on your ESP?

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.

Have you tried another pin?

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.