Inductive proximity sensor lj12a3-4-z/dx 2-wire NC

This will be a small tutorial on how to use a 2 wire proximity sensor with Arduino, let's start.

First, of all how to connect the sensor?
Since we have only two wires, we should connect one to V+ and one to GND to get the sensor working. The problem with this setup is that we can't tell if the sensor detects a metal or not, and the only indicator for that is the sensor light.
image
Online datasheet

If we look closely at the datasheet provided by the sensor company we notice that the load should be connected between the V+ and the sensor V+ pin.

So how could that help? Ok, that is very helpful. First, we set a pin as an output (pin number 8) to power up the sensor. Second, we set a pin as an input (pin number 7) to check if the sensor detects metal or not. Third, we connect a resistor (10 K) between the pins (8,7) to be a load of our circuit. Fourth, we connect the sensor V+(Usually brown wire) to the detection pin (7) and the sensor GND(Usually blue wire) to Arduino GND.

I wrote a minimum script to test the sensor, check it:

bool metal_detected = false;

void setup()
{
  // Power pin
  pinMode(8, OUTPUT);
  // Detection pin
  pinMode(7, INPUT);
  // Powerup the sensor
  digitalWrite(8, HIGH);
  // Initialize serial port
  Serial.begin(9600);
}

void loop()
{
  if (digitalRead(7) == HIGH && !metal_detected)
  {
    // we detect a metal let's print it
    metal_detected = true;
    Serial.println("Metal detected :" + String(metal_detected));
  }
  else if (digitalRead(7) == LOW && metal_detected)
  {
    // sensor is on ideal state, no metal detected
    metal_detected = false;
  }
}

Upload the sketch and test the sensor with surrounding metals.

With a two-wire sensor, most people put the load resistor in the ground lead, and measure the voltage change across it.

Proper choice of the load resistor prevents overvoltage and burnout of the Arduino ADC input.

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