Need help with digitalRead from PNP sensor

Hi
I have twp pcs XKC-Y25-PNP water level sensors that I'm trying to set up. I found a similar post here. Just as OP in that post, I am able to do an analog read from the sensor giving a value 1024 or ~20 depending on trigger status. In the same post OP is adviced to switch to a digitalRead and work with HIGH/LOW. I'd prefer that solution but can't get the digitalRead to work. If I have the sensor on pin A0 digitalRead always returns 0 (printing the read value to serial monitor). If I move to a digital pin, it always returns 1. I have tried switching from pinType INPUT to INPUT_PULLUP, but to no avail.

I have a Lolin D1 Mini, so only one analog pin. If I could get this to work with a digital pin, that would be great!

I have played with the built-in examples AnalogReadSerial, DigitalReadSerial and DigitalInputPullup making changes here and there to test.

Any help appreciated. Thanks.

Hi,

Can you please tell us your electronics, programming, arduino, hardware experience?

This is how you connect a PNP output sensor.
51u7avUIzqL.SS400

The value of R can be 220R, this will give you and output current of;
5/220 = 22mA which is within the 1 to 100mA output spec of your device.

Tom... :smiley: :+1: :coffee: :australia:

Hi
Thanks for the reply.
I'm not very harware or software skilled, but a lot of patience and my best friend is Google..

The sensor can be configured as both NC and NO with NO as default. When just using computer USB power through the D1 Mini I have little less than 4.5 v going through the sensor. Closing the circuit will make the voltage drop to about 3.15 v. So, not 0 v as described in the manual.

I haven't tried with a resistor. I only have a few 470R around but should give just above 1mA if my math is correct.. I didn't realize a resistor was needed but I'll see if there's a difference adding it.

You definitely need a resistor , 470R will be fine .

The resistor did the trick and all is now working as expected!
Thank you!

The only thing that should be changed in the schematic is to show a PNP transistor rather than the NPN.

1 Like

Good catch, can't remember where I pinch the image from.

Tom.... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.