Try Embedded

Count parts with a 24 V proximity sensor

Read a PNP inductive sensor through an optocoupler module, count parts, and signal each finished batch on a stack light through a solid-state relay.

Industrial · Intermediate

In a real product: Packaging line (Industrial): Counts parts and signals each finished batch.

Factory sensors run on 24 V, not 5 V. A PNP sensor's output switches to +24 V when it detects something, and 24 V on an Arduino pin destroys it. The standard fix is an optocoupler module: the 24 V side lights an LED, and a phototransistor on the 5 V side pulls the pin low. The two sides share no wire at all.

What you'll learn

  • The 3-wire sensor: brown +24 V, blue 0 V, black output; PNP versus NPN
  • Isolating 24 V inputs with an optocoupler, and reading them as active LOW
  • Counting edges, not levels, so a part sitting in front of the sensor counts once
  • Switching a 24 V load from a pin with a solid-state relay

How it works

The inductive sensor is powered from the 24 V supply. Its black wire goes to the optocoupler module's IN1, and the module's COM to the supply's 0 V. The module's output side runs from the Arduino's 5 V: OUT1 is HIGH with no part and goes LOW while a metal part is in front of the sensor.

The sketch counts each HIGH-to-LOW change on pin 2: one part. Every 5 parts, it switches the solid-state relay on pin 8 for a second. The SSR's input (3 +, 4 −) takes the pin's 5 V directly, and its output switches 24 V to the stack light's amber tier.

Click the sensor to move a part in front of it, and again to move it away.

Try this

  • Wire the sensor's black wire straight to pin 2 and run: the simulator reports the over-voltage a real board would not survive.
  • Change the sensor to NPN in its settings and wire it without the module: BK to pin 2 with INPUT_PULLUP, and the supply's V− to the Arduino's GND (it then shares a ground).
  • Add a reset button for the counter on another opto input.

sketch.ino

// Counts parts passing a PNP inductive sensor (through an optocoupler module) and lights the stack light's amber
// tier for a second after every batch of 5.
const int SENSOR = 2;  // opto OUT1: LOW while a part is in front of the sensor
const int BATCH = 8;   // solid-state relay input: HIGH switches 24 V to the amber tier
const int BATCH_SIZE = 5;

int parts = 0;
bool lastPresent = false;
unsigned long batchAt = 0;

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR, INPUT);  // the module has its own pull-up
  pinMode(BATCH, OUTPUT);
  Serial.println("Counting parts");
}

void loop() {
  bool present = digitalRead(SENSOR) == LOW;
  if (present && !lastPresent) {  // a new part arrived
    parts++;
    Serial.print("Parts: ");
    Serial.println(parts);
    if (parts % BATCH_SIZE == 0) {
      Serial.println("Batch complete");
      digitalWrite(BATCH, HIGH);
      batchAt = millis();
    }
  }
  lastPresent = present;
  if (batchAt && millis() - batchAt >= 1000) {
    digitalWrite(BATCH, LOW);
    batchAt = 0;
  }
}

Lessons behind it