Parts list:
- Arduino (or any compatible board; I am using an LeoStick for this project)
- 3 wire inductive metal sensor ( I am using an Contrinex DW-AD-403-M5E 10-30 VDC 200mA PNP)
- External power supply 10-30 VDC for sensor
- LED (optional; I am using only for extra indication that sensor is working when activated)
- Wires
- Voltage divider from 12V to 5V (made from one 15k and one 10k resistors)
Step 2
Prepare voltage divider as follow:
- Solder 15k and 10k resistors together.
- Solder an wire same place where you solder the two resistors
Work principle: 15k resistor free end will be input from sensor, 10k resistor free end will be ground connected to Arduino GND pin and external power supply GND; middle wire will be output to Arduino digital input pin (D2 in my case)
Step 3
- Connect sensor positive cable (brown in my case) to 10-30 V power supply
- Connect sensor negative cable (blue in my case) to power supply ground
- Connect sensor data cable (black in my case) to 15k free end of voltage divider.
Step 4 (optional)
- Connect LED positive terminal to middle voltage divider wire
- Connect negative terminal to ground
In my case I've been using same external power supply for this LED due to extra 5V output.
Step 5
- Upload code to Arduino
Code: Select all
/* DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor/serial plotter
This example code is in the public domain.
*/
// digital pin 2 has a PNP sensor attached to it.
int metalSensor = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the sensor's pin an input:
pinMode(metalSensor, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int sensorState = digitalRead(metalSensor);
// print out the state of the sensor:
Serial.println(sensorState);
delay(1000); // delay in between reads for stability
}