I am trying to use this industrial color sensor to detect different colored objects using the Raspberry Pi.

I programmed in the colors and the display on the sensor shows that it is able to correctly identify them.

The sensor is programmable to allow you to set each output as PNP, NPN, or Push-Pull.

Assuming I am running the sensor off of a 12v wall wart (Sensor Operating Voltage is 10-30 VDC). What is the best way to interface the NPN/PNP switching outputs with the 3.3v GPIO pins on the Pi?

I was planning on using an optoisolator to protect the Raspberry Pi pin from the 12v power.

What's the preferred way of connecting an NPN/PNP output to a 3.3v GPIO pin?

Here is the wiring diagram from the sensor manual.

enter image description here

  • \$\begingroup\$ Opto-isolation is fine. But if you don't mind a shared ground, you probably could just use the NPN mode with a resistor to your 3.3 V supply and its output, coupled over to your input. That would add the least parts and most available parts -- a resistor only. You can also consider using a reed relay instead of the opto isolation, with a resistor, too. You have to decide what matters most to you. \$\endgroup\$
    – jonk
    Commented Feb 27, 2018 at 1:21
  • \$\begingroup\$ How do I figure out the value of the resistor? I connected the output of this sensor to positive probe on my multimeter and the other probe to ground and when the sensor is not activated the voltage reads 0.14v and when it is activated the voltage reads .33v. Why is it so low? The output is configured as an NPN (NO). Any chance you would be willing to provide a drawing of how it has to be wired? \$\endgroup\$ Commented Feb 28, 2018 at 0:01
  • \$\begingroup\$ I haven't read the manual, but NPN means "open collector" to me. If that's right, then this just means it goes "low impedance to ground" when active and "high impedance to nowhere in particular" when inactive. If so, tie a 10 kOhm resistor from the output to 3.3 V. See what shows up. It should be either 0 or 3.3V (approx.) \$\endgroup\$
    – jonk
    Commented Feb 28, 2018 at 0:58

1 Answer 1


NPN has an open-collector transistor between output and ground. By itself, this transistor can only drive the line low. To drive the line high, add a pull-up resistor to the +3.3V rail. This can be interfaced directly to the GPIO input.

PNP has an open collector transistor between the output and the sensor's supply rail. The minimum supply voltage is +10V. You can't interface this PNP output directly to a Raspberry Pi.

Opto-isolator approach can work with either NPN or PNP output.


Manual for the color sensor in question.

Additional reading on PNP and NPN outputs: here and here. (Those are in the context of industrial controllers, rather than Raspberry Pi, though.)


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.