How to connect an NPN sensor to Arduino safely

In this video it seems that there are no resistors used while connecting the sensor to Arduino. The brown wire (V+) is connected straight from battery to (Arduino's) Vin pin and blue wire (GND) is connected to Arduino's ground via a breadboard (and this is fine). BUT the black wire (signal) is also connected to Arduino's digital pin without any resistors in between.

However here the guy shows that there is a 1k resistor connected in parallel between signal and the V+ wire.

So should there be a resistor while connecting an NPN sensor to Arduino or not? If there is no resistor, doesn't the DI pin receive 9V from the battery, thus destroying the Arduino. And if there should be a resistor, where to connect it?

For open-collector NPN output you just connect a pull-up to 5V and the signal to an Arduino pin -
the output can only pull low to ground so no high voltages can get to the Arduino.

If paranoid add a diode inline with the signal to make sure it can only pull down (cathode to
sensor, anode to arduino pin).

You can use internal pull up if you like, its a bit weak for a remotely mounted sensor though.
Try a few k ohms if you have problems with noise.

The NPN output of the sensor is a switch to ground. If there is no R from 5v to the input, the pin is "floating". It will pick up stray signals and fluctuates between high and low. Tying the input to a known level removes the float condition. Normal values are from around 4.7k to 47k. You can also active the internal pull up R via software.

Weedpharma

As the others alluded to, there is an internal pull-up available. If you look at the code in the video description, you will see

void setup(){
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);

Yes, thank you. If I put the signal into an analog pin, can I just change the line "pinMode(2, INPUT_PULLUP);" to "pinMode(A0, INPUT_PULLUP)"?

On Arduino's tutorial page the code for using the internal pullup is: "digitalWrite(A0, HIGH)"

And so, is the other way to connect resistor in parallel between the brown wire (V+) and the black wire (signal)?

float metal;
int reading;
int metalPin = 3;

void setup()
{

Serial.begin(9600);
}

void loop()
{
reading = analogRead(metalPin);
metal = (float)reading*100/1024.0;
Serial.print("Metal in Proximity = ");
Serial.print(metal);
Serial.println(" %");
if(reading>250)
Serial.println("Metal Detected");
delay(1000);
}

This is the code I'm intending to use btw. [Source](Stage 4: Complete Beginner's Guide For Arduino Hardware Platform For DIY - CodeProject Metal Sensor)

n Arduino's tutorial page the code for using the internal pullup is: "digitalWrite(A0, HIGH)"

FYI: You can also write --> pinMode(14,INPUT_PULLUP); // since A0 is also DigitalPin #14

pinMode(A0, INPUT_PULLUP); should just work fine. Calling digitalWrite(pin, HIGH) was the old way of doing it (still works though). Arduino isn't that good in updating documentation.... :confused: But the internal pullup does not work with analogReadings.... And to my knowledge those sensors are not meant to give you a distance, only IF there is metal. So a digital reading, metal yes or no.

pwillard:
FYI: You can also write --> pinMode(14,INPUT_PULLUP); // since A0 is also DigitalPin #14

Yes you can but I think that's a foolish way. It's way less readable then just using the name.

I can't say I enjoy being called foolish for pointing out a simple "For Your Information" tidbit. No harm done. Just watch who you call foolish.

Some might also argue that calling it pin 14 is just as valid as A0, since that is the pin's name as well. It may not be printed that way on the board, but some coders like to be clear when they are not using an analog capable pin in its analog capacity as that may cause some confusion as well.

Many newcomers do not even realize right away that analog pins can be used as digital pins.

septillion:
But the internal pullup does not work with analogReadings.... And to my knowledge those sensors are not meant to give you a distance, only IF there is metal. So a digital reading, metal yes or no.

That's too bad.

What I need is an inductive sensor, that gives different output voltages for different metals. (I don't know if there is one.) In the project that I have in mind the distance between sensor and metal objects wouldn't vary.

Internal pull ups do work on analogue inputs.

If you're only interested in different values for different metals (so a meaningless unit) it may be possible to use the sensor. I came across a datasheet that specified that the current to the device changes with distance (and thus probably with metal type as well). (Can't find the sheet again...) So you can try to just add a resistor between GND and the GND of the sensor and measure the voltage across the resistor. The resistor value depends on the current demand of the sensor (under load).

@Mike, you're right! I always toughed the analogRead() function disabled the pullup....

So if I want Arduino's analog pin to receive 4-5V, how do I connect the resistors. 9V battery would be the NPN-sensor's power source. (The idea here is to use a sensor, that gives out different voltages based on the metal it detects. I don't know if there is one however.)

If I only use Arduino's internal pull-up, I think the analog pin will only receive 5 Voltage.

The idea here is to use a sensor, that gives out different voltages based on the metal it detects. I don't know if there is one however.

There is no such thing. If there were metal detectorists would never dig up ring pulls and always dig up gold or silver rings.

9V battery would be the NPN-sensor's power source.

Why? Dose the sensor need that?
Even so the sensor might have an open collector output.

Is this a mythical sensor or a real one.
If it is real post a link to it.

If you do have a 0 to 9V signal convert it to a 0 to 5V signal with a potential divider:-
Voltage_divider
Make the bottom resistor 10K and work out the top one.

septillion:
But the internal pullup does not work with analogReadings.... And to my knowledge those sensors are not meant to give you a distance, only IF there is metal. So a digital reading, metal yes or no.
Yes you can but I think that's a foolish way. It's way less readable then just using the name.

The internal pullup does work with analog readings.

MarkT:
The internal pullup does work with analog readings.

See reply #9 Mark.

Grumpy_Mike:
See reply #9 Mark.

They may not do anything useful.
Dwight

They may not, unless you want a 50k bias resistor to pull the input up.

It's a point of scale - if you are putting resistors of the order of 50k from the input pin to ground, then the internal pull-up will be very convenient. A LDR would likely be in this range for modest illumination. If your monitored resistances are below 1k, you probably need a similar value pull-up.

Paul__B:
They may not, unless you want a 50k bias resistor to pull the input up.

It's a point of scale - if you are putting resistors of the order of 50k from the input pin to ground, then the internal pull-up will be very convenient. A LDR would likely be in this range for modest illumination. If your monitored resistances are below 1k, you probably need a similar value pull-up.

True
Dwight