I have an Contrinex DW-AD-403-M5E 10-30v 200mA PNP sensor.
I would like to connect it to Arduino and to read on Serial Monitor a value (True or False / 0 or 1, etc) when sensor is activated.
I have problems with code.
Anyone can help?
Post the code you have, using code tags ("</>" button), and describe the problems.
You will need a resistive voltage divider on the output of that sensor, to protect the Arduino input from damage.
Thank you for fast reply!
I don't have really a code because I've been trying several combinations. Technically I need one from scratch...
I found simple code on Google but on Serial Monitor was showing all time different values (0, 128, 133, 1024, etc), continuously changing those values.
I've been trying some simple code like read pin A0 and write results to Serial Monitor.
I forgot to specify that sensor it has separate power supply (12 V) because is using 10 to 30 V and only data (black terminal from sensor) I have connected to Arduino A0 pin.
Can you develop your remark regarding voltage divider? What type, which one to use? I am a complete newbie and I need a little more details
Thank you in advance for your time spent to answer my questions.
If this is a PNP sensor, and you have supplied it with 12volt, and connected to an Analogue pin, you might have blown up pin A0 or even the whole chip.
A PNP sensor outputs 12volt, that has to be reduced to 5volt with a voltage divider.
The divider has to be connected to a Digital pin if you just want on/off.
Only NPN sensors (with pullup resistor) can be directly connected to an Arduino pin.
Is an PNP sensor and luckily my board is still working. In fact I am using an LeoStick.
I will make an voltage divider and try again but I still need some code which I do not know how to write...
"DigitalReadSerial" in the examples of the IDE does what you're asking for.
Replace the words "pushButton" with "metalSensor", and "buttonState" with "sensorState" for easier understanding.
Divider could be a 10k resistor to ground and a 15k resistor to the sensor output.
Connect the divider output to pin D2.
Thank you for help.
I have prepared the circuit as you told me but unfortunately I in Monitor I get random values, 0 and 1. When sensor is activated makes no difference.
I even tried with another sensor, NPN type but with same results.
I will try some more but if you have another idea you are welcome to share it.
I just find out that voltage divider is built wrong. Instead of 5v output I get only 4v. This could be the problem?
Arduino should read 4v, right?
PROBLEM SOLVED !!!
I just find out that ground from voltage divider should be connected to Arduino GND pin and to external power source GND.
Thank you for your advice about code and everything else.
For anyone interested in circuit and code, I have attached foto.
Reads a digital input on pin 2, prints the result to the serial monitor
// digital pin 2 has a PNP sensor attached to it.
int metalSensor = 2;
// the setup routine
// initialize serial communication at 9600 bits per second:
// make the sensor's pin an input:
// the loop routine runs over and over again forever:
// read the input pin
int sensorState = digitalRead(metalSensor);
// print out the state of the sensor
delay(1000); // delay in between reads for stability
Your circuit is not right.
Why 5volt for the indicator LED.
A PNP sensor can drive an indicator LED directly (between out and ground).
The divider is also drawn wrong. The junction of the two resistors goes to D2.
See the attached diagram.
can be changed to
The internal pullup resistor was enabled for a switch.
Not needed for a voltage divider.
Yes, you are right. That drawing is wrong but the circuit is ok I was in a hurry to post it.
For that LED I have to use another resistor which complicate too much everything and I don't really need it.
In my case I have a power supply with 3 outputs, 1x12V and 2x5V, that is reason I used external power supply for that LED.
I will try another version, to connect 2 LED on pins 10 and 11 and change the state of LED according sensor state, for example red when sensor is closed and green when is open (activated).
About the code, I have read some more and find out that INPUT_PULLUP in not necessarily. Thank for tip.