Inductive proximity sensor false readings

I recently purchased a inductive proximity sensor linked below,

Uploaded some simple code and powered the sensor with a 12V power supply. Code provided below,

int a;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(3, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
a=digitalRead(3);
if(a==HIGH){
Serial.println("object detected");
}
else if (a==LOW){
Serial.println("no object detected");
}
delay(2000);
}

There's an led light integrated within the sensor module and reacts appropriately when in contact with a metal object. However the serial monitor within the IDE is giving me a mixture of false readings regardless if a metal object is being detected with a series of "objected detected" and "no object detected". I just want to know what might be causing the problem. Should I try a different power supply? Is there enough noise to cause false readings?

Most likely, there is a wiring problem.

Please post a hand drawn diagram showing how you connected the sensor to the Arduino, and identify the Arduino. You need a pullup resistor from the NPN output to Arduino Vcc. And, of course, connect all the grounds.

Just making sure I got this part correct: you are powering the sensor with 12V and you are sending that output of the 12V powered sensor to the input pin of a 5V Uno, and you are wondering why you are having issues, correct?

It should be noted that this type of sensor made in china often has a built-in 10kΩ pull-up, even though it is labeled NPN OUTPUT.
It raises the your sensor signal HIGH output to 12V.
And this causes damage to the Arduino pin.

Please test sensor output with DMM.

So just add a resistor between the signal wire and the GPIO pin?

No.
Start off with don't connect the output of the sensor to Arduino, but connect it to the DMM and bring a piece of metal close to it to check the output voltage with both state.

Alright. Sorry I'm new to this stuff.

It has been noted that the sensor works with5V but with a shorter range.

If you are going to use 12V to the sensor, the 12V must be isolated from the 5V pins. A resistor divider can be used as well as an opto-coupler, and a few other thingies.

Be aware that the 12V that was put put by the sensor when it was connected to the Uno may have damaged the Uno.

You can find this in the question section of the Amazon link you provided.

Your sensor output is pull-up to sensor power (12V in your case) via 10kΩ.

When interfacing a sensor with such a pulled-up NPN output with an Arduino, either use an opt coupler or use only one diode to connect you can also. (I know it some say this is bad...)

You’re checking if the inuit is high or low, not if it ‘goes’ high or low…
As long as the input is in either state, the message will be sent every delay() second.

P.s. in this example, you don’t need the if low test, because you already know it’s not high..l

Given the internal 10K pullup resistor, the additional series resistor will work fine, as long as you use at least 10K. That limits the current through the Arduino input protection diode to a safe value.

Better, use an optocoupler for complete isolation.

So the current has already been limited to 1.2 mA and probably has not harmed the Arduino.

A very reasonable approach is to use a series diode with cathode to the sensor and anode to the Arduino input using INPUT_PULLUP.

The diode should be reasonably close to the Arduino as the internal pullup is a fairly high impedance, but this will work well with the 10k in the sensor.

Using an optocoupler of course prevents any problems with commoning the grounds.

1 Like

Yeah, I remember explaining this to an another OP in other topic.
(But he used PNP sensor, but I misreading it as an NPN.) :joy:

You can use the same approach, but you need to add a "stiffer" pull down on the sensor to ensure it pulls the internal pull-up down adequately - perhaps 2k2 (dissipation 80 mW).

@Paul_B
Ya.
However, if for PNP output and doesn't require isolation, I feel that the simple voltage divider is enough.

to OP, sorry for usurp the topic.

Thanks for all the suggestions. Question, if I were to use a Raspberry Pi does it have the functionality to use INPUT_PULLUP ?

And I'll probably go for a optocoupler as that seems like a safe bet.

While this is not a RPi forum putting 12V onto the GPIO pins of a RPI will destroy the RPi.

I don't know, but it probably does. In any case, INPUT_PULLUP is merely a convenience. You can just use a pull-up resistor.

Which is why the diode method prevents such damage. :sunglasses:

Fully protected circuit:

Same circuit applies to Pi or ESP with 3.3 V

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