I need some confirmation on how to connect an 18m PNP diffuse photoelectric sensor to an Arduino.

  1. I am power the unit via an external 12 volt power supply. The ground between the power supply and the Arduino is shared.

  2. The black wire is connected to pin 8 of the Arduino.

When I read the value of pin 8 and output it to the serial monitor, the values are jumping between high and low (0 and 1) when the sensor is triggered.

Essentially I am looking for a stable high or low reading when the sensor in triggered.

  • 2
    \$\begingroup\$ Part number and link to datasheet for the PNP sensor, please. If it is powered with 12 V and you have connected the black to the microcontroller input then you may have destroyed the microcontroller. \$\endgroup\$
    – Transistor
    Jun 29 '19 at 18:55
  • \$\begingroup\$ Did you have any load resistors? What were they? \$\endgroup\$ Jun 29 '19 at 20:57


simulate this circuit – Schematic created using CircuitLab

Figure 1. (a) is what you seem to have done. (b) is the equivalent circuit which should make it clear that you have fed 12 V into the microcontroller input. (c) is the solution.

R1 and R2 form a 12:5 potential divider which reduces the 12 V to 5 V level compatible with the micro. You can select any resistor combination to give the ratio but ratio values in the 1k to 100k range should be fine. If I suspected that the 12 V supply could rise above 12 V I would reduce the ratio further to protect the input of the micro. D1 protects it in the event of a gross overload by diverting current to the 5 V supply. This limits the input voltage to about 5.6 V max.

  • \$\begingroup\$ how much noise to do expect on a 18m cable? \$\endgroup\$ Jun 29 '19 at 20:55
  • \$\begingroup\$ I have no idea. It could be located in the middle of nowhere. Not enough information in the question. \$\endgroup\$
    – Transistor
    Jun 29 '19 at 21:32
  • \$\begingroup\$ But obviously is a cause of errors. Perhaps an RC filter? Because there has to be a grid nearby to power this. \$\endgroup\$ Jun 29 '19 at 21:36
  • \$\begingroup\$ I was working on his/her statement that the black wire (emitter) was feeding 12V directly into a microcontrol input. \$\endgroup\$
    – Transistor
    Jun 29 '19 at 21:49

I presume you use an R divider to scale 12V down to logic levels. How you did this and the AC noise source to load impedance seems to have poor immunity and is creating logic errors when ON.

A PNP open collector will be high when ON at 12V at low impedance.

When off it will be open circuit shunted by your pull-down R divider network.

I suspect your R ratio is not correct giving marginal levels with a PNP = ON or low impedance. But you may have NPN+PNP outputs so this may be inverted so when Sensor is active, PNP is OFF low high impedance.

If PNP were in the OFF state , when sensor is ON, you have 2 choices:

  1. reduce R pull down. Which requires more power. E.g. 470 Ohms, Pd= 12*12 * 470 v = 1/3W so use >=1/2 W Resistor.

  2. Shunt your R pull down with another RC using >=10uF// <=470 Ohm The RC product can be > 100 ms for even better results and not significantly affect the slow response of the function of this sensor.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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