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.
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.
Leo..
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.
Leo..
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.
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.
pinMode(metalSensor, INPUT_PULLUP);
can be changed to
pinMode(metalSensor, INPUT);
The internal pullup resistor was enabled for a switch.
Not needed for a voltage divider.
Leo..
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.