I'm trying to read output from NPN proximity sensor: but from the GPIO pin I don't read any state change: unfortunately I can't read anything from it cause it seems to fluctuate even if I set the pull resistor(I'm in doubt if it should be pull up or down) on the GPIO.

The proximity sensor should go on HIGH when near a metal surface. The sensor work because the built-in LED light up when near a surface.

Also, another thing I'm in doubt if the ground of the sensor should ground to the raspberry and not on the power supply.

Someone has some idea of why I can't get reads from the sensor?

Hardware: Raspeberry Pi4 model b

Env: Python

1 Answer 1


I would say, the easiest way to connect a NPN one would be a relay. You could also use a voltage divider direct at the GPIO PIN. But that is dagerous for the Raspi and also you have to use inverse logik. So I show you the Relay version only.


simulate this circuit – Schematic created using CircuitLab

The V+ and GND wires of the sensor are connected to the power supply. One side of the relay coil is is connected to the V+ of the power supply. The other side of the coil is connected to the Sensers OUT wire.

If the sensor switches on, the OUT wire of the sensor draws current through the coil and the relay switches on. Then the 3.3 V pin of the raspberry gets connected to the GPIO pin.

The R1 resistor pulls the GPIO pin down to GND as long as the relay is off. So, the GPIO pin avoids floating around. That's all. Be sure to use a relay that closes when its coil gets powered.

Attention !!! Use the 3.3 Volt of the Raspi not the 5 Volt pin. The 5 Volt pin could damage the Raspi.

I don't know your neither your power supply nor your sensor type. So all values are guessed.


There is a risky way of connecting the OUT wire direct to the Raspi. This includes that you set the internal pull up resistor of the used GPIO pin active and connect the GNDs of the Pi, the sensor and the power supply all together.

Your sensor uses voltages of 10-36 Volts. I.e. you have to power it with the 12 Volt output of your power supply. You should avoid connecting the 12 V to the Pi. Be sure not to do so, it would kill it.

Here is a PPK-CAD export ;-) :

I think it is possible to use the internal pull up resistor. If that would not work, you could add a - lets say 4.7 kOhm resistor (R1) alternativly.

enter image description here

The elgo relays have 5V input AFAIK. They do not work with the 12 Volt of the power supply. But, instead of connecting the relay coil to the 12V power supply, you could connect it to the 5V of the power supply.

  • The sensor is this one: link and the power supply is this one link. For the wiring is not possible to direct wire to the Pi?, I am really limited to the additional components I can add... I've already have a relay: the elgoo 8 channel relay... is it good in any case?
    – rebellion
    Commented Jan 27, 2021 at 19:32
  • I' ve edited the answer. Commented Jan 28, 2021 at 9:29
  • Ok, after some hour of trials it worked, I've grounded all the wires as in your second draw. I had some problems because the ground wire of the power supply alimentation is not well connected. But after moving it a little bit every thing worked. I think also all the wire should be connected better since at the moment for testing purpose are all floating and not soldered...
    – rebellion
    Commented Jan 28, 2021 at 14:58
  • how about PNP? I have the same issue...
    – Li Ziming
    Commented Sep 28, 2021 at 2:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.